问题
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