問題描述
如何使用 Discord.js
列出角色中的成員.
How I can list members in a role using Discord.js
.
我的代碼:
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');
}
});
我將如何在嵌入中列出所有具有 go4
角色的成員.當在頻道中輸入消息 .go4-list
時,我希望機器人通過嵌入響應.
How would I go about listing all the members that have the go4
role in an embed. When the message .go4-list
is entered in a channel I would like the bot to respond with the embed.
推薦答案
<Role>.members
返回一個 GuildMember 的 rel="noreferrer">集合s.只需映射此集合即可獲得所需的屬性.
<Role>.members
returns a collection of GuildMembers. Simply map this collection to get the property you want.
根據您的情況,這是一個示例:
Here's an example according to your scenario:
message.guild.roles.get('415665311828803584').members.map(m=>m.user.tag);
這將從具有go4"角色的成員中輸出一組用戶標簽.現在您可以.join(...)
將此數組轉換為您想要的格式.
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.
另外,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('
'));
message.channel.send(ListEmbed);
}
});
正如@Wright 在他的回答中提到的那樣,如果成員過多,則會引發錯誤,因為嵌入最多只能容納 2048 個字符,因此您可能需要在發送嵌入之前進行一些檢查,然后處理超大嵌入通過將它們拆分為多個嵌入消息,或者使用基于反應的頁面.
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.
這篇關于如何在 Discord.Js 中列出所有具有角色的成員的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!