const { REST, Routes } = require('discord.js'); const { getConfig } = require('./config'); const commands = require('./commands'); // Global by default: the bot can be invited to any number of servers, and the // campaign it answers with is decided per server by Dragonroll. // Pass "--guild " while developing to see changes in one server at once. function routeFor(clientId, guildId) { return guildId ? Routes.applicationGuildCommands(clientId, guildId) : Routes.applicationCommands(clientId); } function parseGuildArg(argv) { const index = argv.indexOf('--guild'); if (index === -1) return null; const guildId = argv[index + 1]; if (!guildId || guildId.startsWith('--')) { throw new Error('Usage: npm run deploy-commands -- --guild '); } return guildId; } async function deploy() { const config = getConfig(); const guildId = parseGuildArg(process.argv.slice(2)); const rest = new REST({ version: '10' }).setToken(config.discordToken); await rest.put(routeFor(config.discordClientId, guildId), { body: commands.map((command) => command.toJSON()) }); if (guildId) { console.log(`Registered ${commands.length} commands in Discord server ${guildId}.`); return; } console.log(`Registered ${commands.length} commands globally.`); console.log('New servers can take up to an hour to see them; the bot also registers them when it joins a server.'); } deploy().catch((error) => { console.error('Could not register Discord commands:', error.message); process.exitCode = 1; });