Sending private message to a group of user (Discord.js)

徘徊边缘 提交于 2020-01-17 01:15:15

问题


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?


回答1:


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.
});


来源:https://stackoverflow.com/questions/56263758/sending-private-message-to-a-group-of-user-discord-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!