69 lines
2.1 KiB
TypeScript
69 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);
|
||
|
}
|
||
|
});
|
||
|
};
|