add slashcommands
This commit is contained in:
parent
b012975954
commit
ae4ad46e6e
4 changed files with 86 additions and 11 deletions
31
scripts/registerCommands.js
Normal file
31
scripts/registerCommands.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
// 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();
|
Loading…
Add table
Add a link
Reference in a new issue