diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index ed61724..3d19fd0 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -1,68 +1,20 @@ -import { Events, MessageFlags } from "discord.js"; -import { AuroraClient } from "@/lib/BotClient"; -import { userService } from "@/modules/user/user.service"; -import { createErrorEmbed } from "@lib/embeds"; +import { Events } from "discord.js"; +import { ComponentInteractionHandler, AutocompleteHandler, CommandHandler } from "@/lib/handlers"; import type { Event } from "@lib/types"; const event: Event = { name: Events.InteractionCreate, execute: async (interaction) => { - // Handle Trade Interactions if (interaction.isButton() || interaction.isStringSelectMenu() || interaction.isModalSubmit()) { - const { interactionRoutes } = await import("@lib/interaction.routes"); - - for (const route of interactionRoutes) { - if (route.predicate(interaction)) { - const module = await route.handler(); - const handlerMethod = module[route.method]; - if (typeof handlerMethod === 'function') { - await handlerMethod(interaction); - return; - } else { - console.error(`Handler method ${route.method} not found in module`); - } - } - } + return ComponentInteractionHandler.handle(interaction); } if (interaction.isAutocomplete()) { - const command = AuroraClient.commands.get(interaction.commandName); - if (!command || !command.autocomplete) return; - try { - await command.autocomplete(interaction); - } catch (error) { - console.error(`Error handling autocomplete for ${interaction.commandName}:`, error); - } - return; + return AutocompleteHandler.handle(interaction); } - if (!interaction.isChatInputCommand()) return; - - const command = AuroraClient.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; - } - - - // Ensure user exists in database - try { - await userService.getOrCreateUser(interaction.user.id, interaction.user.username); - } catch (error) { - console.error("Failed to ensure user exists:", error); - } - - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - const errorEmbed = createErrorEmbed('There was an error while executing this command!'); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral }); - } else { - await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral }); - } + if (interaction.isChatInputCommand()) { + return CommandHandler.handle(interaction); } }, }; diff --git a/src/lib/handlers/AutocompleteHandler.ts b/src/lib/handlers/AutocompleteHandler.ts new file mode 100644 index 0000000..ce56285 --- /dev/null +++ b/src/lib/handlers/AutocompleteHandler.ts @@ -0,0 +1,21 @@ +import { AutocompleteInteraction } from "discord.js"; +import { AuroraClient } from "@/lib/BotClient"; + +/** + * Handles autocomplete interactions for slash commands + */ +export class AutocompleteHandler { + static async handle(interaction: AutocompleteInteraction): Promise { + const command = AuroraClient.commands.get(interaction.commandName); + + if (!command || !command.autocomplete) { + return; + } + + try { + await command.autocomplete(interaction); + } catch (error) { + console.error(`Error handling autocomplete for ${interaction.commandName}:`, error); + } + } +} diff --git a/src/lib/handlers/CommandHandler.ts b/src/lib/handlers/CommandHandler.ts new file mode 100644 index 0000000..e4fca44 --- /dev/null +++ b/src/lib/handlers/CommandHandler.ts @@ -0,0 +1,39 @@ +import { ChatInputCommandInteraction, MessageFlags } from "discord.js"; +import { AuroraClient } from "@/lib/BotClient"; +import { userService } from "@/modules/user/user.service"; +import { createErrorEmbed } from "@lib/embeds"; + +/** + * Handles slash command execution + * Includes user validation and comprehensive error handling + */ +export class CommandHandler { + static async handle(interaction: ChatInputCommandInteraction): Promise { + const command = AuroraClient.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + // Ensure user exists in database + try { + await userService.getOrCreateUser(interaction.user.id, interaction.user.username); + } catch (error) { + console.error("Failed to ensure user exists:", error); + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + const errorEmbed = createErrorEmbed('There was an error while executing this command!'); + + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral }); + } else { + await interaction.reply({ embeds: [errorEmbed], flags: MessageFlags.Ephemeral }); + } + } + } +} diff --git a/src/lib/handlers/ComponentInteractionHandler.ts b/src/lib/handlers/ComponentInteractionHandler.ts new file mode 100644 index 0000000..b317df1 --- /dev/null +++ b/src/lib/handlers/ComponentInteractionHandler.ts @@ -0,0 +1,27 @@ +import { ButtonInteraction, StringSelectMenuInteraction, ModalSubmitInteraction } from "discord.js"; + +type ComponentInteraction = ButtonInteraction | StringSelectMenuInteraction | ModalSubmitInteraction; + +/** + * Handles component interactions (buttons, select menus, modals) + * Routes to appropriate handlers based on customId patterns + */ +export class ComponentInteractionHandler { + static async handle(interaction: ComponentInteraction): Promise { + const { interactionRoutes } = await import("@lib/interaction.routes"); + + for (const route of interactionRoutes) { + if (route.predicate(interaction)) { + const module = await route.handler(); + const handlerMethod = module[route.method]; + + if (typeof handlerMethod === 'function') { + await handlerMethod(interaction); + return; + } else { + console.error(`Handler method ${route.method} not found in module`); + } + } + } + } +} diff --git a/src/lib/handlers/index.ts b/src/lib/handlers/index.ts new file mode 100644 index 0000000..fbaa946 --- /dev/null +++ b/src/lib/handlers/index.ts @@ -0,0 +1,3 @@ +export { ComponentInteractionHandler } from "./ComponentInteractionHandler"; +export { AutocompleteHandler } from "./AutocompleteHandler"; +export { CommandHandler } from "./CommandHandler";