I am trying to make my bot DM all people (not just new members), and i have no clue how i would go about doing this. any help would be greatly appreciated.
Asked
Active
Viewed 4,263 times
1 Answers
0
// Async context (within async function), 'message' being the command message.
const members = message.guild.members.filter(m => !m.user.bot).array(); // Filter out bots.
let undelivered = 0;
for (let i = 0; i < members.length; i++) { // Using an array and a for loop rather than
const member = members[i]; // Collection.forEach() due to the fact that
await member.send('Hello there.') // the latter will move onto the proceeding
.catch(() => undelivered++); // code before waiting for the promises to
} // fulfill. https://stackoverflow.com/a/37576787
message.channel.send(`Messages sent. ${undelivered} members couldn't receive it.`)
.catch(console.error);

slothiful
- 5,548
- 3
- 12
- 36
-
The code above must be within an async function (declared as `async function(...) {` or `async (...) => {`). – slothiful May 24 '19 at 22:13
-
The callback must be async. That line should look like this: `bot.on('ready', async () => {`. You'll also need to change `message.guild` to the guild you want to use (i.e. `client.guilds.get('id')`). – slothiful May 24 '19 at 22:17
-
is there a way without id – Golden_ May 24 '19 at 22:34
-
You could use [`Collection.find()`](https://discord.js.org/#/docs/main/stable/class/Collection?scrollTo=find) to search for a guild with a certain property, i.e. `client.guilds.find(g => g.name === 'test')`. – slothiful May 24 '19 at 22:36
-
You can use `message.guild.members.filter(...).forEach(async member => { // code })` instead of the for loop – PLASMA chicken May 25 '19 at 17:34
-
As explained in the comment, `forEach()` won't wait for promises to fulfill, and will therefore send the message after the loop immediately. https://stackoverflow.com/a/37576787 – slothiful May 25 '19 at 17:35