Ecstasyyy.
Ecstasyyy.

Reputation: 39

Discordjs DM all server members

I'm trying to DM all server members by a bot trough a command and it only DMs 4 people that are Administrators

message.guild.members.cache.forEach(member => { // Looping through each member of the guild.
    // Trying to send a message to the member.
    // This method might fail because of the member's privacy settings, so we're using .catch

    member.send(`hi`)

    .catch(() => (`Couldn't DM member ${member.user.tag}`));
    message.channel.send(`Success`)

        .catch(console.error);
});

Upvotes: 0

Views: 1864

Answers (2)

zS1L3NT
zS1L3NT

Reputation: 500

This operation can be extremely time-consuming. @SuleymanCelik's answer was partially correct because not every user is stored in the bot member cache. To get every single user in the server, you need to make a fetch() call for all the users like this.

guild.members
    .fetch()
    .then(members => members.forEach(member => {
        member
            .send("Hello!")
            .catch(() => {
                console.error(`Failed to send ${member.user.tag} a message`)
            })
    }))

Upvotes: 1

Efe Celik
Efe Celik

Reputation: 546

you can send a message to all users whose privacy settings are not turned on in this way

message.guild.members.cache.forEach(member => { // Looping through each member of the guild.
    // Trying to send a message to the member.
    // This method might fail because of the member's privacy settings, so we're using .catch
    member.send("Hello, this is my message!").catch(e => console.error(`Couldn't DM member ${member.user.tag}`));
});

Upvotes: 0

Related Questions