Docs
Slash commands
Register commands with Discord's routes, and answer them when people run them from the / menu.
A bot registers slash commands (Discord's application commands) with Discord's routes, using its own
token. Typing / in the message box lists the commands the person may run in that channel: the server's
own, then each bot that is in the server and can see the channel, under its name and picture. Pressing
Enter sends your bot an INTERACTION_CREATE, and you answer it within 3 seconds.
The bot is its own application, so the application id is the bot's id (READY's application.id,
or Get Current Application). discord.py's tree.sync() and discord.js's
application.commands.set() work unmodified.
An example
discord.py
import discord
import yarl
from discord import app_commands
ORIGIN = 'https://app.tethr.lol' # your tether server
discord.http.Route.BASE = f'{ORIGIN}/api/v1'
discord.asset.Asset.BASE = f'{ORIGIN}/api/v1'
discord.gateway.DiscordWebSocket.DEFAULT_GATEWAY = yarl.URL(ORIGIN.replace('http', 'ws', 1) + '/gateway')
client = discord.Client(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)
@tree.command(name='echo', description='Repeat what you say')
@app_commands.describe(text='What to repeat')
async def echo(interaction: discord.Interaction, text: str):
await interaction.response.send_message(text)
@client.event
async def on_ready():
await tree.sync() # every server; tree.sync(guild=discord.Object(id=...)) for one
print(f'Logged in as {client.user}')
client.run('YOUR BOT TOKEN')
discord.js
import { Client, Events, GatewayIntentBits, SlashCommandBuilder } from 'discord.js';
const ORIGIN = 'https://app.tethr.lol'; // your tether server
const client = new Client({
intents: [GatewayIntentBits.Guilds],
rest: { api: `${ORIGIN}/api`, version: '1', cdn: `${ORIGIN}/api/v1` },
});
client.once(Events.ClientReady, async (c) => {
await c.application.commands.set([
new SlashCommandBuilder()
.setName('echo')
.setDescription('Repeat what you say')
.addStringOption((o) => o.setName('text').setDescription('What to repeat').setRequired(true)),
]);
console.log(`Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async (i) => {
if (i.isChatInputCommand() && i.commandName === 'echo')
await i.reply(i.options.getString('text'));
});
client.login(process.env.BOT_TOKEN);
Run it, type /echo in a channel the bot can see, and the bot says what you typed.
Registering commands
The routes are Discord's, and they answer the bot itself with its token. A person gets 403
20002, another application's id gets 404 10002.
| Route | What it does |
|---|---|
| Get / Create global commands | A global command works in every server the bot is in. |
| Bulk Overwrite | Replaces the whole list. A command with the same name and type keeps its id (and version). |
| Get, Edit, Delete | One command. |
The same under /applications/{application.id}/guilds/{guild.id}/commands |
A server's own commands. The bot must be in the server (403 50001 otherwise). |
Writes are limited to 40 a minute per bot; a sync is one request. A command of a server hides the global command of the same name and type.
Discord's rules, kept
- A
/command's name is 1 to 32 lowercase letters, digits,-or_; its description 1 to 100 characters. - Up to 25 options; required options come before optional ones. Up to 25
choices, not together withautocomplete.min_value,max_value,min_length,max_lengthandchannel_typeswork. - At most 8000 characters of names, descriptions and choices in one command; at most 100
/commands, 15 user and 15 message commands in one scope (30032). - A bad body is
40050035with the path of the field (options.0.choices.2.value). default_member_permissions(a decimal string;nullfor everyone,"0"for administrators only) decides who sees a command.
Option types (see Application command option): 1 sub command, 2 group, 3 string, 4 integer, 5 boolean, 6 user, 7 channel, 8 role, 9 mentionable, 10 number, 11 attachment.
Who sees a command
The / menu asks Get Application Command Index for the channel it is
in and lists a bot's command when:
- the bot is a member of the server and can see the channel, and
- the person holds the command's
default_member_permissionsthere.
A bot command that has the same name as one of the server's built-in commands is listed in both places,
each under its own heading, never merged. A command with sub commands is one line per sub command
(/config roles add), as Discord's client shows them.
What you receive
INTERACTION_CREATE with type: 2:
{
"id": "…",
"application_id": "<your bot's id>",
"type": 2,
"token": "…",
"guild_id": "…",
"channel_id": "…",
"member": { "user": {}, "permissions": "…" },
"data": {
"id": "<command id>",
"name": "echo",
"type": 1,
"options": [{ "name": "text", "type": 3, "value": "hello" }],
"resolved": {}
}
}
optionsare Discord's[{name, type, value}], nested for sub commands and groups ({name: "roles", type: 2, options: [{name: "add", type: 1, options: […]}]}).resolvedholdsusers,members(withoutuser, withpermissions),rolesandchannelsfor the user, role, channel and mentionable values.- Everything is checked against your definition first (types, ranges, lengths, choices,
channel_types, required options, people who are in the server, channels the person can see). A bad value never reaches you.
Answering
Answer with Create Interaction Response, within 3 seconds:
- type
4: a message in the channel, or only for the person withflags: 64; - type
5: "I'll answer later". Nothing is drawn while you think (there is no "thinking" placeholder), so the first Edit Original Interaction Response makes the message, public, or private if the deferral was. Follow-ups add more.
Types 6 and 7 are for buttons and menus and are 400 for a command. More in
Buttons and interactions.
Autocomplete
An option marked autocomplete: true sends INTERACTION_CREATE with type 4 while the person types:
data is the command with the options so far, and the one being typed has focused: true (its value is
the text so far, a string even for numbers). Answer with callback type 8 and up to 25
{name, value} choices (numbers for INTEGER and NUMBER options). The app asks after a short pause, and
not more than 30 times in 5 seconds.
User and message commands
Register them with type: 2 (user) or type: 3 (message), a name (spaces and capitals allowed) and no description or
options, as discord.py's app_commands.ContextMenu and discord.js's ContextMenuCommandBuilder do. They appear
under Apps in the right-click menu of a person or a message, for whoever may run them there. The bot gets
INTERACTION_CREATE type 2 with data.type 2 or 3, data.target_id and data.resolved: users and
members for a person, messages for a message. The message is as your bot may read it: if the server has
not given the bot the text, content is empty, as in any message it is sent. Answer as for any command.
What is not there
- ATTACHMENT options can be registered (so a whole tree syncs) but the app cannot attach a file to a
command option yet; the
/menu says so instead of running the command. Nothing hasresolved.attachments. Your answers can still carry files: Files. - Direct messages and user installs. Commands run in servers only;
contextsandintegration_typesare stored and shown back,dm_permissionis alwaysfalse. - Localizations are accepted and ignored.
nsfwis stored and nothing hides the command. - Per-command permission overrides (Discord's "Integrations" settings) are not offered: who sees a
command is its
default_member_permissionsand whether the bot sees the channel. - "X used /command" is not drawn above the reply; the message has no
interactionfield.
When a bot leaves a server its commands for that server stay stored (hidden) and are there again if it is added back. Deleting the bot deletes all its commands.