init: buttons

This commit is contained in:
Mylloon 2023-01-16 09:53:26 +01:00
parent e6d8bd7265
commit 0299770ec1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 75 additions and 2 deletions

36
src/buttons/loader.ts Normal file
View file

@ -0,0 +1,36 @@
import { readdir } from 'fs/promises';
import { removeExtension } from '../utils/misc';
import { Client } from 'discord.js';
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;
}),
);
}),
);
};

View file

@ -1,6 +1,7 @@
import loadClient, { quit } from './utils/client'; import loadClient, { quit } from './utils/client';
import loadEvents from './events/loader'; import loadEvents from './events/loader';
import loadModals from './modals/loader'; import loadModals from './modals/loader';
import loadButtons from './buttons/loader';
import loadCommands from './commands/loader'; import loadCommands from './commands/loader';
import { logStart } from './utils/misc'; import { logStart } from './utils/misc';
@ -41,6 +42,15 @@ const run = async () => {
throw logStart(modals_name, false); throw logStart(modals_name, false);
}); });
// Buttons Discord.JS
const buttons_name = 'Buttons';
await loadButtons(client)
.then(() => console.log(logStart(buttons_name, true)))
.catch((err) => {
console.error(err);
throw logStart(buttons_name, false);
});
// Commands Slash Discord.JS // Commands Slash Discord.JS
const commands_name = 'Commands'; const commands_name = 'Commands';
await loadCommands(client) await loadCommands(client)

View file

@ -37,7 +37,29 @@ declare module 'discord.js' {
interaction: (interaction: ModalSubmitInteraction, client: Client) => unknown interaction: (interaction: ModalSubmitInteraction, client: Client) => unknown
} }
>, >,
},
/** Store all the buttons */
buttons: {
categories: Collection<
/** Category name */
string,
/** Name of the buttons in the category */
string[]
>,
list: Collection<
/** Button name */
string,
/** Button itself */
{
/** Data about the button */
data: {
name: string
},
/** How the button interact */
interaction: (interaction: ButtonInteraction, client: Client) => unknown
} }
>,
},
/** Store all the slash commands */ /** Store all the slash commands */
commands: { commands: {
categories: Collection< categories: Collection<

View file

@ -24,6 +24,11 @@ export default async () => {
list: new Collection(), list: new Collection(),
}; };
client.buttons = {
categories: new Collection(),
list: new Collection(),
};
client.commands = { client.commands = {
categories: new Collection(), categories: new Collection(),
list: new Collection(), list: new Collection(),