diff --git a/src/commands/loader.js b/src/commands/loader.js new file mode 100644 index 0000000..b815286 --- /dev/null +++ b/src/commands/loader.js @@ -0,0 +1,37 @@ +import { REST } from '@discordjs/rest'; +import { Routes } from 'discord-api-types/v9'; +import { readdir } from 'fs/promises'; + +export default async client => { + const rest = new REST({ version: '9' }).setToken(client.token); + + const command_categories = (await readdir('./src/commands')) + .filter(element => !element.endsWith('.js')); + + const commands = ( + await Promise.all( + // For each categorie + command_categories.map(async command_category => { + // Retrieve all the commands + const command_files = await readdir(`./src/commands/${command_category}`); + + // Add the command + return Promise.all( + command_files.map(async command_file => { + const command = ( + await import(`../commands/${command_category}/${command_file}`) + ).default; + + client.commands.set(command.data.name, command); + + return command.data.toJSON(); + }), + ); + }), + ) + ).flat(2); + + return await rest.put(Routes.applicationCommands(client.user.id), { + body: commands, + }); +}; diff --git a/src/index.js b/src/index.js index e38f786..a2f4a4a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import loadClient from './utils/client.js'; import loadEvents from './events/loader.js'; +import loadCommands from './commands/loader.js'; const run = async () => { console.log('Starting Botanique...'); @@ -14,19 +15,25 @@ const run = async () => { .catch(() => { throw 'Client ❌'; }); - console.log('Client ✅'); + if (client) { + console.log('Client ✅'); + } await loadEvents(client) - .then(() => console.log('Events ✅')) + .then(console.log('Events ✅')) .catch(() => { throw 'Events ❌'; }); await client.login(client.config.token_discord); - console.log( - `Botanique "${client.user.username}" ${client.config.version} started!` - ); + await loadCommands(client) + .then(console.log('Commands ✅')) + .catch(() => { + throw 'Commands ❌'; + }); + + console.log(`Botanique "${client.user.username}" ${client.config.version} started!`); }; run().catch(error => console.error(error)); diff --git a/src/utils/client.js b/src/utils/client.js index 4e201c0..446ad35 100644 --- a/src/utils/client.js +++ b/src/utils/client.js @@ -1,4 +1,4 @@ -import { Client, Intents } from 'discord.js'; +import { Client, Collection, Intents } from 'discord.js'; import { readFileSync } from 'fs'; const { version } = JSON.parse(readFileSync('./package.json')); @@ -11,10 +11,14 @@ export default async () => { ], }); + // Store the client configuration client.config = { version: version, token_discord: process.env.TOKEN_DISCORD, }; + // Store the commands available + client.commands = new Collection(); + return client; };