pr -
pr -

Reputation: 358

Message all members in server not working

I'm trying to make a command that sends a DM to every member in the server. The error I get is this:

(node:4741) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'members' of undefined

Here's the code:

bot.guild.members.cache.fetch().then(membersfetch => {
    membersfetch.forEach((member) => member.send("Hello"));
})

Upvotes: 1

Views: 48

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

There is no .guild property on the client/bot, only .guilds which contains all of the guilds the client is currently handling.

If your bot is only used on one server, you can safely get the .first() one like this, then fetch the members and send them a DM:

bot.on('ready', async () => {
  console.log('Bot is connected...');

  const guild = await bot.guilds.cache.first();
  const members = await guild.members.fetch();

  members.each((member) => member.send('Hello'));
});

If it's on more than one server, you need to get the guild ID and fetch the guild by that id:

bot.guilds.fetch('222078108977594368');

enter image description here

Upvotes: 1

Related Questions