简体   繁体   中英

Checking predefined role and executing a command

I need to check if a user has a predefined role, and if it has the role, remove it. If it does not, then send a message saying something.

let user = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
let muterole = message.guild.roles.find(x => x.name === "Muted");

if (user.roles.has(muterole)) user.removeRole(muterole);
if (!user.roles.has(muterole)) message.channel.send("This user does not have the muted role!");

I expected it to remove the role if the user had it, and to send a message if he did not. However my current results are it sends the message no matter if he has the Muted role, and if he does have it, it does not remove it.

My end goal is this. My mute command uses node ms so I can mute for a specified time, so I need an unmute, but if the user is unmuted, that does not stop the timer (I don't think there is a way) so my idea was to put in the mute command -> "If user has mute after time remove the role, but if the user does not have the mute role, return"

this might work to detect if the member has the role

 // assuming role.id is an actual ID of a valid role: if(message.member.roles.has(role.id)) { console.log(`Yay, the author of the message has the role!`); } else { console.log(`author does not have the role`); } 

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