const { REST, Routes } = require('discord.js'); const config = require('./config.json'); const fs = require('fs'); const path = require('path'); const deployCommands = async () => { const commandsPath = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); const commandsMap = new Map(); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if (!command.data || typeof command.data.toJSON !== 'function') { continue; } const name = command.data.name; if (commandsMap.has(name)) { continue; } commandsMap.set(name, command.data.toJSON()); } const commands = Array.from(commandsMap.values()); const rest = new REST({ version: '10' }).setToken(config.token); const guildId = '936783164402188329'; try { console.log('🔍Obteniendo comandos de gremio existentes...'); const existingCommands = await rest.get(Routes.applicationGuildCommands(config.clientId, guildId)); const existingMap = new Map(existingCommands.map(cmd => [cmd.name, cmd])); const commandsToUpdateOrCreate = []; for (const command of commands) { const existing = existingMap.get(command.name); if (!existing) { commandsToUpdateOrCreate.push(command); } else { if (JSON.stringify(existing) !== JSON.stringify(command)) { commandsToUpdateOrCreate.push(command); } else { console.log(`✅ Command up-to-date, skipping: ${command.name}`); } } } if (commandsToUpdateOrCreate.length === 0) { return; } console.log('🔄 Registrando comandos nuevos y actualizados... Por favor espere'); const finalCommandsMap = new Map(); for (const cmd of commandsToUpdateOrCreate) { finalCommandsMap.set(cmd.name, cmd); } for (const existing of existingCommands) { if (!finalCommandsMap.has(existing.name)) { finalCommandsMap.set(existing.name, existing); } } const finalCommands = Array.from(finalCommandsMap.values()); await rest.put( Routes.applicationGuildCommands(config.clientId, guildId), { body: finalCommands } ); console.log('✅Comandos implementados exitosamente.'); } catch (error) { console.error('❌ Command deployment failed:', error); throw error; } }; module.exports = deployCommands;