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

2022-01-10 00:00:00 roles javascript discord.js

我正在尝试创建一个命令,该命令将向我显示具有特定角色的所有成员.该命令应该类似于 $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)

相关文章