Skip to content

Concepts

Discord slash commands explained: how bots receive them

How slash commands work for bot makers: registration, guild or global, options, permissions, the three second rule, deferring replies and ephemeral messages.

· Updated · 7 min read
Cover image: Discord slash commands explained: how bots receive them

Slash commands are the normal way to talk to a Discord bot. Members type a slash, pick a command from a list, fill in the options and send it. This guide explains how they work from the bot maker side, which helps when you read or change the generated code.

Registration: telling Discord the commands exist

A slash command exists once the bot has registered it with Discord through the API. That is the job of the deploy script in your project. It sends the name, description and options of every command. You run it once and again whenever you add or change a command. It is not run every time the bot starts.

Guild commands and global commands

  • Guild commands belong to one server and appear there immediately. Use them while you build and test.
  • Global commands belong to the bot and work in every server it joins. They can take longer to show up after a change.

The deploy script in a BotsMaker project uses guild commands when GUILD_ID is set in the .env file, and global commands when it is empty.

Options and types

A command can have options such as a string, an integer, a member, a channel or a role. Discord checks the type before the bot sees the command, so a member option always contains a member. Options can be required, can have limits, and can offer a fixed list of choices.

Who may use a command

A command can declare a default member permission, such as Kick Members. Discord hides the command from members who do not have it, and server admins can change this in the server settings. Because the check is enforced by Discord, it is safer than a check in the code alone, but the bot should still verify permissions for risky actions.

The three second rule

When a command arrives, the bot has three seconds to respond. If the work takes longer, such as calling an AI API or reading a lot of data, the bot must acknowledge the command first with a deferred reply. After that it has fifteen minutes to send the real answer. The generated code defers before slow work and then edits the reply.

await interaction.deferReply()
const answer = await doSlowThing()
await interaction.editReply(answer)

Replies only the member sees

A reply can be ephemeral, which means only the member who ran the command sees it. This is right for errors, personal data and confirmations. In discord.js you set the Ephemeral flag on the reply.

Buttons, menus and modals

Slash commands can answer with components: buttons, select menus and forms called modals. A click arrives as a separate interaction with a custom id that the bot chose. This is how ticket panels and giveaway buttons work, and they do not need the Message Content intent.

Common mistakes

  • Changing a command but forgetting to run the deploy script again.
  • Registering with the wrong CLIENT_ID.
  • Not answering within three seconds.
  • Using the same custom id for two different buttons.

Written by

BotsMaker Team

We build BotsMaker, the AI Discord bot creator. The guides come from what we see when bots are generated, tested and set up in the Discord Developer Portal and on real hosts.

More guides

RSS feed · All guides