Discord.js V12 how can I show all members with a certain role?

旧街凉风 提交于 2021-01-24 12:22:22

问题


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: \n${members}`)
    }})

回答1:


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}`);
    };
});

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

Also, you were using the find function wrong.

You can't use it like this:

member.roles.cache.find(testRole)

This is how you should use it:

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


来源:https://stackoverflow.com/questions/62646389/discord-js-v12-how-can-i-show-all-members-with-a-certain-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!