Guide to Create Mute and Unmute Command For Your discord bot 1

Guide to Create Mute and Unmute Command For Your discord bot

Here we are with new topic and new post and I will be teaching you how you can make a nice and clean mute command for your discord bot. As you know there are many types of bot with many types of commands, and they are basically based on its category like giveaway bot concentrate on giveaway commands and moderation bot concentrate on mods command. For your information I shall tell you that mute and unmute commands come in moderation category which is very useful for a server owner and the bot. I am creating this post to make you understand how these things works so, I requested you guys please try to understand what I did in codes instead of copying it.

Let’s start creating the mute command first, I will be dividing every thing in particular section.

MUTE COMMAND

First I will check if a user and a bot have required permission to use the command

 if (!message.member.hasPermission("MANAGE_ROLES")) {
      return message.channel.send(
        "Sorry but you do not have permission to unmute anyone"
      );
    }

    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      return message.channel.send("I do not have permission to manage roles.");
    }

Now we will define the user and send the error message to user if he did not mentioned member, We will also make sure that author does not mention himself

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

    if (!user) {
      return message.channel.send(
        "Please mention the member to who you want to unmute"
      );
    }
 
    if(user.id === message.author.id) {
      return message.channel.send("I won't mute you -_-");
    }

And it’s time to define the reason and send message if there is no reason

 
    let reason = args.slice(1).join(" ")
    
    
    if(!reason) {
      return message.channel.send("Please Give the reason to mute the member")
    }

After this we will find the role with name Muted and send error message if server do not have that role

  
    let muterole = message.guild.roles.cache.find(x => x.name === "Muted")
    
    
      if(!muterole) {
      return message.channel.send("This server do not have role with name `Muted`")
    }
    

Before we give Muted role to member we will let our bot to check if the mentioned do not have muted role already and if have then send the error message.

 if(user.roles.cache.has(muterole)) {
      return message.channel.send("Given User is already muted")
    }

We have done all the required error message and now it’s time to add Muted role and send message if mentioned user avoided all above statements.

 
    user.roles.add(muterole)
    
await message.channel.send(`You muted **${message.mentions.users.first().username}** For \`${reason}\``)
    
    user.send(`You are muted in **${message.guild.name}** For \`${reason}\``)
Guide to Create Mute and Unmute Command For Your discord bot 2

UNMUTE COMMAND

We will do same thing here and check all statement step by step so first we will be checking permission and if author do not have permission then send error message

 if (!message.member.hasPermission("MANAGE_ROLES")) {
      return message.channel.send(
        "Sorry but you do not have permission to unmute anyone"
      );
    }

    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      return message.channel.send("I do not have permission to manage roles.");
    }

Now we will define the user and send the error message to user if he did not mention member

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

    if (!user) {
      return message.channel.send(
        "Please mention the member to who you want to unmute"
      );
    }

After this we will find the role with name Muted and send error message if mentioned user do not have muted role

let muterole = message.guild.roles.cache.find(x => x.name === "Muted")
    
    
 if(user.roles.cache.has(muterole)) {
      return message.channel.send("Given User do not have mute role so what i am suppose to take")
    }

Now it’s time to take role and send message in channel and also in dm of mentioned user if user pass all above statement

   user.roles.remove(muterole)
    
    await message.channel.send(`**${message.mentions.users.first().username}** is unmuted`)
    
    user.send(`You are now unmuted from **${message.guild.name}**`)
Guide to Create Mute and Unmute Command For Your discord bot 3

RESULT CODE

Here is the result code – Github

One Comment

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *