From 019966f25fb63c5ebed4379bd185b36deb93c0da Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 29 Nov 2021 11:51:54 +0000 Subject: [PATCH] Migrate kick command --- commands/kick.js | 93 -------------------------------------------- src/commands/kick.ts | 61 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 93 deletions(-) delete mode 100644 commands/kick.js create mode 100644 src/commands/kick.ts diff --git a/commands/kick.js b/commands/kick.js deleted file mode 100644 index 4878270..0000000 --- a/commands/kick.js +++ /dev/null @@ -1,93 +0,0 @@ -// Required Components -const { command } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); - -const embedColor = "0x3050ba"; - -// Command Class -class kick extends command { - constructor() { - // Sets the command's run method, description, category, and usage - super("kick"); - super.description = "Kicks the mentioned user with an optional reason"; - super.category = "Moderation"; - super.usage = "<@user> [reason]"; - - // Sets the required configs for the command - super.requiredConfigs = "modrole"; - super.requiredConfigs = "logchannel"; - } - - // The command's run method - kick(context) { - // Checks if the user has the mod role, set in the config json string - if (context.message.member.roles.cache.find(role => role.name == context.client.config.kick.modrole)) { - // Gets the first user pinged in the command - const user = context.message.mentions.users.first(); - - // If a user was pinged - if (user) { - // Gets the guild member object of 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) { - // Gets the part of the argument array which holds the reason - const reasonArgs = context.arguments; - reasonArgs.splice(0, 1); - - // Joins the reason into a string - const reason = reasonArgs.join(" "); - - // If the server is available - if (context.message.guild.available) { - // If the bot client can kick the mentioned member - if (member.kickable) { - // The embed to go into the bot log - const embedLog = new MessageEmbed() - .setTitle("Member Kicked") - .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 channel the command was sent in - const embedPublic = new MessageEmbed() - .setColor(embedColor) - .setDescription(`${user} has been kicked`); - - // Attemtp to kick the user, if successful send the embeds, if unsuccessful notify the chat and log the error - member.kick({ reason: reason }).then(() => { - context.message.guild.channels.cache.find(channel => channel.name == context.client.config.kick.logchannel).send(embedLog); - context.message.channel.send(embedPublic); - - context.message.delete(); - }).catch(err => { - errorEmbed(context, "An error has occurred"); - console.log(err); - }); - } else { // If the user isn't kickable - errorEmbed(context, "I am unable to kick this user"); - } - } - } else { // If the member object is invalid - errorEmbed(context, "Please specify a valid user"); - } - } else { // If the user object is invalid - errorEmbed(context, "Please specify a valid user"); - } - } - } -} - -// Function to post 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 = kick; diff --git a/src/commands/kick.ts b/src/commands/kick.ts new file mode 100644 index 0000000..1d9123f --- /dev/null +++ b/src/commands/kick.ts @@ -0,0 +1,61 @@ +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 Kick 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.kickable) { + const embed = new ErrorEmbed(context, ErrorMessages.InsufficientBotPermissions); + embed.SendToCurrentChannel(); + return; + } + + const logEmbed = new LogEmbed(context, "Member Kicked"); + logEmbed.AddUser("User", targetUser, true); + logEmbed.AddUser("Moderator", context.message.author); + logEmbed.AddReason(reason); + + const publicEmbed = new PublicEmbed(context, "", `${targetUser} has been kicked`); + + await targetMember.kick(reason); + + logEmbed.SendToModLogsChannel(); + publicEmbed.SendToCurrentChannel(); + } +} \ No newline at end of file