# Commands

> Registering, reading, editing and deleting an app’s commands, globally and per server, and reading who can use them in a server. Every field of a command, the checks, limits and errors of each route.

## Before you read

These routes manage your app’s [application commands](https://docs.klang.gg/bots/reference/objects/#application-command).
The guide, with library examples, is [Slash commands](https://docs.klang.gg/bots/commands/).

- **`{app.id}`** is your app’s ID (the same as its bot’s). Only the app’s own token works:
  another app’s ID answers `403` with `20012`.
- **Global routes** start with `/applications/{app.id}/commands`. **Server routes** start with
  `/applications/{app.id}/guilds/{guild.id}/commands`, and need the bot to be in that server:
  otherwise they answer `403` with `50001`, whether the server exists or not.
- **Rate limits**: reads count against `read-commands` (20 every 10 seconds per bot) and writes
  against `commands-write` (20 every 20 seconds per app, global and servers together). See
  [Rate limits](https://docs.klang.gg/bots/reference/#rate-limits).
- **The answers** are commands in Discord’s shape, with the IDs bots see.

## List commands

```http
GET /applications/{app.id}/commands
GET /applications/{app.id}/guilds/{guild.id}/commands
```

Every command of the scope: the global list, or the server’s own commands (not the global ones).

| Query                | Type    | Notes                                                                               |
| -------------------- | ------- | ----------------------------------------------------------------------------------- |
| `with_localizations` | boolean | With `true`, each command, option and choice comes with its `*_localizations` maps. |

Without `with_localizations`, the maps are left out. If the request has an `X-Discord-Locale`
header with a locale, each name and description also comes as `name_localized` and
`description_localized`, in that locale or in the default text. Libraries don’t send it unless
you ask.

`GET /applications/1575120256797806592/commands` with `X-Discord-Locale: es-ES` (one command of
three):

```json
[
  {
    "id": "1575120622050381825",
    "type": 1,
    "application_id": "1575120256797806592",
    "name": "tide",
    "description": "Show the tide table for a harbor",
    "options": [
      {
        "type": 3,
        "name": "harbor",
        "description": "Which harbor",
        "required": true,
        "autocomplete": true,
        "name_localized": "puerto",
        "description_localized": "Qué puerto"
      },
      {
        "type": 4,
        "name": "days",
        "description": "How many days ahead",
        "min_value": 1,
        "max_value": 7,
        "name_localized": "days",
        "description_localized": "How many days ahead"
      },
      {
        "type": 3,
        "name": "units",
        "description": "Heights in meters or feet",
        "choices": [
          { "name": "Meters", "value": "m", "name_localized": "Metros" },
          { "name": "Feet", "value": "ft", "name_localized": "Pies" }
        ],
        "name_localized": "units",
        "description_localized": "Heights in meters or feet"
      }
    ],
    "default_member_permissions": null,
    "dm_permission": true,
    "nsfw": false,
    "integration_types": [0],
    "contexts": [0, 1],
    "version": "1575120622050381824",
    "name_localized": "marea",
    "description_localized": "Muestra la tabla de mareas de un puerto"
  }
]
```

| Status | Code    | When                                     |
| ------ | ------- | ---------------------------------------- |
| `403`  | `20012` | `{app.id}` isn’t your app.               |
| `403`  | `50001` | A server route, and the bot isn’t there. |

## Create a command

```http
POST /applications/{app.id}/commands
POST /applications/{app.id}/guilds/{guild.id}/commands
```

Creates one command, with the [body below](https://docs.klang.gg/bots/reference/commands/#the-body-of-a-command). If the scope already has a
command with the same name and type, that one is updated instead, keeping its ID: the answer is
`200` instead of `201`. Either way the answer is the command, with its translation maps.

```json
{
  "name": "lamp",
  "description": "Switch the lighthouse lamp",
  "default_member_permissions": "32",
  "contexts": [0],
  "options": [
    { "type": 1, "name": "on", "description": "Turn it on" },
    {
      "type": 1,
      "name": "off",
      "description": "Turn it off",
      "options": [{ "type": 5, "name": "quietly", "description": "Without a notice" }]
    }
  ]
}
```

The answer, `201`, for a server:

```json
{
  "id": "1575120622373343232",
  "type": 1,
  "application_id": "1575120256797806592",
  "guild_id": "1575120255329800192",
  "name": "lamp",
  "name_localizations": null,
  "description": "Switch the lighthouse lamp",
  "description_localizations": null,
  "options": [
    {
      "type": 1,
      "name": "on",
      "name_localizations": null,
      "description": "Turn it on",
      "description_localizations": null
    },
    {
      "type": 1,
      "name": "off",
      "name_localizations": null,
      "description": "Turn it off",
      "description_localizations": null,
      "options": [
        {
          "type": 5,
          "name": "quietly",
          "name_localizations": null,
          "description": "Without a notice",
          "description_localizations": null
        }
      ]
    }
  ],
  "default_member_permissions": "32",
  "dm_permission": false,
  "nsfw": false,
  "integration_types": [0],
  "contexts": [0],
  "version": "1575120622373343233"
}
```

| Status | Code    | When                                                                      |
| ------ | ------- | ------------------------------------------------------------------------- |
| `400`  | `50035` | A field breaks a rule. See [the body](https://docs.klang.gg/bots/reference/commands/#the-body-of-a-command).            |
| `400`  | `30032` | The scope already has 100 slash commands, or 15 user or message commands. |
| `400`  | `30034` | 200 commands were created in this server today (UTC).                     |
| `403`  | `20012` | `{app.id}` isn’t your app.                                                |
| `403`  | `50001` | A server route, and the bot isn’t there.                                  |

`30034` is a `400` and not a `429` on purpose: libraries wait and retry a `429`, and this one only
clears at midnight UTC. Updating a command doesn’t count towards it, and global commands don’t
either.

## Replace all commands

```http
PUT /applications/{app.id}/commands
PUT /applications/{app.id}/guilds/{guild.id}/commands
```

The body is an array of commands, and the scope ends up exactly like it, in one step: what isn’t
in the array is deleted, with its permissions. This is what libraries send when they sync. The
answer, `200`, is the new list, with the translation maps.

A command keeps its ID when the array names it by `id` or has one with its name and type.
A command whose fields didn’t change isn’t written again and keeps its `version`, so sending the
same list at every start costs nothing and tells nobody.

- Up to 100 slash, 15 user and 15 message commands. More, or two commands with the same name and
  type, answer `50035`.
- New commands count towards the 200 a day in a server (`30034`).
- `[]` deletes every command of the scope.

## The body of a command

The fields that `POST`, `PUT` and `PATCH` take. Unknown fields are ignored.

| Field                        | Type                         | Notes                                                                                                                 |
| ---------------------------- | ---------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `name`                       | string                       | Required. Slash commands: 1 to 32 lowercase letters, numbers, `-` and `_`. User and message: 1 to 32 characters.      |
| `type`                       | integer                      | `1` slash (default), `2` user, `3` message.                                                                           |
| `description`                | string                       | Slash commands: required, 1 to 100 characters. User and message commands: empty or left out.                          |
| `name_localizations`         | object or null               | Discord locale → name, with the same rules as `name`.                                                                 |
| `description_localizations`  | object or null               | Discord locale → description.                                                                                         |
| `options`                    | array of [options](https://docs.klang.gg/bots/reference/commands/#options) | Slash commands only, up to 25.                                                                                        |
| `default_member_permissions` | string or null               | Discord’s permission bits someone needs. `null` everyone, `"0"` administrators only.                                  |
| `contexts`                   | array of integers or null    | `0` servers, `1` the bot’s direct messages, `2` other private chats (kept, never used in Klang). `null`: `0` and `1`. |
| `dm_permission`              | boolean                      | Older field: `false` without `contexts` means `[0]`.                                                                  |
| `nsfw`                       | boolean                      | Only in age-restricted channels.                                                                                      |
| `integration_types`          | array of integers            | Accepted and ignored: Klang only installs bots in servers, and answers `[0]`.                                         |
| `default_permission`         | boolean                      | Older field, ignored.                                                                                                 |
| `id`                         | ID                           | Only in a bulk `PUT`: which command this one updates.                                                                 |

All of a command’s names, descriptions and choices, in every language, add up to 8,000 characters
at most. An unknown locale in a translation map answers `50035`.

`default_member_permissions` is translated to Klang’s permissions and back. A bit Klang makes up
from another (see [Permissions](https://docs.klang.gg/bots/differences/#permissions)) asks for the Klang permission
behind it. A bit for something Klang doesn’t have can’t be met, so the command becomes
administrators only and reads back as `"0"`.

### Options

| Field                       | Type              | Notes                                                                                                                                                    |
| --------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type`                      | integer           | Required. `1` subcommand, `2` group, `3` text, `4` integer, `5` boolean, `6` user, `7` channel, `8` role, `9` mentionable, `10` number, `11` attachment. |
| `name`                      | string            | Required. 1 to 32 lowercase letters, numbers, `-` and `_`. Unique at its level.                                                                          |
| `description`               | string            | Required. 1 to 100 characters.                                                                                                                           |
| `name_localizations`        | object or null    | As in the command.                                                                                                                                       |
| `description_localizations` | object or null    | As in the command.                                                                                                                                       |
| `required`                  | boolean           | Required options go before the rest. Never on a subcommand or a group.                                                                                   |
| `choices`                   | array             | Text, integer and number options: up to 25 `{ name, name_localizations?, value }`. The `name` is 1 to 100 characters; a text `value`, 1 to 100.          |
| `options`                   | array             | Subcommands: up to 25 options. Groups: up to 25 subcommands, at least one.                                                                               |
| `channel_types`             | array of integers | Channel options: `0` text, `2` voice, `4` category, `11` thread. Other types match nothing in Klang.                                                     |
| `min_value`, `max_value`    | number            | Integer and number options, between −2⁵³ and 2⁵³. Integers for an integer option.                                                                        |
| `min_length`, `max_length`  | integer           | Text options, 0 to 6,000 and 1 to 6,000.                                                                                                                 |
| `autocomplete`              | boolean           | Text, integer and number options without `choices`.                                                                                                      |

Subcommands and groups can’t be mixed with other options at the same level, a group only holds
subcommands, and groups only go at the top. An attachment option (`11`) is kept, but people can’t
fill it in yet: see [Options and choices](https://docs.klang.gg/bots/commands/#options-and-choices).

A problem answers `50035`, with the path of the field in `errors`. The name rule, for example:

```json
{
  "code": 50035,
  "message": "The request body isn’t valid.",
  "errors": {
    "name": {
      "_errors": [
        {
          "code": "INVALID",
          "message": "Must be 1-32 lowercase letters, numbers, “-” or “_”, without spaces (Discord’s rule)."
        }
      ]
    }
  }
}
```

## One command

```http
GET /applications/{app.id}/commands/{command.id}
GET /applications/{app.id}/guilds/{guild.id}/commands/{command.id}
```

The command, with its translation maps.

| Status | Code    | When                                     |
| ------ | ------- | ---------------------------------------- |
| `404`  | `10063` | No such command in this scope.           |
| `403`  | `20012` | `{app.id}` isn’t your app.               |
| `403`  | `50001` | A server route, and the bot isn’t there. |

## Edit a command

```http
PATCH /applications/{app.id}/commands/{command.id}
PATCH /applications/{app.id}/guilds/{guild.id}/commands/{command.id}
```

Changes the fields you send and keeps the rest. The whole command is checked again, with the
rules of [the body](https://docs.klang.gg/bots/reference/commands/#the-body-of-a-command). `type` doesn’t change. A name already used by another
command of the same type answers `50035` on `name`. The answer is the command; if anything
changed, it has a new `version`.

```json
{ "description": "Turn the lighthouse lamp on or off" }
```

| Status | Code    | When                                         |
| ------ | ------- | -------------------------------------------- |
| `400`  | `50035` | A field breaks a rule, or the name is taken. |
| `404`  | `10063` | No such command in this scope.               |

## Delete a command

```http
DELETE /applications/{app.id}/commands/{command.id}
DELETE /applications/{app.id}/guilds/{guild.id}/commands/{command.id}
```

Deletes the command and its permissions. The answer is `204`. Nobody can use it from that moment.

| Status | Code    | When                           |
| ------ | ------- | ------------------------------ |
| `404`  | `10063` | No such command in this scope. |

## Command permissions

```http
GET /applications/{app.id}/guilds/{guild.id}/commands/permissions
GET /applications/{app.id}/guilds/{guild.id}/commands/{command.id}/permissions
```

Who can use your commands in a server, as the server’s managers set it in **Server settings ›
Integrations**. The first route lists every command that has rules; the second, one command.
`{command.id}` can also be your app’s ID: those rules apply to every command that has none of its
own. How the rules combine is in [Who sees a command](https://docs.klang.gg/bots/commands/#who-sees-a-command).

Each entry of `permissions` has an `id`, a `type` (`1` role, `2` user, `3` channel) and
`permission` (`true` allows, `false` stops). Two IDs are special, as in Discord: the server’s ID
is the `@everyone` role, and the server’s ID minus one is “every channel”.

`GET …/commands/permissions`: `/lamp` stopped in every channel but `#general`, and allowed for the
Keepers role:

```json
[
  {
    "id": "1575120622373343232",
    "application_id": "1575120256797806592",
    "guild_id": "1575120255329800192",
    "permissions": [
      { "id": "1575120255329800191", "type": 3, "permission": false },
      { "id": "1575120255329800194", "type": 3, "permission": true },
      { "id": "1575120623174455296", "type": 1, "permission": true }
    ]
  }
]
```

| Status | Code    | When                                                     |
| ------ | ------- | -------------------------------------------------------- |
| `404`  | `10063` | One command’s route, and no such command in this server. |
| `404`  | `10066` | One command’s route, and the command has no rules here.  |
| `403`  | `50001` | The bot isn’t in the server.                             |

**Changing them** isn’t for bots, in Klang as in Discord:

```http
PUT /applications/{app.id}/guilds/{guild.id}/commands/permissions
PUT /applications/{app.id}/guilds/{guild.id}/commands/{command.id}/permissions
```

Both answer `403` with `20001`, `"Bots cannot use this endpoint"`. In Klang, people with
**Manage server** change them in the app, by role and by channel.
