discord.js - 我的不和谐机器人的成员命令 - discord.js
问题描述
所以我一直在尝试创建一个 ?members 命令来列出所有具有角色的用户。
到目前为止,我有这个:
if (message.content.startsWith("?members")) {
let roleName = message.content.split(" ").slice(1).join(" ");
let membersWithRole = message.guild.members.filter(member => {
return member.roles.find("name", roleName);
}).map(member => {
return member.user.username;
})
const embed = new Discord.RichEmbed({
"title": `Members in ${roleName}`,
"description": membersWithRole.join("\n"),
"color": 0xFFFF
});
return message.channel.send(embed);
}
因此,如果您键入角色的确切名称,它会起作用,但在您 ping 或键入第一个单词时不起作用。我已经尝试了几个小时来弄清楚如何做到这一点,我想我应该寻求帮助。
提前致谢!
解决方案
Pings get translated into a code as they come through, there is a lot of information on how to parse them in the official guide After it's parsed into a role id you can just use members.roles.get() because that is what they are indexed by.
As for finding a partial name, for that you are going to have to run a function on your find and use String.includes.
return member.roles.find(role => role.name.includes(roleName));
This will also work for find the whole name of course, so it can replace your existing line.
However, this may result in more than one role. This is also true for searching by the entire role name, however, as there are no restrictions on duplicate named roles. For this you may want to invert you design and search through message.guild.roles first for any matching roles, then search for members with the roles found.
推荐阅读
- python - Keras:可以给 lamda 层命名吗?
- xml - PostgreSQL 大 XML 文件解析
- android - 如何将2个编辑文本和一个按钮水平居中
- php - 如何使用查询生成器从不同的表中检索数组值?
- python - Pycharm:有条件地运行集成测试
- javascript - 使用 Angular 5 和 Node js 后端的 get 方法未正确发送多个参数
- python - 从 Bigquery 中读取几行作为侧面输入,得到 None
- ios - 在ios中将数据流重定向到wifi或蜂窝
- javascript - 按特定键值对多个数组进行排序
- ios - ARKit 中的 camera.transform 和 eulerAngles 有什么区别?