const { REST, Routes } = require('discord.js'); const fs = require('fs'); const path = require('path'); require('dotenv').config(); const token = process.env.DISCORD_BOT_TOKEN; const clientId = process.env.DISCORD_CLIENT_ID; if (!token) { console.error('Missing DISCORD_BOT_TOKEN'); process.exit(1); } if (!clientId) { console.error('Missing DISCORD_CLIENT_ID'); process.exit(1); } const commandsPath = path.join(__dirname, '../commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); const commands = []; 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()); console.log(`Loaded command: ${command.data.name}`); } } const rest = new REST({ version: '10' }).setToken(token); (async () => { try { console.log(`\nFetching existing commands...`); const existingCommands = await rest.get( Routes.applicationCommands(clientId) ); console.log(`Found ${existingCommands.length} existing commands`); const entryPointCommands = existingCommands.filter(cmd => cmd.type === 4); const allCommands = [...commands, ...entryPointCommands.map(cmd => ({ name: cmd.name, description: cmd.description, type: cmd.type, handler: cmd.handler, }))]; console.log(`Registering ${commands.length} commands (preserving ${entryPointCommands.length} entry points)...`); const data = await rest.put( Routes.applicationCommands(clientId), { body: allCommands } ); console.log(`\nSuccessfully registered ${data.length} commands:`); data.forEach(cmd => console.log(` - /${cmd.name}`)); } catch (error) { if (error.code === 50240) { console.warn('\nEntry Point detected. Registering individually...'); let successCount = 0; for (const command of commands) { try { await rest.post( Routes.applicationCommands(clientId), { body: command } ); console.log(` + /${command.name}`); successCount++; } catch (postError) { if (postError.code === 50045) { console.log(` ~ /${command.name} (already exists)`); } else { console.error(` x /${command.name}: ${postError.message}`); } } } console.log(`\nRegistered ${successCount} commands individually.`); } else { console.error('Error registering commands:', error); } } })();