提问者:小点点

发送私人消息给一组用户(discord.js)


我正在寻找一种方法向一组具有相同角色的用户发送私人消息(使用discord.js)

我找到了发送消息的方法(client.users.get(“id”)。send(“message”);但无法让所有在列表中具有相同角色和循环的成员向他们发送私人消息。有人能帮我吗?


共1个答案

匿名用户

您可以首先列出具有所需角色的所有成员(请参见collection.filter()),然后循环(请参见map.foreach())并向每个成员发送DM。查看下面的代码以获取一个示例。

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