2022-07-03 21:09:57 +02:00
|
|
|
import { REST } from '@discordjs/rest';
|
|
|
|
import { Routes } from 'discord-api-types/v9';
|
2022-07-20 23:01:47 +02:00
|
|
|
import { Client } from 'discord.js';
|
2022-07-03 21:09:57 +02:00
|
|
|
import { readdir } from 'fs/promises';
|
|
|
|
|
2022-07-22 11:46:47 +02:00
|
|
|
/** Load all the commands. */
|
2022-07-20 23:01:47 +02:00
|
|
|
export default async (client: Client) => {
|
|
|
|
const rest = new REST({ version: '9' }).setToken(client.token ?? '');
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2022-07-20 23:01:47 +02:00
|
|
|
const command_categories = (await readdir(__dirname))
|
|
|
|
.filter(element => !element.endsWith('.js') && !element.endsWith('.ts'));
|
2022-07-03 21:09:57 +02:00
|
|
|
|
|
|
|
const commands = (
|
|
|
|
await Promise.all(
|
|
|
|
// For each categorie
|
|
|
|
command_categories.map(async command_category => {
|
|
|
|
// Retrieve all the commands
|
2022-07-20 23:01:47 +02:00
|
|
|
const command_files = await readdir(`${__dirname}/${command_category}`);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
|
|
|
// Add the command
|
|
|
|
return Promise.all(
|
|
|
|
command_files.map(async command_file => {
|
|
|
|
const command = (
|
|
|
|
await import(`../commands/${command_category}/${command_file}`)
|
|
|
|
).default;
|
|
|
|
|
2022-07-20 23:47:41 +02:00
|
|
|
// Add it to the collection so the interaction will work
|
2022-07-22 11:46:47 +02:00
|
|
|
client.commands.set(command.data(client).name, command);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2022-07-22 11:46:47 +02:00
|
|
|
return command.data(client).toJSON();
|
2022-07-03 21:09:57 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
).flat(2);
|
|
|
|
|
2022-07-20 23:47:41 +02:00
|
|
|
// Send commands to Discord
|
2022-07-20 23:01:47 +02:00
|
|
|
return await rest.put(Routes.applicationCommands(client.user?.id ?? ''), {
|
2022-07-03 21:09:57 +02:00
|
|
|
body: commands,
|
|
|
|
});
|
|
|
|
};
|