From be329d709f7de21fee496fce47d829099ef6f7ce Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 29 Nov 2021 11:46:16 +0000 Subject: [PATCH] Migrate clear command --- commands/clear.js | 58 -------------------------------------- src/commands/clear.ts | 36 +++++++++++++++++++++++ src/helpers/LogEmbed.ts | 2 +- src/helpers/PublicEmbed.ts | 2 +- 4 files changed, 38 insertions(+), 60 deletions(-) delete mode 100644 commands/clear.js create mode 100644 src/commands/clear.ts diff --git a/commands/clear.js b/commands/clear.js deleted file mode 100644 index d84a288..0000000 --- a/commands/clear.js +++ /dev/null @@ -1,58 +0,0 @@ -// Required components -const { command } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -const embedColor = "0x3050ba"; - -// Command Class -class clear extends command { - constructor() { - // Set execute method, description, category, and usage - super("clear"); - super.description = "Bulk deletes the chat for up to 100 messages"; - super.category = "Moderation"; - super.usage = ""; - - // Set required configs in the config.clear json string - super.requiredConfigs = "modrole"; - super.requiredConfigs = "logchannel"; - } - - // Execute method - clear(context) { - // If the user has the config.clear.modrole role - if (context.message.member.roles.cache.find(role => role.name == context.client.config.clear.modrole)) { - // If the command specifies a number between 1 and 100 - if (context.arguments.length > 0 && context.arguments[0] > 0 && context.arguments[0] < 101) { - // Attempt to bulk delete the amount of messages specified as an argument - context.message.channel.bulkDelete(context.arguments[0]).then(() => { - // Public embed - const embed = new MessageEmbed() - .setColor(embedColor) - .setDescription(`${context.arguments[0]} messages were removed`); - - // Send the embed into the channel the command was sent in - context.message.channel.send(embed); - }).catch(err => { // If the bot couldn't bulk delete - errorEmbed(context, "An error has occurred"); - console.log(err); - }); - } else { // If the user didn't give a number valid (between 1 and 100) - errorEmbed(context, "Please specify an amount between 1 and 100"); - } - } else { // If the user doesn't have the mod role - errorEmbed(context, `This command requires the \`${context.client.config.clear.modrole}\` role to run`); - } - } -} - -// Function to send an error embed -function errorEmbed(context, message) { - const embed = new MessageEmbed() - .setColor(embedColor) - .setDescription(message); - - context.message.channel.send(embed); -} - -module.exports = clear; diff --git a/src/commands/clear.ts b/src/commands/clear.ts new file mode 100644 index 0000000..14015d4 --- /dev/null +++ b/src/commands/clear.ts @@ -0,0 +1,36 @@ +import { Command, ICommandContext } from "vylbot-core"; +import ErrorEmbed from "../helpers/ErrorEmbed"; +import { TextChannel } from "discord.js"; +import PublicEmbed from "../helpers/PublicEmbed"; + +export default class Clear extends Command { + constructor() { + super(); + + super._category = "Moderation"; + super._roles = [ + process.env.ROLES_MODERATOR! + ]; + } + + public override async execute(context: ICommandContext) { + if (context.args.length == 0) { + const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); + errorEmbed.SendToCurrentChannel(); + return; + } + + const totalToClear = Number.parseInt(context.args[0]); + + if (!totalToClear || totalToClear <= 0 || totalToClear > 100) { + const errorEmbed = new ErrorEmbed(context, "Please specify an amount between 1 and 100"); + errorEmbed.SendToCurrentChannel(); + return; + } + + await (context.message.channel as TextChannel).bulkDelete(totalToClear); + + const embed = new PublicEmbed(context, "", `${totalToClear} message(s) were removed`); + embed.SendToCurrentChannel(); + } +} \ No newline at end of file diff --git a/src/helpers/LogEmbed.ts b/src/helpers/LogEmbed.ts index 5a801d1..4eff943 100644 --- a/src/helpers/LogEmbed.ts +++ b/src/helpers/LogEmbed.ts @@ -9,7 +9,7 @@ export default class LogEmbed extends MessageEmbed { constructor(context: ICommandContext, title: string) { super(); - super.setColor(process.env.ERROR_EMBED!); + super.setColor(process.env.EMBED_COLOUR!); super.setTitle(title); this._context = context; diff --git a/src/helpers/PublicEmbed.ts b/src/helpers/PublicEmbed.ts index 75d99fe..790f27f 100644 --- a/src/helpers/PublicEmbed.ts +++ b/src/helpers/PublicEmbed.ts @@ -7,7 +7,7 @@ export default class PublicEmbed extends MessageEmbed { constructor(context: ICommandContext, title: string, description: string) { super(); - super.setColor(process.env.ERROR_EMBED!); + super.setColor(process.env.EMBED_COLOUR!); super.setTitle(title); super.setDescription(description);