diff --git a/src/buttons/loader.ts b/src/buttons/loader.ts index 521263c..1a54878 100644 --- a/src/buttons/loader.ts +++ b/src/buttons/loader.ts @@ -1,6 +1,7 @@ import { readdir } from 'fs/promises'; import { removeExtension } from '../utils/misc'; -import { Client } from 'discord.js'; +import { ChatInputCommandInteraction, Client, MessageComponentInteraction } from 'discord.js'; +import { getLocale } from '../utils/locales'; export default async (client: Client) => { // Dossier des buttons @@ -34,3 +35,33 @@ export default async (client: Client) => { }), ); }; + +/** + * Collect interactions for buttons + * @param client Client + * @param interaction Chat interaction + * @param id Button ID + * @param deferUpdate defer update in case update take time + */ +export const collect = (client: Client, interaction: ChatInputCommandInteraction, id: string, deferUpdate = false) => { + const loc = getLocale(client, interaction.locale); + const button = client.buttons.list.get(id); + + if (!button) { + interaction.reply({ + content: loc.get('e_interacreate_no_button'), + }); + } + + const filter = (i: MessageComponentInteraction) => i.customId === id; + const collector = interaction.channel?.createMessageComponentCollector({ filter }); + collector?.on('collect', async (i) => { + if (deferUpdate) { + await i.deferUpdate(); + } + const msg = await button?.interaction(i, client); + if (msg !== undefined) { + await i.update(msg); + } + }); +}; diff --git a/src/events/interactions/interactionCreate.ts b/src/events/interactions/interactionCreate.ts index 1b83778..cef1087 100644 --- a/src/events/interactions/interactionCreate.ts +++ b/src/events/interactions/interactionCreate.ts @@ -1,4 +1,4 @@ -import { Client, ComponentType, Interaction, InteractionType } from 'discord.js'; +import { Client, Interaction, InteractionType } from 'discord.js'; import { getLocale } from '../../utils/locales'; /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-interactionCreate */ @@ -29,21 +29,6 @@ export default (interaction: Interaction, client: Client) => { return modal.interaction(interaction, client); } - case InteractionType.MessageComponent: - if (interaction.componentType == ComponentType.Button) { - const button = client.buttons.list.get(interaction.customId); - if (!button) { - return interaction.reply({ - content: loc.get('e_interacreate_no_button'), - }); - } - - return button.interaction(interaction, client); - } - - // Handle only buttons for now - throw RangeError; - default: break; } diff --git a/src/modules/client.ts b/src/modules/client.ts index 1e6aeb8..9af4913 100644 --- a/src/modules/client.ts +++ b/src/modules/client.ts @@ -56,7 +56,7 @@ declare module 'discord.js' { name: string }, /** How the button interact */ - interaction: (interaction: ButtonInteraction, client: Client) => unknown + interaction: (interaction: MessageComponentInteraction, client: Client) => Promise } >, },