向一组用户发送私人消息(Discord.js) [英] Sending private message to a group of user (Discord.js)

查看:31
本文介绍了向一组用户发送私人消息(Discord.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我找到了发送消息的方法(client.users.get("ID").send("Message");但是,这不是让所有具有相同角色的成员在该列表上循环向他们发送私人消息的方法.有人可以帮助我吗?

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?

推荐答案

您可以通过首先列出具有所需角色的所有成员的列表来做到这一点(请参阅 Map.forEach()),并将DM发送给每个成员.请查看下面的代码作为示例.

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

这篇关于向一组用户发送私人消息(Discord.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆