import { REST, Routes } from 'discord.js'; import { readdirSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export class Deployer { static async deploy(clientId: string, token: string) { const commands = []; const rootDir = join(__dirname, '..'); const commandsPath = join(rootDir, 'commands'); const commandFolders = readdirSync(commandsPath); for (const folder of commandFolders) { const folderPath = join(commandsPath, folder); const commandFiles = readdirSync(folderPath).filter(file => file.endsWith('.ts')); for (const file of commandFiles) { const filePath = join(folderPath, file); const fileUrl = pathToFileURL(filePath).href; const command = (await import(fileUrl)).default; if (command && 'data' in command) { commands.push(command.data.toJSON()); } } } const rest = new REST().setToken(token); console.log(`[DEPLOYER] Refreshing ${commands.length} application (/) commands.`); const data: any = await rest.put( Routes.applicationCommands(clientId), { body: commands }, ); console.log(`[DEPLOYER] Successfully reloaded ${data.length} application (/) commands.`); return data.length; } static async deployIfMissing(clientId: string, token: string) { const rest = new REST().setToken(token); try { const data: any = await rest.get(Routes.applicationCommands(clientId)); if (data.length > 0) { console.log('[DEPLOYER] Commands already registered. Skipping deployment.'); return data.length; } } catch (error) { // Commands don't exist yet } return this.deploy(clientId, token); } }