From 0d3134bf4541e7e888e4007d72edc9c4adb80894 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 29 Nov 2021 15:06:13 +0000 Subject: [PATCH] Migrate mute command --- .env.template | 2 + commands/mute.js | 98 ---------------------------------- src/commands/mute.ts | 70 ++++++++++++++++++++++++ src/constants/ErrorMessages.ts | 1 + src/helpers/PublicEmbed.ts | 5 ++ src/vylbot.ts | 1 + 6 files changed, 79 insertions(+), 98 deletions(-) delete mode 100644 commands/mute.js create mode 100644 src/commands/mute.ts diff --git a/.env.template b/.env.template index bfbe4a3..6320a30 100644 --- a/.env.template +++ b/.env.template @@ -20,6 +20,8 @@ FOLDERS_EVENTS=src/events COMMANDS_DISABLED= COMMANDS_DISABLED_MESSAGE=This command is disabled. +COMMANDS_MUTE_ROLE=Muted + EMBED_COLOUR=0x3050ba EMBED_COLOUR_ERROR=0xD52803 diff --git a/commands/mute.js b/commands/mute.js deleted file mode 100644 index 3b1fcd1..0000000 --- a/commands/mute.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 mute extends command { - constructor() { - // Set the command's run method, description, category, and usage - super("mute"); - super.description = "Mutes the mentioned user with an optional reason"; - super.category = "Moderation"; - super.usage = "<@user> [reason]"; - - // Set the required configs for the command - super.requiredConfigs = "modrole"; - super.requiredConfigs = "logchannel"; - super.requiredConfigs = "muterole"; - } - - // The command's run method - mute(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 of the mentioned 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 arguments array which contains the reason - const reasonArgs = context.arguments; - reasonArgs.splice(0, 1); - - // Join the reason 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's roles - if (member.manageable) { - // The embed to go into the bot log - const embedLog = new MessageEmbed() - .setTitle("Member Muted") - .setColor(embedColor) - .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 muted`) - .addField("Reason", reason || "*none*"); - - // Get the 'Muted' role - const mutedRole = context.message.guild.roles.cache.find(role => role.name == context.client.config.mute.muterole); - - // Attempt to mute the user, if successful send the embeds, if not log the error - member.roles.add(mutedRole, reason).then(() => { - context.message.guild.channels.cache.find(channel => channel.name == context.client.config.mute.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 mute 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 when an error occurs -function errorEmbed(context, message) { - const embed = new MessageEmbed() - .setColor(embedColor) - .setDescription(message); - - context.message.channel.send(embed); -} - -module.exports = mute; diff --git a/src/commands/mute.ts b/src/commands/mute.ts new file mode 100644 index 0000000..b1fe8bb --- /dev/null +++ b/src/commands/mute.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 Mute 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 Muted"); + logEmbed.AddUser("User", targetUser, true) + logEmbed.AddUser("Moderator", context.message.author); + logEmbed.AddReason(reason); + + const publicEmbed = new PublicEmbed(context, "", `${targetUser} has been muted`); + publicEmbed.AddReason(reason); + + const mutedRole = context.message.guild.roles.cache.find(role => role.name == process.env.COMMANDS_MUTE_ROLE); + + if (!mutedRole) { + const embed = new ErrorEmbed(context, ErrorMessages.RoleNotFound); + embed.SendToCurrentChannel(); + return; + } + + await targetMember.roles.add(mutedRole, reason); + + logEmbed.SendToModLogsChannel(); + publicEmbed.SendToCurrentChannel(); + } +} \ No newline at end of file diff --git a/src/constants/ErrorMessages.ts b/src/constants/ErrorMessages.ts index d3c1fe1..c2c9434 100644 --- a/src/constants/ErrorMessages.ts +++ b/src/constants/ErrorMessages.ts @@ -1,4 +1,5 @@ export default class ErrorMessages { public static readonly InsufficientBotPermissions = "Unable to do this action, am I missing permissions?"; public static readonly CantFindChannel = "Unable to find channel"; + public static readonly RoleNotFound = "Unable to find role"; } \ No newline at end of file diff --git a/src/helpers/PublicEmbed.ts b/src/helpers/PublicEmbed.ts index 790f27f..da278e5 100644 --- a/src/helpers/PublicEmbed.ts +++ b/src/helpers/PublicEmbed.ts @@ -14,6 +14,11 @@ export default class PublicEmbed extends MessageEmbed { this._context = context; } + // Detail methods + public AddReason(message: String) { + super.addField("Reason", message || "*none*"); + } + // Send methods public SendToCurrentChannel() { this._context.message.channel.send(this); diff --git a/src/vylbot.ts b/src/vylbot.ts index c9b0eee..ed42734 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -12,6 +12,7 @@ if (!process.env.ROLES_MODERATOR) throw "ROLES_MODERATOR is required in .env"; if (!process.env.CHANNELS_LOGS_MESSAGE) throw "CHANNELS_LOGS_MESSAGE is required in .env"; if (!process.env.CHANNELS_LOGS_MEMBER) throw "CHANNELS_LOGS_MEMBER is required in .env"; if (!process.env.CHANNELS_LOGS_MOD) throw "CHANNELS_LOGS_MOD is required in .env"; +if (!process.env.COMMANDS_MUTE_ROLE) throw "COMMANDS_MUTE_ROLE is required in .env"; const client = new CoreClient(); client.start(); \ No newline at end of file