Files
tv-discord/scripts/register-commands.js
T

36 lines
1.4 KiB
JavaScript

#!/usr/bin/env node
// Register slash commands without starting the bot.
// node scripts/register-commands.js → guild-scoped for cfg.guildIds, else global
// node scripts/register-commands.js --global → global
// node scripts/register-commands.js --clear → remove them (same scope rules)
const { REST, Routes } = require('discord.js');
const { loadConfig } = require('../src/config');
const { commandDefs } = require('../src/bot/commands');
async function main() {
const cfg = loadConfig();
const args = process.argv.slice(2);
const body = args.includes('--clear') ? [] : commandDefs;
const rest = new REST({ version: '10' }).setToken(cfg.token);
const guilds = args.includes('--global') ? [] : cfg.guildIds || [];
if (guilds.length) {
for (const g of guilds) {
const res = await rest.put(Routes.applicationGuildCommands(cfg.applicationId, g), { body });
console.log(`guild ${g}: ${res.length} commands`);
}
} else {
// keep Discord's Activity entry-point command (type 4): a bulk overwrite without it fails
const existing = await rest.get(Routes.applicationCommands(cfg.applicationId));
const res = await rest.put(Routes.applicationCommands(cfg.applicationId), {
body: [...body, ...existing.filter((c) => c.type === 4)],
});
console.log(`global: ${res.length} commands (may take up to an hour to appear)`);
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});