From 0299770ec1be379bde2f9b1c6aa3cc1c20e1cc85 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 16 Jan 2023 09:53:26 +0100 Subject: [PATCH] init: buttons --- src/buttons/loader.ts | 36 ++++++++++++++++++++++++++++++++++++ src/index.ts | 10 ++++++++++ src/modules/client.ts | 26 ++++++++++++++++++++++++-- src/utils/client.ts | 5 +++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/buttons/loader.ts diff --git a/src/buttons/loader.ts b/src/buttons/loader.ts new file mode 100644 index 0000000..521263c --- /dev/null +++ b/src/buttons/loader.ts @@ -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; + }), + ); + }), + ); +}; diff --git a/src/index.ts b/src/index.ts index 80ddc0f..7ce6835 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import loadClient, { quit } from './utils/client'; import loadEvents from './events/loader'; import loadModals from './modals/loader'; +import loadButtons from './buttons/loader'; import loadCommands from './commands/loader'; import { logStart } from './utils/misc'; @@ -41,6 +42,15 @@ const run = async () => { 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 const commands_name = 'Commands'; await loadCommands(client) diff --git a/src/modules/client.ts b/src/modules/client.ts index a70ae18..1e6aeb8 100644 --- a/src/modules/client.ts +++ b/src/modules/client.ts @@ -2,7 +2,7 @@ import { Collection } from 'discord.js'; import { SlashCommandBuilder } from '@discordjs/builders'; import { Database } from 'sqlite3'; -export {}; +export { }; declare module 'discord.js' { // eslint-disable-next-line no-shadow @@ -37,7 +37,29 @@ declare module 'discord.js' { 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 */ commands: { categories: Collection< diff --git a/src/utils/client.ts b/src/utils/client.ts index 84bd16a..916c85b 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -24,6 +24,11 @@ export default async () => { list: new Collection(), }; + client.buttons = { + categories: new Collection(), + list: new Collection(), + }; + client.commands = { categories: new Collection(), list: new Collection(),