31 lines
No EOL
1.1 KiB
JavaScript
31 lines
No EOL
1.1 KiB
JavaScript
// this script registers slash commands for a Discord bot using the Discord.js library
|
|
// needs to run once to set up commands, after they changed
|
|
import 'dotenv/config';
|
|
import { REST, Routes } from 'discord.js';
|
|
import { commands } from '../src/functions/commands.js';
|
|
|
|
const { DISCORD_TOKEN, CLIENT_ID, GUILD_ID } = process.env;
|
|
|
|
if (!DISCORD_TOKEN) throw new Error('DISCORD_TOKEN missing');
|
|
if (!CLIENT_ID) throw new Error('CLIENT_ID missing (Discord application ID)');
|
|
|
|
const rest = new REST({ version: '10' }).setToken(DISCORD_TOKEN);
|
|
const body = commands.map(c => c.data.toJSON());
|
|
|
|
async function main() {
|
|
try {
|
|
console.log('Registering slash commands...');
|
|
if (GUILD_ID) {
|
|
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body });
|
|
console.log(`Registered ${body.length} guild command(s) to ${GUILD_ID}`);
|
|
console.log(body);
|
|
} else {
|
|
await rest.put(Routes.applicationCommands(CLIENT_ID), { body });
|
|
console.log(`Registered ${body.length} global command(s)`);
|
|
console.log(body);
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|
|
main(); |