How to sent all members each server a message in 'ready'

When my bot starts up, I want it to send a message to all the members in each server, for example

I want it to say Hello.

client.on('ready', message  => {
  client.user.setActivity(`${client.guilds.cache.size} servers have members ${client.users.cache.size}`+` | !help `, { type: 'WATCHING' })

  client.guild.send("Hello")
});

1 answer

  • answered 2020-06-02 12:57 Tenclea

    I think this code might be what you need :

    client.users.cache.forEach(user => {
        try {
            user.send('Hello');
        } catch (e) {
            console.log(`Could not send the message to ${user.tag} : ${e}`);
        };
    });
    

    This simply grabs all users the bot can communicate with, tries to send a message, and logs an error if the message could not be sent (The error will most likely be caused by the user locking their DMs / blocking the bot).