本文介绍了如何列出在Disord.Js中有角色的所有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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.

推荐答案

返回 rel = noreferrer> GuildMember 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('\n'));
        message.channel.send(ListEmbed);                    
    }
});

正如@Wright在他的回答中提到的那样,如果成员过多,则会由于embed最多只能容纳2048个字符,因此您可能需要先进行一些检查,然后再发送出embed,然后通过将它们拆分成多个embed消息或使用基于响应的页面来处理过大的embed。

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.

这篇关于如何列出在Disord.Js中有角色的所有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:22