discord-hosting.cloud
20% Off First MonthSee prices

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.

Updated 6 min read

Guild or global?

TypeWhere it appearsUse for
Guild commandOnly in one server; updates right awayTesting and private bots
Global commandEvery server the bot is in, and DMs if allowedPublic 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.

deploy-commands.js
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 ready

Commands do not show up?

  • The bot was invited without the applications.commands scope: 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.