mbot/deploy-commands.js

46 lines
1.5 KiB
JavaScript

const { REST, Routes } = require('discord.js');
const { clientID, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
const informationStatus = "[INFO]: ";
const warningStatus = "[WARNING]: ";
const errorStatus = "[ERROR]: ";
const commands = [];
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(warningStatus + `This file at ${filePath} does not contain data or execute properties, will be ignored.`);
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(informationStatus + `Beginning deploy commands for ${commands.length} commands!`);
const data = await rest.put(
Routes.applicationCommands(clientID),
{ body: commands },
);
console.log(informationStatus + `Loaded all ${data.length} commands!`);
} catch (error) {
console.log(errorStatus + `Something went wrong while trying to load commanads! Refer to the error below to debug.`);
console.error(error);
}
})();