mute command discord.js v12 code example

Example 1: unban command discord.js v12

if(command === "unban"){
    if(!msg.member.hasPermission("BAN_MEMBERS")) {
      return msg.channel.send(`**${msg.author.username}**, You do not have perms to unban someone`)
    }
    
    if(!msg.guild.me.hasPermission("BAN_MEMBERS")) {
      return msg.channel.send(`**${msg.author.username}**, I do not have perms to unban someone`)
    }
    
    let userID = args[0]
      msg.guild.fetchBans().then(bans=> {
      if(bans.size == 0) return 
      let bUser = bans.find(b => b.user.id == userID)
      if(!bUser) return
      msg.guild.members.unban(bUser.user)
})
    
  ;}

Example 2: mute someone discord.js

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = "!";
const token = ""; // Fill in your token

client.on("message", msg => {
  msg.content.startsWith(prefix + "mute") {
    
    
    // Variables
    var muteRole = msg.guild.roles.find(role => role.name.toLowerCase().includes("muted"));
    var muteChannel = msg.guild.channels.find(channel => channel.name.includes("modlogs"));
    var muteUser = msg.mentions.members.first();
    var muteReason = msg.content.slice(prefix.length + 27);
    
    
    // Conditions
    if (!msg.member.hasPermission("MANAGE_MESSAGES")) return msg.channel.send("You don't have the permissions");
    if (!muteUser) return msg.channel.send("You have to mention a valid member");
    if (!muteChannel) return msg.channel.send("There's no channel called modlogs");
    if (!muteRole) return msg.channel.send("There's no role called muted");
    if (!msg.guild.member(client.user.id).hasPermission("MANAGE_ROLES")) return msg.channel.send("I Don't have permissions");
    if (!muteReason) muteReason = "No reason given";
    
    // Embed
    var muteEmbed = new Discord.RichEmbed() 
    .setTitle("Mute")
    .addField("Muted user", muteUser)
    .addField("Reason", muteReason)
    .setFooter(`Muted by ${msg.author.tag}`)
    .setTimestamp();
    
    //Mute
    muteUser.addRole(muteRole);
    msg.channel.send(`${muteUser} has been muted`);
    muteChannel.send(muteEmbed);
    
  }
})

Example 3: mute everyone in call discord.js

client.on('message', (message) => {
    if (message.content == '/muteAll') {
        let channel = message.member.voiceChannel;
        for (let member of channel.members) {
            member[1].setMute(true)
        }
     }
});

Example 4: discord.js v12 command ban

if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
let User = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0])
if (!User) return message.channel.send("Invalid User")
if (User.hasPermission("BAN_MEMBERS")) return message.reply("Invalid Permissions")
let banReason = args.join(" ").slice(22);
if (!banReason) {
  banReason = "None"
}

User.ban({reason: banReason})

Example 5: discord.js unmute script

client.on('message', message => {
    if (message.content.startsWith(prefix + 'unmute')) {
        if (message.member.hasPermission('MANAGE_ROLES')) {
            const role = message.guild.roles.cache.find(role => role.name === 'Muted');
            const member = message.mentions.members.first();
            member.roles.remove(role);
            message.channel.send(member + ' Has Been Unmuted');

        } else {
            message.channel.send("Please Mention Who To Unute")
        }
    }
})