Mute command help

const prefix = “!”

client.on(‘message’, message => {
if(message.content.startsWith(prefix + ‘mute’)){
let user = message.mentions.users.first();
let role = message.guild.roles.find(r => r.name === ‘Muted’);
if(!role) message.guild.createRole({name: ‘Muted’});
if(user.bot){
return message.channel.send(I can't mute ${user} because he is a bot);
}
if(user.hasPermission(‘ADMINISTRATOR’)) {
return message.channel.send(I can't mute ${user} because he is staff);
}

if(!user){
    message.channel.send(`There's no person to mute tho`);
}
message.guild.channels.forEach(f => {
    f.overwritePermissions(role, {
        SEND_MESSAGES: false
    });
    user.addRole(role);
   
});
 message.channel.send(`I muted ${user}`);

}
});

client.on(‘message’, message => {
if(message.content.startsWith(prefix + ‘unmute’)){
let user = message.mentions.users.first();

let role = message.guild.roles.find(r => r.name === 'Muted');

if(!user.roles.has(role)) {
return message.channel.send(He is not muted);
}
user.removeRole(role).then(message.channel.send(Unmuted ${user}));

}
});

This is my code, I just don’t know how to fix it because it isnt working :confused:

What exactly doesn’t work?

It doesn’t give the role to the user.

I think the bot must have sufficient permissions to give roles to users…

What is the exact error message?

It just doesn’t add the role.

The bot has EVERY permission.

Try message.mentions.members.first();

Hey @Krxvee,

Can you do

.catch(console.error)

Under user.addRole(role) ?

That worked. But the unmute doesn’t work with taking the role off.

This quote above is in the unmute command.

Try changing that to this as well:

let user = message.mentions.members.first();

I did that. But now it says that they’re not muted and it doesn’t take the role away.

Instead of this, can you try:

let ifHasRole = message.member.roles.find(role => role.name === 'Muted');
if(!ifHasRole){
return message.channel.send('He is not muted');
}

Omg! thank you so much. This works!!