fix button implementation for updates

This commit is contained in:
Mylloon 2023-01-16 12:02:04 +01:00
parent 4bac943011
commit ffc395b4aa
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 34 additions and 18 deletions

View file

@ -1,6 +1,7 @@
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
import { removeExtension } from '../utils/misc'; 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) => { export default async (client: Client) => {
// Dossier des buttons // 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);
}
});
};

View file

@ -1,4 +1,4 @@
import { Client, ComponentType, Interaction, InteractionType } from 'discord.js'; import { Client, Interaction, InteractionType } from 'discord.js';
import { getLocale } from '../../utils/locales'; import { getLocale } from '../../utils/locales';
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-interactionCreate */ /** 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); 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: default:
break; break;
} }

View file

@ -56,7 +56,7 @@ declare module 'discord.js' {
name: string name: string
}, },
/** How the button interact */ /** How the button interact */
interaction: (interaction: ButtonInteraction, client: Client) => unknown interaction: (interaction: MessageComponentInteraction, client: Client) => Promise<InteractionUpdateOptions>
} }
>, >,
}, },