Home > Mobile >  Discord.js v13 How to prevent mod commands from working on mods?
Discord.js v13 How to prevent mod commands from working on mods?

Time:02-19

So I wanted to make a mute command for my Discord bot. It is supposed to mute a mentioned user untill they are unmuted through another command (WIP), additionaly there should be an option to give a reason. If there is no reason provided it just says Reason: none. I've managed to Restrict the command acces to a mod role (although in the following code it checks for the MODERATE_MEMBERS flag, because that's a better way to do it imo) and made it return all the required Error messages like "I can't mute that user" and such. Here's my code:

const { Permissions } = require('discord.js');

module.exports = {
    name: 'mute',
    description: "Mutes a user for an unlimited amount of time.",
    execute(message, args)
    {
        const target = message.mentions.members.first();
        let muteReason = args.join(" ").slice(22);

        if(message.member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
        {
            if(target)
            {
                if(target.id == '943093289031176203')
                {
                    message.reply("I can't mute myself.")
                }
                else if(message.member == target)
                {
                    message.reply("You can't mute yourself!")
                }
                else if(target.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
                {
                    if(!muteReason)
                    {
                        muteReason = "None"
                    }
                    let muteRole = message.guild.roles.cache.find(role => role.name === "muted");
                    let memberTarget = message.guild.members.cache.get(target.id);

                    memberTarget.roles.add(muteRole.id);
                    message.reply(`<@${memberTarget.user.id}> has been muted. Rason: `   muteReason);
                }
                else
                {
                    message.reply("I can't mute that user.")
                }
            
            }
            else
            {
                message.reply("You have to mention a valid member of this server.")
            }
        }
        else
        {
            message.reply("You can't use that.")
        }
       
    }
}

The problem I'm having is that moderators can mute other moderators which shouldn't happen. So basically I need a solution that makes the command not work on other people who have a mod role or a command that doesn't work on people with certain permissions (preferably the 2nd one since it's gonna be more usefull).

CodePudding user response:

You could check for moderator permissions on the specified user, then only mute if the user doesn't have the permission. This way it won't allow mods to be muted.

//...

  let memberTarget = message.guild.members.cache.get(target.id);

  if (memberTarget.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS)) {
    message.reply(`<@${memberTarget.user.id}> is a Moderator. You cant' mute them.`);
  } else {
    memberTarget.roles.add(muteRole.id);
    message.reply(`<@${memberTarget.user.id}> has been muted. Reason: `   muteReason);
  }

//...
  • Related