简体   繁体   English

列出具有特定 Discord 角色的每个用户

[英]Listing every user that has a specific Discord Role

I'm looking to run a command that lists everyone that has a specified Discord Role [using the command, I will provide the ID for said role in question] Documentation and googling around seems to be a dead end as Discord updated their function.我正在寻找一个命令,列出具有指定 Discord 角色的每个人 [使用该命令,我将为有问题的角色提供 ID] 文档和谷歌搜索似乎是一个死胡同,因为 Discord 更新了他们的 ZC1C4145268E68ZA4458。 The biggest issue is that the current method I have here only works if the user is online/has typed recently and they will be stored in the cache along with their roles.最大的问题是,我在这里使用的当前方法仅在用户在线/最近输入过并且他们将与他们的角色一起存储在缓存中时才有效。 I was hoping to have one that lists the roles no matter what.我希望有一个无论如何都列出角色的。

 case 'rolecheck': var roleneeded = message.content.substring(prefix.length).split(' '); if(message.content.startsWith("$rolecheck")){ const ListEmbed = new Discord.MessageEmbed().setTitle('Users with this role:').setDescription(message.guild.roles.cache.get(roleneeded[1]).members.map(m=>m.user.tag).join('\n')); } break;

You can try enabling the intents from here and fetch all the members while logging or the alternative thing would be fetching members -您可以尝试从此处启用意图并在记录时获取所有成员,或者替代方法是获取成员 -

message.guild.members.fetch({cache : false}).then(members=>{
members.filter(x=>x.roles.cache.has("id")); //this would be your member array 
})

Note this method has the potential to throw an error Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.请注意,此方法可能会引发错误Error [GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time. if you don't have Guild Members intent enabled如果您没有启用Guild Members意图

You can use the message.guild.roles.fetch method like this:您可以像这样使用message.guild.roles.fetch方法:

case 'rolecheck':
    const roleneeded = message.content.substring(prefix.length).split(' ');
    if (message.content.startsWith("$rolecheck")) {
        message.guild.roles.fetch(roleneeded[1], true, true).then(role => {
            const ListEmbed = new Discord.MessageEmbed()
                .setTitle('Users with this role:')
                .setDescription(role.members.map(m => m.user.tag).join('\n'));
            message.channel.send(ListEmbed);
        }).catch(console.error)
    }
break;

The fetch(roleneeded[1], true, true) makes discord.js get the latest info from the API without using the cache. fetch(roleneeded[1], true, true)使 discord.js 从 API 获取最新信息而不使用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM