discord-music-bot/deploy-commands.js

62 lines
2.6 KiB
JavaScript

const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
const logger = require('./src/utils/logger'); // Assuming logger is setup
require('dotenv').config(); // Load .env variables
// --- Configuration ---
const clientId = process.env.CLIENT_ID;
const token = process.env.DISCORD_TOKEN;
// const guildId = process.env.GUILD_ID; // Uncomment for guild-specific commands during testing
if (!clientId || !token) {
logger.error('Missing CLIENT_ID or DISCORD_TOKEN in .env file for command deployment!');
process.exit(1);
}
const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(__dirname, 'src', 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
logger.info(`Started loading ${commandFiles.length} application (/) commands for deployment.`);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
try {
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
logger.info(`Loaded command: ${command.data.name}`);
} else {
logger.warn(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
} catch (error) {
logger.error(`Error loading command at ${filePath} for deployment: ${error.message}`, error);
}
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);
// and deploy your commands!
(async () => {
try {
logger.info(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
// Use Routes.applicationCommands(clientId) for global deployment
// Use Routes.applicationGuildCommands(clientId, guildId) for guild-specific deployment
const data = await rest.put(
Routes.applicationCommands(clientId), // Deploy globally
// Routes.applicationGuildCommands(clientId, guildId), // Deploy to specific guild (for testing)
{ body: commands },
);
logger.info(`Successfully reloaded ${data.length} application (/) commands globally.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
logger.error('Failed to refresh application commands:', error);
}
})();