0
votes

I have a discord bot, it has a mute command. The mute command works in theory, but when I tested it in a proper server, I noticed an issue. The role "Muted" was placed right at the bottom of the list of roles, meaning that all other roles above it override the mute, making it effectively useless. Would there be a method to make the role "Muted" created to the highest possible level the bot can assign, so the mute can be in effect?

Here is the code:

if (command === 'mute') {
  if (!message.member.hasPermission("MANAGE_ROLES") || !message.guild.owner) return message.channel.send("You don't have enough authority to do this.");

  if (!message.guild.me.hasPermission(["MANAGE_ROLES", "ADMINISTRATOR"])) 
    return message.channel.send("Give me the permissions first.")

  let role = message.guild.roles.find(x => x.name === "Muted");
  let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  if (!member) return message.reply(`I can't find the user ` + member)

  let duration = ms(args[1]);
  let reason = args.slice(2).join(' ');
  if (!reason) reason = "No reason provided";
  if (!role) {
    try {
      role = await message.guild.createRole({
        name: "Muted",
        color: "#000000",
        permissions: []
      })
      message.guild.channels.forEach(async(channel, id) => {
        message.channel.overwritePermission(role, {
          SEND_MESSAGES: false,
          ADD_REACTIONS: false,
          SEND_TTS_MESSAGES: false,
          ATTACH_FILES: false,
          SPEAK: false
        });
      });
    } catch (e) {
      console.log(e);
    }
  }

  member.addRole(role).catch(console.error);
  message.channel.send(`<@${member.id}> , you have been muted for ${duration}.`)

  setTimeout(function() {
    member.removeRole(role.id);
    message.channel.send(`${member} has been automatically unmuted.`);
  }, duration);
}
1

1 Answers

0
votes

Disclaimer: you seem to be using discord.js@v11, I'll reply with code for that version.

You should use the position option when creating the role:

role = await message.guild.createRole({
  name: "Muted",
  color: "#000000",
  permissions: [],
  position: 1
})

You can find a list of all the options you can use when creating a role here.