{"size": 4232, "ext": "js", "max_stars_repo_path": "commands/mod/mute.js", "max_stars_repo_name": "Xladen18/LoneBot", "max_stars_repo_stars_event_min_datetime": "2020-10-16T16:08:10.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-16T16:08:10.000Z", "max_issues_repo_path": "commands/mod/mute.js", "max_issues_repo_name": "VojvodaSeselj/LoneBot", "max_issues_repo_issues_event_min_datetime": "2021-05-10T07:50:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-22T08:38:30.000Z", "max_forks_repo_path": "commands/mod/mute.js", "max_forks_repo_name": "VojvodaSeselj/LoneBot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 39.5514018692, "max_line_length": 171, "alphanum_fraction": 0.5907372401}
const { RichEmbed } = require("discord.js");
const { promptMessage, getMember } = require("../../functions.js");
const { stripIndents } = require("common-tags");
const ms = require("ms");
const Mute = require("../../models/mute.js");
const Guild = require("../../models/guild.js");

module.exports = {
    name: "mute",
    aliases: [],
    category: "Moderation",
    description: "Mute member in chat or voice.",
    usage: "Mute <User> <Chat/Voice> [Reason] [Time]",
    cooldown: 5,
    run: async (bot, message, args) => {
      let guild = await Guild.findOne({
        Guild: message.guild.id
      });
      if(message.deletable) message.delete()

      if (!message.member.roles.some(r=>guild.ModeratorRole.concat(guild.AdminRole).includes(r.name)) && !message.member.hasPermission("ADMINISTRATOR")) {
        return message.reply("You do not have required permission to use this command!").then(m => m.delete(5000));
      }
      if (!message.guild.me.hasPermission("MUTE_MEMBERS")) {
        return message.reply("I do not have permission to mute members.").then(m => m.delete(5000));
      }
      if (!args[0]) {
        return message.reply("You need to provide a member to mute!").then(m => m.delete(5000));
      }
      const toMute = await getMember(message, args[0]);
      if (!toMute) {
        return message.reply("Couldn't find that member!").then(m => m.delete(5000))
      }
      if (toMute === message.author.id) {
        return message.reply("You can't mute yourself!");
      }
      const reason = args.slice(3).join(" ");
      let muteTime = ms(args[2]);
      let muteLenght = muteTime / 1000 + " seconds";
      if(isNaN(muteTime)) {
        muteLenght = "Permanently muted!"
        reason = args.slice(2).join(" ");
        muteTime = Number.MAX_SAFE_INTEGER
      }
      if(!reason) reason = "No reason provided!";
      const logsChannel = message.guild.channels.find(channel => channel.name === guild.LogsChannel);
      if (!logsChannel) return message.reply("Couldn't find logs channel.");

      const type = args[1];
      if (!type) {
        return message.reply("You must supply type of mute! Either Chat or Voice! Caps sensitive!").then(m => m.delete(5000));
      }
      if (type !== "Chat" && type !== "Voice") {
        return message.reply("Type must be Chat or Voice!").then(m => m.delete(5000));
      }
      const chatMute = message.guild.roles.find(role => role.name === "Chat Muted") || message.guild.createRole({ name:"Chat Muted", color:"#27272b", permissions:[] });
      const voiceMute = message.guild.roles.find(role => role.name === "Voice Muted") || message.guild.createRole({ name:"Voice Muted", color:"#27272b", permissions:[] });
      if (type === "Chat") {
        toMute.addRole(chatMute);
    		message.guild.channels.forEach((channel) => {
    			channel.overwritePermissions(chatMute, {
    				SEND_MESSAGES: false,
    			});
    		});
      }
      else if (type === "Voice") {
        toMute.addRole(voiceMute);
        message.guild.channels.forEach((channel) => {
    			channel.overwritePermissions(voiceMute, {
    				SPEAK: false,
    			});
    		});
      }
  		const mute = new Mute({
  			Guild: message.guild.id,
  			MutedUser: {
          Username: toMute.user.username,
          ID: toMute.id,
        },
        MutedBy: {
          Username: message.author.username,
          ID: message.member.id,
        },
        Type: type,
        Time: muteTime,
        Created: Date.now(),
        Reason: reason || "No reason provided"
  		});
  		const embed = new RichEmbed()
        .setColor("#cf0808")
        .setThumbnail(toMute.user.displayAvatarURL)
        .setFooter(message.author.username, message.author.displayAvatarURL)
        .setDescription(stripIndents`**Mute**
        **Muted User** ${toMute.user} **with ID** ${toMute.user.id}
        **Muted By** ${message.member} **with ID** ${message.member.id}
        **Muted At** ${message.createdAt}
        **Mute Type** ${type}
        **Mute Lenght** ${muteLenght}
        **Reason** ${reason ? reason : "No reason provided!"}`);
  		logsChannel.send(embed);

      mute.save()
        .then(result => console.log(result))
        .catch(err => console.log(err));
  }
}
