import { REST } from '@discordjs/rest' import { Agent } from 'undici' import { Routes } from 'discord-api-types/v10' import { readdirSync } from 'node:fs' import { resolve, dirname } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) export async function deployCommands() { const token = process.env.DISCORD_TOKEN if (!token) throw new Error('Missing required environment variable: DISCORD_TOKEN') const clientId = process.env.DISCORD_CLIENT_ID if (!clientId) throw new Error('Missing required environment variable: DISCORD_CLIENT_ID') // If DEPLOY_GUILD_ID is not set, this test guild ID is used as a fallback. // Remove this hardcoded value (and the ?? below) before switching to global deploy. const TEST_GUILD_FALLBACK = '1508872676242882741' const guildId = process.env.DEPLOY_GUILD_ID ?? TEST_GUILD_FALLBACK const commandsDir = resolve(__dirname, 'commands') const files = readdirSync(commandsDir).filter((f) => f.endsWith('.js')) const commandData = [] for (const file of files) { const mod = await import(pathToFileURL(resolve(commandsDir, file)).href) if (!mod?.data) { console.warn(`[deploy] Skipping ${file} — no 'data' export found`) continue } console.log(`[deploy] Found command: ${mod.data.name}`) commandData.push(mod.data.toJSON()) } const rest = new REST({ version: '10', agent: new Agent({ connect: { timeout: 60_000 } }) }).setToken(token) try { console.log('[deploy] Commands array:', JSON.stringify(commandData)) console.log('[deploy] Registering to guild:', guildId, 'app:', clientId) if (guildId) { await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commandData }) console.log(`[deploy] Successfully registered ${commandData.length} commands to guild ${guildId}`) } else { await rest.put(Routes.applicationCommands(clientId), { body: commandData }) console.log(`[deploy] Successfully registered ${commandData.length} commands globally`) } } catch (err) { console.error('[deploy] Failed to register commands:', err) process.exit(1) } } if (process.argv[1].endsWith('deploy-commands.js')) { deployCommands().catch((err) => { console.error(err) process.exit(1) }) }