Load commands

This commit is contained in:
Mylloon 2022-07-03 20:37:23 +02:00
parent 6eda872b08
commit 8f33f68fe6
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 54 additions and 6 deletions

37
src/commands/loader.js Normal file
View file

@ -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,
});
};

View file

@ -1,5 +1,6 @@
import loadClient from './utils/client.js'; import loadClient from './utils/client.js';
import loadEvents from './events/loader.js'; import loadEvents from './events/loader.js';
import loadCommands from './commands/loader.js';
const run = async () => { const run = async () => {
console.log('Starting Botanique...'); console.log('Starting Botanique...');
@ -14,19 +15,25 @@ const run = async () => {
.catch(() => { .catch(() => {
throw 'Client ❌'; throw 'Client ❌';
}); });
if (client) {
console.log('Client ✅'); console.log('Client ✅');
}
await loadEvents(client) await loadEvents(client)
.then(() => console.log('Events ✅')) .then(console.log('Events ✅'))
.catch(() => { .catch(() => {
throw 'Events ❌'; throw 'Events ❌';
}); });
await client.login(client.config.token_discord); await client.login(client.config.token_discord);
console.log( await loadCommands(client)
`Botanique "${client.user.username}" ${client.config.version} started!` .then(console.log('Commands ✅'))
); .catch(() => {
throw 'Commands ❌';
});
console.log(`Botanique "${client.user.username}" ${client.config.version} started!`);
}; };
run().catch(error => console.error(error)); run().catch(error => console.error(error));

View file

@ -1,4 +1,4 @@
import { Client, Intents } from 'discord.js'; import { Client, Collection, Intents } from 'discord.js';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
const { version } = JSON.parse(readFileSync('./package.json')); const { version } = JSON.parse(readFileSync('./package.json'));
@ -11,10 +11,14 @@ export default async () => {
], ],
}); });
// Store the client configuration
client.config = { client.config = {
version: version, version: version,
token_discord: process.env.TOKEN_DISCORD, token_discord: process.env.TOKEN_DISCORD,
}; };
// Store the commands available
client.commands = new Collection();
return client; return client;
}; };