DudeThatsErin

Untitled

Nov 17th, 2020 (edited)
671
20 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require("discord.js");
  2. const ms = require("ms");
  3.  
  4. module.exports = {
  5.   name: "mute",
  6.   description: "This will mute users for at least 1 second. You specify the time the user is muted.\n**Note:** You have to have the MANAGE_MESSAGES permission to use this command.",
  7.   aliases: ['tempmute', 'turnoff', 'off', 'nospeak', 'm'],
  8.   usage: ".mute @username or user ID [length of time] [reason]",
  9.   inHelp: "yes",
  10.   execute (message, args) {
  11.  
  12.     // Defines and logs variables
  13.     let reason = args.slice(1).join(' ');
  14.     console.log('reason:' + reason);
  15.     let person = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
  16.     console.log('user:' + person);
  17.     const cachedUserRoles = {};
  18.     console.log('user roles are cached.')
  19.    
  20.     // find person
  21.     if (!person) {
  22.       message.reply(":x: Couldn't find that member!");
  23.       return;
  24.     }
  25.     // Requiring reason
  26.     if (reason.length < 1) {
  27.       message.reply('You must supply a reason for the mute.');
  28.       return;
  29.     }
  30.    
  31.     // makes sure bot and user have roles.
  32.     if(!message.member.hasPermission("MUTE_MEMBERS", "MANAGE_ROLES", "ADMINISTRATOR") || !message.guild.owner) {
  33.       message.channel.send(":no_entry_sign: You do not have the permission to use this command!");
  34.       return;
  35.     }
  36.  
  37.     if(!message.guild.me.hasPermission(["MUTE_MEMBERS", "MANAGE_ROLES", "ADMINISTRATOR"])) {
  38.       message.channel.send(":no_entry_sign: I do not have the permission to add roles!");
  39.       return;
  40.     }
  41.  
  42.     //finds role and if it isn't there, creates it.
  43.     const muterole = message.guild.roles.cache.find( r => r.name === 'Muted');
  44.  
  45.     if (!muterole) {
  46.       try {
  47.         muterole = message.guild.roles.create({
  48.           data: {
  49.             name:'Muted',
  50.             color: 'RED',
  51.             permissions: []
  52.           },
  53.           reason: 'Need a role for muted users.',
  54.         });
  55.         message.guild.channels.forEach((channel, id) => {
  56.           channel.overwritePermissions(muterole, {
  57.             SEND_MESSAGES: false,
  58.             MANAGE_MESSAGES: false,
  59.             ADD_REACTIONS: false,
  60.             CONNECT: false,
  61.           });
  62.         });
  63.       }catch(e) { console.log(e.stack); }
  64.       console.log('role created!');
  65.     } // end if
  66.  
  67.     // time
  68.     const time = args[2];
  69.     console.log('time:' + time);
  70.  
  71.     if (!time)
  72.       return message.reply('How long would you like this user to be muted?');
  73.  
  74.     //Cache their already existing roles
  75.     cachedUserRoles[person.id] = person.roles.cache;
  76.  
  77.     //Set their roles to an empty array to clear them, then add the muted role once all roles were removed successfully
  78.     person.roles.set([]).then(member => member.roles.add(muterole)).catch(console.error);
  79.  
  80.     // gives person role.
  81.     person.roles.add(muterole);
  82.     message.channel.send(`@${person.id} has been muted for ${ms(ms(time))}`);
  83.     console.log('Successful!');
  84.  
  85.     setTimeout(function(){
  86.       person.roles.set(cachedUserRoles[person.id]).catch(console.error)
  87.       message.channel.send(`<@${person.id}> is no longer muted! Welcome Back!!`);
  88.       console.log('Automatically Unmuted! YOUR COMMAND WORKS!');
  89.     }, ms(time));
  90.  
  91.  
  92.   },
  93. };
  94.  
RAW Paste Data