import { REST } from '@discordjs/rest'; import { Routes } from 'discord-api-types/v9'; import { Client } from 'discord.js'; import { readdir } from 'fs/promises'; import { removeExtension } from '../utils/misc'; /** Load all the commands. */ export default async (client: Client) => { const rest = new REST({ version: '9' }).setToken(client.token ?? ''); const command_categories = (await readdir(__dirname)) .filter(element => !element.endsWith('.js') && !element.endsWith('.ts')); const commands = ( await Promise.all( // For each categorie command_categories.map(async command_category => { // Retrieve all the commands const command_files = await readdir(`${__dirname}/${command_category}`); // Add the category to the collection for the help command client.commands.categories.set( command_category, command_files.map(removeExtension), ); // Add the command return Promise.all( command_files.map(async command_file => { const command = ( await import(`../commands/${command_category}/${command_file}`) ).default; // Add it to the collection so the interaction will work command.data = command.data(client); client.commands.list.set(command.data.name, command); return command.data.toJSON(); }), ); }), ) ).flat(2); // Send commands to Discord return await rest.put(Routes.applicationCommands(client.user?.id ?? ''), { body: commands, }); };