0
votes

I am trying to do a basic Discord.js bot, and I want to add a mute command.

I've tried this (the code below). That assigns a "foca muted you oof" role (I know the names are weird.) which doesn't allow you to do ANYTHING. But on my test account, even if it has that role assigned, I can still type messages extremely fine.

main.js:

    } else if(cmd === `${prefix}mute`) {
        if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("no no no, you can't manage messages. ok? alright?");
        let toMute = msg.guild.member(msg.mentions.users.first());
        if(toMute.user.username === msg.author.username) return msg.channel.send("oof. u can't mute urself.");
        if(!toMute) return msg.channel.send("idk who to mute. does that person exist????");

        let role = msg.guild.roles.find(r => r.name === "foca muted you oof");
        if(!role) {
            try {
                role = await msg.guild.createRole({
                    name: "foca muted you oof",
                    color: "#000000",
                    permissions: []
                });
                msg.guild.channels.forEach(async (channel, id) => {
                    await channel.overwritePermissions(role, {
                        "SEND_MESSAGES": false,
                        "ADD_REACTIONS": false
                    });
                })
            } catch(e) {
                console.log(e.stack);
            }
        }
        if(toMute.roles.has(role.id)) return msg.channel.send("oof. this user is already muted. tried to do a bravery, huh?");
        await toMute.addRole(role);
        msg.channel.send(`${toMute.user.username} has been muted.`);
    }

Can you tell me what's wrong?

1
Perhaps the user who is getting muted has another role that overwrites lower role permission overwrites? - Steam gamer
well, that worked before (and the user had extra roles) but now it doesn't work even if the user had 0 roles (except the muted one) - Stevineon
and now it doesn't work. - Stevineon
What about the overwrites for the @everyone role? - Steam gamer
Do you have the permissions set on the actual channels in the discord server set to disallow everything for that role? Because if you just unchecked the permissions on the role itself that isn't going to do anything. Additionally verify that the @everyone default role doesn't have any permissions set to explicitly allow on the channels themselves as well. - Gridonyx

1 Answers

0
votes
}else if(cmd === `${prefix}mute`){
  if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("You don't have permission to use this command."); //Check for permission.
  const toMute = msg.mentions.members.first(); //Get first mention of member.
  if(!toMute) return msg.reply("Member doesen't exist."); //If there's no member.
  if(toMute.user.id == msg.author.id) return msg.reply("You can't mute yourself."); //If member is author.
  const role = msg.guild.roles.get('Muted'); //Fetch the role.
  if(!role){ //If role doesen't exist create one.
    try{ //Try block which stops code in case of error.
      await msg.guild.creatRole({ //Create the role.
        name: 'Muted',
        color: '#F87666',
        permissions: ['SEND_MESSAGES': false, 'ADD_REACTIONS': false]
      });
      await msg.guild.channels.forEach(async (channel) => { //Overwrite every permission in every channel.
        'SEND_MESSAGES': false,
        'ADD_REACTIONS': false
      });
      await toMute.removeRoles(toMute.roles); //Remove every role member has.
      await toMute.addRole(role.id); //If everything went good add muted role to member.
      return msg.reply('Member muted successfully!')
    }catch(e){ //In case of error execute the code inside.
      msg.reply(`Error: ${e.message}`); //Tell author about error.
    }
  }else{ //If role exists
    if(toMute.roles.has(role.id)) return msg.reply('Member is already muted.'); //If member is muted reply.
    await toMute.removeRoles(toMute.roles);
    await toMute.addRole(role.id);
    return msg.reply('Member muted successfully!')
  }
}

I haven't tested it yet, it should work. Enjoy <3 Here's what I used:

hasPermission: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=hasPermission

Get function instead of find because it's deprecated (Soon to be removed): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

createRole: https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=createRole

removeRole: https://discord.js.org/#/docs/main/stable/class/GuildMemberscrollTo=removeRoles

addRole: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=addRole

has: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

forEach: https://www.w3schools.com/jsref/jsref_foreach.asp

Random red color for role: https://coolors.co/