0

I'm looking a way to send a private message to à group of users, who have the same role (using discord.js)

I found the way to send a message (client.users.get("ID").send("Message"); But not the way to get all member who have the same role and loop on that list to send them a private message. Someone can help me?

pares
  • 3
  • 2

1 Answers1

0

You could do so by first making a list of all the members with the desired role (see Collection.filter()), and then loop through (see Map.forEach()) and send a DM to each member. Check out the code below for an example.

// Assuming 'guild' is defined as the guild with the desired members.

const roleID = ''; // Insert ID of role.
const members = guild.members.filter(m => m.roles.has(roleID) && m.user.id !== client.user.id);

members.forEach(member => {
  member.send('Hello there.')
    .catch(() => console.error(`Unable to send DM to ${member.user.tag}.`));
    // The above error would most likely be due to the user's privacy settings within the guild.
});
slothiful
  • 5,548
  • 3
  • 12
  • 36