const fs = require('fs'); const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); const config = require('../config.json'); const { SlashCommandBuilder } = require('discord.js'); const commands = []; function loadCommands(client, prefix) { const ascii = require('ascii-table'); const table = new ascii().setHeading('Commands', 'Status'); const commandsFolder = fs.readdirSync('./Commands'); for (const folder of commandsFolder) { const commandFiles = fs.readdirSync(`./Commands/${folder}`).filter((file) => file.endsWith('.js')); for (const file of commandFiles) { const commandFile = require(`../commands/${folder}/${file}`); if (commandFile.data) { // Register slash commands commands.push(commandFile.data.toJSON()); } else if (commandFile.execute) { // Register prefix commands const command = new SlashCommandBuilder() .setName(commandFile.name) .setDescription(commandFile.description); // Add role option if required if (commandFile.requiredRole) { command.addRoleOption((option) => option.setName('role').setDescription('The required role for the command.') ); } commands.push(command.toJSON()); } else if (commandFile.commands) { // Add commands from the array commandFile.commands.forEach((command) => { const slashCommand = new SlashCommandBuilder() .setName(command.name) .setDescription(command.description); commands.push(slashCommand.toJSON()); }); } table.addRow(file, 'loaded'); } } console.log(table.toString(), '\nLoaded Commands'); } async function registerCommands() { const rest = new REST({ version: '9' }).setToken(config.token); try { console.log('Started refreshing application (/) commands.'); await rest.put( Routes.applicationCommands(config.clientId), { body: commands }, ); console.log('Successfully registered application (/) commands globally.'); } catch (error) { console.error(error); } } module.exports = { loadCommands, registerCommands, };