I have a question with mute system discord.js

71 Views Asked by At

I have a problem with my mute system. If I send +mute @member it doesn't give a role.

client.on('message', message => {
    if (message.content === "+mute") {
      if (!message.member.permissions.has("MANAGE_ROLES")) {
        return message.reply({content: `You need permissions to use command`})
        const mutedRole = message.guild.roles.cache.get('<986283559092359228>');
        if (!mutedRole)
          return message.reply('There is no Muted role on this server');
        const member = message.mentions.members.first();
        member.roles.add(role); 
        message.channel.send(member + 'has Been Muted');
      } else {
        message.channel.send("You didn't mention the user to mute!")
      }
    }
})
2

There are 2 best solutions below

4
MegaMix_Craft On

You define your mute role as mutedRole, but when you give member a role - you use role, which doesn't exist, so solution is simple, change member.roles.add(role); to member.roles.add(mutedRole);

0
Christy On

You need to write only the role ID when defining muteRole, so change this

const mutedRole = message.guild.roles.cache.get('<986283559092359228>');

To this

const mutedRole = message.guild.roles.cache.get('986283559092359228');

Then use the same name mutedRole in role.add() and use try/catch because the bot cannot give a role that is higher than its highest role, so try:

try {
    member.roles.add(mutedRole);
  } catch (err) {
           console.log(err)
           message.channel.send({content: `Role is higher than my highest role!`})
           } 

Just to add, you can directly do

message.channel.send({content: `${member} has Been Muted`});

You also need to check if the user has the mutedRole already or not