const { Events, MessageFlags } = require("discord.js"); const { guardSessionInteraction } = require("../utils/interactionSession"); require("dotenv").config(); module.exports = { name: Events.InteractionCreate, once: false, async execute(interaction) { if (interaction.isAutocomplete()) { return autocompleteHandler(interaction); } if (interaction.isChatInputCommand()) { return commandHandler(interaction); } if (interaction.isButton()) { return buttonHandler(interaction); } if ( interaction.isMentionableSelectMenu() || interaction.isUserSelectMenu() ) { return selectMenuHandler(interaction); } if (interaction.isModalSubmit()) { return modalHandler(interaction); } } }; async function autocompleteHandler(interaction) { const command = interaction.client.commands.get( interaction.commandName ); if (!command) { return console.error( `No command matching ${interaction.commandName} was found.` ); } if (typeof command.autocomplete !== "function") { return; } try { await command.autocomplete(interaction); } catch (error) { console.error( `Autocomplete error for /${interaction.commandName}:`, error ); try { await interaction.respond([]); } catch {} } } async function commandHandler(interaction) { const command = interaction.client.commands.get( interaction.commandName ); if (!command) { return console.error( `No command matching ${interaction.commandName} was found.` ); } try { await command.execute(interaction); } catch (error) { console.error(error); await sendInteractionError( interaction, "There was an error while executing this command!" ); } } async function buttonHandler(interaction) { const allowed = await guardSessionInteraction(interaction); if (!allowed) { return; } const button = interaction.client.buttons.get( interaction.customId ); if (!button) { return console.error( `No button registered matching ${interaction.customId}.` ); } try { await button.execute(interaction); } catch (error) { console.error(error); await sendInteractionError( interaction, "There was an error while executing this button!" ); } } async function selectMenuHandler(interaction) { const selectMenu = interaction.client.selectMenus.get( interaction.customId ); if (!selectMenu) { return console.error( `No Select Menu registered matching ${interaction.customId}.` ); } try { await selectMenu.execute(interaction); } catch (error) { console.error(error); await sendInteractionError( interaction, "There was an error while executing this select menu!" ); } } async function modalHandler(interaction) { const customId = interaction.customId.split(":")[0]; const modal = interaction.client.modals.get(customId); if (!modal) { return console.error( `No Modal registered matching ${interaction.customId}.` ); } try { await modal.execute(interaction); } catch (error) { console.error(error); await sendInteractionError( interaction, "There was an error while executing this modal!" ); } } async function sendInteractionError(interaction, content) { const payload = { content, flags: MessageFlags.Ephemeral }; try { if (interaction.replied || interaction.deferred) { await interaction.followUp(payload); } else { await interaction.reply(payload); } } catch (error) { console.error("Failed to send interaction error response:", error); } }