UNPKG

artibot

Version:

Modern, fast and modular open-source Discord bot

70 lines 2.58 kB
import log from "../logger.js"; import { MessageContextMenuOption, UserContextMenuOption } from "../modules.js"; export const name = "interactionCreate"; /** Context interaction listener */ export async function execute(interaction, artibot) { // Checks if the interaction is a button interaction if (!interaction.isContextMenuCommand()) return; const { localizer, modules } = artibot; // Don't execute interactions in DM channels if (!interaction.channel) { await interaction.reply({ content: localizer._("This is disabled in DM channels."), ephemeral: true }); return; } if (interaction.isUserContextMenuCommand()) { const command = findUserCommand(interaction.commandName, modules); if (!command) return; // A try to execute the interaction. try { await command.execute(interaction, artibot); } catch (err) { log("ContextMenuHandler", err.message, "warn", true); await interaction.reply({ content: localizer._("An error occured when executing this interaction..."), ephemeral: true, }); } } else if (interaction.isMessageContextMenuCommand()) { const command = findMessageCommand(interaction.commandName, modules); if (!command) return; // A try to execute the interaction. try { await command.execute(interaction, artibot); } catch (err) { log("ContextMenuHandler", err.message, "warn", true); await interaction.reply({ content: localizer._("An error occured when executing this interaction..."), ephemeral: true, }); } } else { // Almost impossible, but we are still catching the bug. log("InteractionManager", localizer._("Something weird happened with the menu. Received an unknown menu type."), "warn", true); } } function findUserCommand(name, modules) { for (const [, { parts }] of modules) { for (const part of parts) { if ((part instanceof UserContextMenuOption) && part.data.name == name) return part; } } } function findMessageCommand(name, modules) { for (const [, { parts }] of modules) { for (const part of parts) { if ((part instanceof MessageContextMenuOption) && part.data.name == name) return part; } } } //# sourceMappingURL=contextMenuHandler.js.map