require('dotenv').config({ path: '../backend/.env' }); const { REST, Routes } = require('discord.js'); const fs = require('fs'); const path = require('path'); const commands = []; const commandsPath = path.join(__dirname, 'commands'); 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 (command.enabled === false || command.dev === true) continue; if (command.builder) { try { // Some command modules export builder as a function (builder => builder...) or as an instance if (typeof command.builder === 'function') { // create a temporary SlashCommandBuilder by requiring it from discord.js const { SlashCommandBuilder } = require('discord.js'); const built = command.builder(new SlashCommandBuilder()); if (built && typeof built.toJSON === 'function') commands.push(built.toJSON()); else commands.push({ name: command.name, description: command.description }); } else if (command.builder && typeof command.builder.toJSON === 'function') { commands.push(command.builder.toJSON()); } else { commands.push({ name: command.name, description: command.description }); } } catch (e) { console.warn(`Failed to build command ${command.name}:`, e && e.message ? e.message : e); commands.push({ name: command.name, description: command.description }); } } else { commands.push({ name: command.name, description: command.description }); } } const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN); const deployCommands = async (guildId) => { try { // Minimal logging: indicate a refresh is happening (no per-guild spam) console.log('🔁 Refreshing application commands...'); await rest.put( Routes.applicationGuildCommands(process.env.DISCORD_CLIENT_ID, guildId), { body: commands }, ); console.log(`✅ Reloaded application commands (${commands.length} commands)`); } catch (error) { console.error('Failed to deploy commands:', error && error.message ? error.message : error); } }; // Standalone execution if (require.main === module) { const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.once('ready', async () => { console.log(`Logged in as ${client.user.tag}`); console.log(`Deploying commands to ${client.guilds.cache.size} guilds...`); for (const [guildId, guild] of client.guilds.cache) { await deployCommands(guildId); } console.log('All commands deployed!'); client.destroy(); }); client.login(process.env.DISCORD_BOT_TOKEN); } module.exports = deployCommands;