0

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"

Ragnar Lothbrok
  • 306
  • 1
  • 4
  • 22
  • `user.roles` seems to be an array. I know of no standard `arr.has` method, did you mean to use [Array.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – James Dec 31 '18 at 16:22
  • Someone told me it was `member.roles.has(mutedRole.id)` but I was unsure on how to adapt that to read the user from `let user =` and the role from `let muterole =` – Ragnar Lothbrok Dec 31 '18 at 16:45
  • I think you’re right about using the id. See [this question](https://stackoverflow.com/questions/45317305/find-out-if-someone-has-a-role) – James Jan 01 '19 at 20:59

1 Answers1

0

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`);
}
Golden_
  • 41
  • 2
  • 12
  • I am unsure if this works, I can't seem to make it work, can you maybe try and adapting it to fit into here? https://hastebin.com/ehinatabij.js Basically, if the user has the role, run that, if not, cancel. Because if someone manually unmutes, the unmute embed and attempt will run anyway at the moment. – Ragnar Lothbrok Jan 01 '19 at 00:55