简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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