Discord.js V12 如何显示具有特定角色的所有成员?

时间:2023-02-11
本文介绍了Discord.js V12 如何显示具有特定角色的所有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个命令,该命令将向我显示具有特定角色的所有成员.该命令应该类似于 $rolelist,它会在具有角色 TEST ROLE 的消息中显示所有成员的显示名称.如果可以的话,请帮助我:D

I am trying to make a command that will show me all the members with a certain role. The command should be like $rolelist and it shows all members' display names in a message that have the role TEST ROLE. Help me out if you can :D

client.on('message', async message => {
    if (message.content.startsWith(prefix + "rolelist")) {
        const testRole = message.guild.roles.cache.find(role => role.name == "TEST ROLE");
        const members = message.guild.members.filter(member => member.roles.find(testRole)).map(member => member.user.username)
        message.channel.send(`These people currently have the TEST ROLE: 
${members}`)
    }})

推荐答案

client.on('message', async message => {
    if (message.content.startsWith(prefix + "rolelist")) {
        const Role = message.guild.roles.cache.find(role => role.name == "TEST ROLE");
        const Members = message.guild.members.cache.filter(member => member.roles.cache.find(role => role == Role)).map(member => member.user.tag);
        message.channel.send(`Users with ${Role.name}: ${Members}`);
    };
});

由于您使用的是 V12,因此忘记将 cache 添加到 message.guild.membersmessage.roles.

You forgot to add cache to message.guild.members and message.roles since you are using V12.

另外,你使用的 find 函数有误.

Also, you were using the find function wrong.

你不能这样使用它:

member.roles.cache.find(testRole)

这是你应该如何使用它:

This is how you should use it:

members.roles.cache.find(role => role == testRole)

这篇关于Discord.js V12 如何显示具有特定角色的所有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Discord.js V12 消息收集器“错误"? 下一篇:将引号内的内容计算为一个参数

相关文章

最新文章