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-25 00:54:19 +02:00
|
|
|
import { removeExtension } from '../utils/misc';
|
2022-07-03 21:09:57 +02:00
|
|
|
|
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) => {
|
2022-07-27 21:46:09 +02:00
|
|
|
const rest = new REST({ version: '10' }).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
|
|
|
|
2022-07-25 00:54:19 +02:00
|
|
|
// Add the category to the collection for the help command
|
|
|
|
client.commands.categories.set(
|
|
|
|
command_category,
|
|
|
|
command_files.map(removeExtension),
|
|
|
|
);
|
|
|
|
|
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-25 00:54:19 +02:00
|
|
|
command.data = command.data(client);
|
|
|
|
client.commands.list.set(command.data.name, command);
|
2022-07-03 21:09:57 +02:00
|
|
|
|
2022-07-25 00:54:19 +02:00
|
|
|
return command.data.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,
|
|
|
|
});
|
|
|
|
};
|