From bc6ef0c117bdf9efb0944664795c243b558428cc Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 13 Apr 2022 12:23:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20interactionCreate=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/events/interactionCreate/index.ts | 58 ++--------------------- src/events/interactionCreate/isCommand.ts | 53 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 src/events/interactionCreate/isCommand.ts diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index ed09f93..33b006b 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -1,59 +1,11 @@ -import config from "../../../config.json"; -import logger from "../../logger"; -import guilds from "../../database/schemas/guild"; +// Dependencies +import { CommandInteraction } from "discord.js"; -import { Interaction, ColorResolvable } from "discord.js"; +import isCommand from "./isCommand"; export default { name: "interactionCreate", - async execute(interaction: Interaction) { - // Destructure member, client - const { client, guild } = interaction; - - // If interaction is command - if (interaction.isCommand()) { - // Get command from collection - const command = client.commands.get(interaction.commandName); - - // If command do not exist - if (!command) return; - - // Create guild if it does not exist already - await guilds.findOne({ guildId: guild?.id }, { new: true, upsert: true }); - - // Defer reply - await interaction.deferReply({ ephemeral: true }); - - try { - // Execute command - await command.execute(interaction); - - const { commandName, user } = interaction; - - return logger?.verbose( - `Guild: ${guild?.id} User: ${user?.tag} executed ${commandName}` - ); - } catch (e) { - // Send debug message - logger.error(e); - - // Send interaction reply - await interaction.editReply({ - embeds: [ - { - author: { - name: client?.user?.username, - icon_url: client?.user?.displayAvatarURL(), - url: "https://bot.zyner.org/", - }, - title: "Error", - description: "There was an error while executing this command!", - color: config.colors.error as ColorResolvable, - timestamp: new Date(), - }, - ], - }); - } - } + async execute(interaction: CommandInteraction) { + await isCommand(interaction); }, }; diff --git a/src/events/interactionCreate/isCommand.ts b/src/events/interactionCreate/isCommand.ts new file mode 100644 index 0000000..d5a3837 --- /dev/null +++ b/src/events/interactionCreate/isCommand.ts @@ -0,0 +1,53 @@ +import { ColorResolvable, CommandInteraction } from "discord.js"; +import config from "../../../config.json"; +import logger from "../../logger"; +import guilds from "../../database/schemas/guild"; + +export default async (interaction: CommandInteraction) => { + if (!interaction.isCommand()) return; + + const { client, guild } = interaction; + + // Get command from collection + const command = client.commands.get(interaction.commandName); + + // If command do not exist + if (!command) return; + + // Create guild if it does not exist already + await guilds.findOne({ guildId: guild?.id }, { new: true, upsert: true }); + + // Defer reply + await interaction.deferReply({ ephemeral: true }); + + try { + // Execute command + await command.execute(interaction); + + const { commandName, user } = interaction; + + return logger?.verbose( + `Guild: ${guild?.id} User: ${user?.tag} executed ${commandName}` + ); + } catch (e) { + // Send debug message + logger.error(e); + + // Send interaction reply + await interaction.editReply({ + embeds: [ + { + author: { + name: client?.user?.username, + icon_url: client?.user?.displayAvatarURL(), + url: "https://bot.zyner.org/", + }, + title: "Error", + description: "There was an error while executing this command!", + color: config.colors.error as ColorResolvable, + timestamp: new Date(), + }, + ], + }); + } +};