import { AnyInteraction, ApplicationCommandType, InteractionType, } from "discord.js" import i18next from "i18next" import { logger } from "../logger" export const interactionCreateEvent = async (interaction: AnyInteraction) => { if ( interaction.type !== InteractionType.ApplicationCommand || !interaction.inCachedGuild() ) { return } // Using dynamic import to make sure translations are available const commands = await import("../commands") const command = Object.values(commands).find( (command) => command.name === interaction.commandName ) if (!command) { return } const isSlashCommand = interaction.type === InteractionType.ApplicationCommand && command.type === ApplicationCommandType.ChatInput const isContextMenuCommand = interaction.isContextMenuCommand() && (command.type === ApplicationCommandType.Message || ApplicationCommandType.User) try { if (isSlashCommand) { if (command.conditions) { const conditionsResults = await Promise.all( command.conditions.map((condition) => condition(interaction)) ) const allConditionsMet = conditionsResults.every( (condition) => condition === true ) if (allConditionsMet) { await command.execute(interaction, interaction.locale) } else { await interaction.reply( i18next.t("common.condition_failed", { lng: interaction.locale, }) ) } } else { await command.execute(interaction, interaction.locale) } } else if (isContextMenuCommand) { await command.execute(interaction, interaction.locale) } } catch (error) { if (error instanceof Error) { logger.error("Command failed: %s", command.name) logger.error(error, error.message) } } }