Guide
Register Discord slash commands: guild and global
Slash commands are registered with Discord separately from your bot's code that handles them. Register them once (and again when they change), not on every message.
Guild or global?
| Type | Where it appears | Use for |
|---|---|---|
| Guild command | Only in one server; updates right away | Testing and private bots |
| Global command | Every server the bot is in, and DMs if allowed | Public bots |
discord.js: a register script
Run this once from the panel console or your computer whenever commands change. Remove GUILD_ID to register globally.
import 'dotenv/config';
import { REST, Routes, SlashCommandBuilder } from 'discord.js';
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Check the bot\'s latency'),
].map((c) => c.toJSON());
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
const route = process.env.GUILD_ID
? Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID)
: Routes.applicationCommands(process.env.CLIENT_ID);
await rest.put(route, { body: commands });
console.log(`Registered ${commands.length} commands`);discord.py: syncing the tree
TEST_GUILD = discord.Object(id=123456789012345678)
@client.event
async def on_ready():
tree.copy_global_to(guild=TEST_GUILD)
await tree.sync(guild=TEST_GUILD) # shows up in the test server right away
# await tree.sync() # global, when you are readyCommands do not show up?
- The bot was invited without the
applications.commandsscope: invite it again with that scope. - You registered to a different application ID (check
CLIENT_ID). - Discord caches commands in the client: press Ctrl+R (Cmd+R) to reload Discord.
- Registration is rate limited: do not re-register on every start of a big bot.