Botanique/src/buttons/loader.ts
Anri 8f096b9589 feat: Reminders (#44)
Checklist:

- [x] Suivre les indications de `CONTRIBUTING.md`
- [x] Référence aux tickets (par exemple `Closes #xyz`)

Closes #1

Additional changes:
- use of ChatInputCommandInteraction (part of v14)
- fixed locales progress bars
- updates dependencies
- better handle of errors
- support of modals
- support of buttons

Co-authored-by: Mylloon <kennel.anri@tutanota.com>
Reviewed-on: https://git.kennel.ml/ConfrerieDuKassoulait/Botanique/pulls/44
2023-01-17 12:15:14 +01:00

68 lines
2.1 KiB
TypeScript

import { readdir } from 'fs/promises';
import { removeExtension } from '../utils/misc';
import { ChatInputCommandInteraction, Client, MessageComponentInteraction } from 'discord.js';
import { getLocale } from '../utils/locales';
export default async (client: Client) => {
// Dossier des buttons
const buttons_categories = (await readdir(__dirname))
.filter(element => !element.endsWith('.js') && !element.endsWith('.ts'));
await Promise.all(
// For each categorie
buttons_categories.map(async buttons_category => {
// Retrieve all the commands
const button_files = await readdir(`${__dirname}/${buttons_category}`);
// Add the category to the collection for the help command
client.buttons.categories.set(
buttons_category,
button_files.map(removeExtension),
);
// Add the button
return Promise.all(
button_files.map(async button_file => {
const button = (
await import(`../buttons/${buttons_category}/${button_file}`)
).default;
// Add it to the collection so the interaction will work
client.buttons.list.set(button.data.name, button);
return button.data;
}),
);
}),
);
};
/**
* 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 | MessageComponentInteraction, id: string, deferUpdate = false) => {
const loc = getLocale(client, interaction.locale);
const button = client.buttons.list.get(id.split('_')[0]);
if (!button) {
interaction.reply({
content: loc.get('e_interacreate_no_button'),
ephemeral: true,
});
}
const filter = (i: MessageComponentInteraction) => i.customId === id;
const collector = interaction.channel?.createMessageComponentCollector({ filter, max: 1 });
collector?.on('collect', async (i) => {
if (deferUpdate) {
await i.deferUpdate();
}
const msg = await button?.interaction(i, client);
if (msg !== undefined) {
await i.update(msg);
}
});
};