简体   繁体   中英

discord.js how to find all users assigned @everyone when bot is brought online in server

I am having trouble attempting to find users assigned to @everyone when I bring the bot online in my server. I can do it via commands in my discord server, however, when trying to do it automatically, I get the error:

Cannot read properties of undefined (reading 'roles')

Here is my code:

const Discord = require('discord.js');

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', (message) => {
    console.log('Bot is online!');
    client.commands.get('find').execute(message);
});
module.exports = {
    name: 'find',
    description: "",
    execute(message){

        const role = message.guild.roles.cache.find(role => role.name === "@everyone");

        console.log('complete')
    }
}

How do I find all the members assigned @everyone when my bot goes online instead of having to type a command in my discord server?

@everyone isn't a role on discord

Roles on discord.js are well, roles. It's giving you this error because it's trying to read it as a role, which it isn't to begin with. Discord treats it as a special "kind" of class of your server members.

Rather than trying to find for @everyone , you should instead use a role that was manually made and assigned to everybody in your server instead, basically just a member role in other words.

@everyone role members are basically the total members of the guild

It isn't something you can assign/remove or fetch it as a role. Incase you need the members, fetch the guild members

<Guild>.members.fetch()// Make sure to have the intents GUILD_MEMBERS enabled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM