向一组用户 (Discord.js) 发送私信
Sending private message to a group of user (Discord.js)
我正在寻找向具有相同角色(使用 discord.js)的一组用户发送私人消息的方法
我找到了发送消息的方法(client.users.get("ID").send("Message");
但不是让所有具有相同角色并在该列表上循环的成员向他们发送私人消息的方法。有人可以帮助我吗?
您可以先列出具有所需角色的所有成员(请参阅 Collection.filter()
), and then loop through (see 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.
});
我正在寻找向具有相同角色(使用 discord.js)的一组用户发送私人消息的方法
我找到了发送消息的方法(client.users.get("ID").send("Message"); 但不是让所有具有相同角色并在该列表上循环的成员向他们发送私人消息的方法。有人可以帮助我吗?
您可以先列出具有所需角色的所有成员(请参阅 Collection.filter()
), and then loop through (see 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.
});