From 4ff88d0694107baeeb3679ebc3ab3965b3aba286 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 2 Dec 2021 12:45:25 +0000 Subject: [PATCH] Migrate unmute command --- commands/unmute.js | 98 ------------------------------------------ src/commands/unmute.ts | 70 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 98 deletions(-) delete mode 100644 commands/unmute.js create mode 100644 src/commands/unmute.ts diff --git a/commands/unmute.js b/commands/unmute.js deleted file mode 100644 index 24b7528..0000000 --- a/commands/unmute.js +++ /dev/null @@ -1,98 +0,0 @@ -// Required components -const { command } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -const embedColor = "0x3050ba"; - -// Command Class -class unmute extends command { - constructor() { - // Set run method, description, category, usage - super("unmute"); - super.description = "Unmutes the mentioned user with an optional reason"; - super.category = "Moderation"; - super.usage = "<@user> [reason]"; - - // Set required configs - super.requiredConfigs = "modrole"; - super.requiredConfigs = "logchannel"; - super.requiredConfigs = "muterole"; - } - - // The command's run method - unmute(context) { - // Check if the user has the mod role - if (context.message.member.roles.cache.find(role => role.name == context.client.config.mute.modrole)) { - // Get the user first pinged in the message - const user = context.message.mentions.users.first(); - - // If the user object exists - if (user) { - // Get the guild member object from the pinged user - const member = context.message.guild.member(user); - - // If the member object exists, i.e. if the user is in the server - if (member) { - // Get the part of the argument array which contains the reason - const reasonArgs = context.arguments; - reasonArgs.splice(0, 1); - - // Join the array into a string - const reason = reasonArgs.join(" "); - - // If the server is available - if (context.message.guild.available) { - // If the bot client can manage the user - if (member.manageable) { - // The embed to go into the bot log - const embedLog = new MessageEmbed() - .setColor(embedColor) - .setTitle("Member Unmuted") - .addField("User", `${user} \`${user.tag}\``, true) - .addField("Moderator", `${context.message.author} \`${context.message.author.tag}\``, true) - .addField("Reason", reason || "*none*") - .setThumbnail(user.displayAvatarURL); - - // The embed to go into the channel the command was sent in - const embedPublic = new MessageEmbed() - .setColor(embedColor) - .setDescription(`${user} has been unmuted`) - .addField("Reason", reason || "*none*"); - - // Get the muted role - const mutedRole = context.message.guild.roles.cache.find(role => role.name == context.client.config.unmute.muterole); - - // Attempt to remove the role from the user, and then send the embeds. If unsuccessful log the error - member.roles.remove(mutedRole, reason).then(() => { - context.message.guild.channels.cache.find(channel => channel.name == context.client.config.unmute.logchannel).send(embedLog); - context.message.channel.send(embedPublic); - - context.message.delete(); - }).catch(err => { - errorEmbed(context, "An error occurred"); - console.log(err); - }); - } else { // If the bot can't manage the user - errorEmbed(context, "I am unable to unmute this user"); - } - } - } else { // If the member object doesn't exist - errorEmbed(context, "Please specify a valid user"); - } - } else { // If the user object doesn't exist - errorEmbed(context, "Please specify a valid user"); - } - } - } -} - -// Send an embed in case of an error -function errorEmbed(context, message) { - const embed = new MessageEmbed() - .setColor(embedColor) - .setDescription(message); - - context.message.channel.send(embed); -} - -module.exports = unmute; diff --git a/src/commands/unmute.ts b/src/commands/unmute.ts new file mode 100644 index 0000000..07fb03a --- /dev/null +++ b/src/commands/unmute.ts @@ -0,0 +1,70 @@ +import { Command, ICommandContext } from "vylbot-core"; +import ErrorMessages from "../constants/ErrorMessages"; +import ErrorEmbed from "../helpers/ErrorEmbed"; +import LogEmbed from "../helpers/LogEmbed"; +import PublicEmbed from "../helpers/PublicEmbed"; + +export default class Unmute extends Command { + constructor() { + super(); + + super._category = "Moderation"; + super._roles = [ + process.env.ROLES_MODERATOR! + ]; + } + + public override async execute(context: ICommandContext) { + const targetUser = context.message.mentions.users.first(); + + if (!targetUser) { + const embed = new ErrorEmbed(context, "User does not exist"); + embed.SendToCurrentChannel(); + return; + } + + const targetMember = context.message.guild?.member(targetUser); + + if (!targetMember) { + const embed = new ErrorEmbed(context, "User is not in this server"); + embed.SendToCurrentChannel(); + return; + } + + const reasonArgs = context.args; + reasonArgs.splice(0, 1); + + const reason = reasonArgs.join(" "); + + if (!context.message.guild?.available) { + return; + } + + if (!targetMember.manageable) { + const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); + embed.SendToCurrentChannel(); + return; + } + + const logEmbed = new LogEmbed(context, "Member Unmuted"); + logEmbed.AddUser("User", targetUser, true) + logEmbed.AddUser("Moderator", context.message.author); + logEmbed.AddReason(reason); + + const publicEmbed = new PublicEmbed(context, "", `${targetUser} has been unmuted`); + publicEmbed.AddReason(reason); + + const mutedRole = context.message.guild.roles.cache.find(role => role.name == process.env.ROLES_MUTED); + + if (!mutedRole) { + const embed = new ErrorEmbed(context, ErrorMessages.RoleNotFound); + embed.SendToCurrentChannel(); + return; + } + + await targetMember.roles.remove(mutedRole, reason); + + logEmbed.SendToModLogsChannel(); + publicEmbed.SendToCurrentChannel(); + } +} \ No newline at end of file