如何在 Discord.Js 中列出所有具有角色的成员

How do I list all Members with a Role In Discord.Js(如何在 Discord.Js 中列出所有具有角色的成员)
本文介绍了如何在 Discord.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.

推荐答案

<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 中列出所有具有角色的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)