javascript 如何列出在 Discord.Js 中具有角色的所有成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48897574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I list all Members with a Role In Discord.Js
提问by KillerVillnave
How I can list members in a role using Discord.js.
我如何使用Discord.js.
My code:
我的代码:
client.on("message", message => {
var guild = message.guild;
let args = message.content.split(" ").slice(1);
if (!message.content.startsWith(prefix)) return;
if (message.author.bot) return;
if(message.content.startsWith(prefix + 'go4-add')) {
guild.member(message.mentions.users.first()).addRole('415665311828803584');
}
});
How would I go about listing all the members that have the go4role in an embed. When the message .go4-listis entered in a channel I would like the bot to respond with the embed.
我将如何列出go4在嵌入中具有该角色的所有成员。当.go4-list在频道中输入消息时,我希望机器人使用嵌入进行响应。
回答by newbie
<Role>.membersreturns a collectionof GuildMembers. Simply map this collection to get the property you want.
<Role>.members返回集合的GuildMember秒。只需映射此集合即可获得您想要的属性。
Here's an example according to your scenario:
这是根据您的场景的示例:
message.guild.roles.get('415665311828803584').members.map(m=>m.user.tag);
This will output an array of user tags from members that have the "go4" role. Now you can .join(...)this array to your desired format.
这将输出来自具有“go4”角色的成员的用户标签数组。现在您可以.join(...)将此数组转换为您想要的格式。
Also, guild.member(message.mentions.users.first()).addRole('415665311828803584');could be shortened down to: message.mentions.members.first().addRole('415665311828803584');
此外,guild.member(message.mentions.users.first()).addRole('415665311828803584');可以缩短为:message.mentions.members.first().addRole('415665311828803584');
Here's a rough example of how it would look as a result:
这是结果如何的粗略示例:
client.on("message", message => {
if(message.content.startsWith(`${prefix}go4-add`)) {
message.mentions.members.first().addRole('415665311828803584'); // gets the <GuildMember> from a mention and then adds the role to that member
}
if(message.content == `${prefix}go4-list`) {
const ListEmbed = new Discord.RichEmbed()
.setTitle('Users with the go4 role:')
.setDescription(message.guild.roles.get('415665311828803584').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
});
As @Wright mentioned in his answer, if there are over many members it will throw an error as an embed can only hold 2048 characters maximum, so you may want to do some checks before sending out the embed and then handle oversized embeds by either splitting them into multiple embed messages, or using reaction based pages maybe.
正如@Wright 在他的回答中提到的,如果成员过多,它会抛出错误,因为嵌入最多只能容纳 2048 个字符,因此您可能需要在发送嵌入之前进行一些检查,然后通过拆分来处理超大嵌入将它们嵌入多个嵌入消息中,或者使用基于反应的页面。
回答by Wright
if(message.content.startsWith("//inrole")){
let roleName = message.content.split(" ").slice(1).join(" ");
//Filtering the guild members only keeping those with the role
//Then mapping the filtered array to their usernames
let membersWithRole = message.guild.members.filter(member => {
return member.roles.find("name", roleName);
}).map(member => {
return member.user.username;
})
let embed = new discord.RichEmbed({
"title": `Users with the ${roleName} role`,
"description": membersWithRole.join("\n"),
"color": 0xFFFF
});
return message.channel.send({embed});
}
Example use on discord:
不和谐的示例使用:
Do note though that if there are a lot of members with the role, you may get an error telling you that you have exceeded the number of chars you can put in an embed. In such a case, you can decide to send multiple embeds splitting the users.
但请注意,如果该角色有很多成员,您可能会收到错误消息,告诉您已超出可以放入嵌入的字符数。在这种情况下,您可以决定发送多个嵌入来拆分用户。
回答by Syntle
Since Discord v12, you now have to use roles.add()instead of addRole()
从 Discord v12 开始,您现在必须使用roles.add()代替addRole()


