# Components

> Every component type Klang takes in messages and modals, with its fields and limits. Action rows, buttons, the five select menus, the layout components of IS_COMPONENTS_V2 messages, and the fields of a modal, from labels and text inputs to file uploads, radio groups and checkboxes.

## How to read it

Components go in the `components` of a [message](https://docs.klang.gg/bots/reference/objects/#message) and in the
`data` of a modal callback. The guides are
[Buttons, menus and layouts](https://docs.klang.gg/bots/components/) and [Modals](https://docs.klang.gg/bots/modals/).

- **Types and fields** are Discord’s, with the same numbers, so library builders work as they are.
- **`id`** is an optional number on every component, unique in its message or modal, from 1 to
  2,147,483,647. Leave it out, or send `0`, and Klang numbers the component itself, in order. It
  always comes back.
- **What you send is what comes back**, plus the `id`s and the fields Klang fills in for images
  and files. Fields with `null` count as not sent.
- **Anything that breaks a rule** answers `400` with `50035`, and the error says where
  (`components.0.components.1.custom_id`). Nothing is dropped quietly.

## Component types

| `type` | Name               | In messages                   | In modals           | In Klang                 |
| ------ | ------------------ | ----------------------------- | ------------------- | ------------------------ |
| `1`    | Action row         | Yes                           | The older form only | Supported                |
| `2`    | Button             | In an action row or a section | No                  | Supported, but style `6` |
| `3`    | String select      | In an action row              | In a label          | Supported                |
| `4`    | Text input         | No                            | In a label          | Supported                |
| `5`    | User select        | In an action row              | In a label          | Supported                |
| `6`    | Role select        | In an action row              | In a label          | Supported                |
| `7`    | Mentionable select | In an action row              | In a label          | Supported                |
| `8`    | Channel select     | In an action row              | In a label          | Supported                |
| `9`    | Section            | v2                            | No                  | Supported                |
| `10`   | Text display       | v2                            | Yes                 | Supported                |
| `11`   | Thumbnail          | v2, in a section              | No                  | Supported                |
| `12`   | Media gallery      | v2                            | No                  | Supported                |
| `13`   | File               | v2                            | No                  | Supported                |
| `14`   | Separator          | v2                            | No                  | Supported                |
| `17`   | Container          | v2                            | No                  | Supported                |
| `18`   | Label              | No                            | Yes                 | Supported                |
| `19`   | File upload        | No                            | In a label          | Supported                |
| `21`   | Radio group        | No                            | In a label          | Supported                |
| `22`   | Checkbox group     | No                            | In a label          | Supported                |
| `23`   | Checkbox           | No                            | In a label          | Supported                |

“v2” means a message with the `IS_COMPONENTS_V2` flag (`1 << 15`). Without it, a message takes
only action rows. Discord’s component types that aren’t listed don’t exist in Klang either, and a
type in a place it doesn’t fit answers `50035` with a message that says so, such as “Text inputs
only go in modals.”

## Action row

| Field        | Type    | Notes                                                                   |
| ------------ | ------- | ----------------------------------------------------------------------- |
| `type`       | `1`     |                                                                         |
| `id`         | integer |                                                                         |
| `components` | array   | 1 to 5 [buttons](https://docs.klang.gg/bots/reference/components/#button), or exactly one [select menu](https://docs.klang.gg/bots/reference/components/#select-menus). |

A message without `IS_COMPONENTS_V2` has up to 5 action rows. In a v2 message, rows go at the top
level or inside a [container](https://docs.klang.gg/bots/reference/components/#container).

## Button

| Field       | Type    | Notes                                                                                       |
| ----------- | ------- | ------------------------------------------------------------------------------------------- |
| `type`      | `2`     |                                                                                             |
| `id`        | integer |                                                                                             |
| `style`     | integer | `1` primary, `2` secondary, `3` success, `4` danger, `5` link. `6` is refused.              |
| `label`     | string  | Up to 80 characters. A button needs a `label`, an `emoji` or both.                          |
| `emoji`     | object  | `{ "name": "⛵" }` for a Unicode emoji, or `{ "id": "…", "name": "…", "animated": false }`. |
| `custom_id` | string  | 1 to 100 characters, unique in the message. Every style but `5` needs one.                  |
| `url`       | string  | Only style `5`, which needs one: `http` or `https`, up to 512 characters.                   |
| `disabled`  | boolean | Shown dimmed; nobody can press it.                                                          |

## Select menus

The five menus share these fields:

| Field         | Type    | Notes                                                                                |
| ------------- | ------- | ------------------------------------------------------------------------------------ |
| `type`        | integer | `3` string, `5` user, `6` role, `7` mentionable, `8` channel.                        |
| `id`          | integer |                                                                                      |
| `custom_id`   | string  | 1 to 100 characters, unique in the message or modal.                                 |
| `placeholder` | string  | Up to 150 characters.                                                                |
| `min_values`  | integer | 0 to 25. By default `1`. Not more than `max_values`.                                 |
| `max_values`  | integer | 1 to 25. By default `1`.                                                             |
| `disabled`    | boolean | In messages only. A modal can’t have disabled fields.                                |
| `required`    | boolean | In modals only, `true` by default. A required menu needs `min_values` of at least 1. |

**String select** (`3`) adds `options`: 1 to 25, with unique values.

| Field         | Type    | Notes                                                            |
| ------------- | ------- | ---------------------------------------------------------------- |
| `label`       | string  | 1 to 100 characters.                                             |
| `value`       | string  | 1 to 100 characters. What your bot gets in `values`.             |
| `description` | string  | Up to 100 characters.                                            |
| `emoji`       | object  | As in a button.                                                  |
| `default`     | boolean | Chosen when the menu shows. Not more defaults than `max_values`. |

`min_values` can’t be more than the number of options.

**User, role, mentionable and channel selects** (`5`–`8`) add `default_values`: up to 25
`{ "id": "…", "type": "user" | "role" | "channel" }`, of the kinds the menu takes (a mentionable
menu takes users and roles), and between `min_values` and `max_values` of them. `@everyone` is
the server’s ID, as everywhere in the bot API. **Channel select** (`8`) also takes
`channel_types`, a list of Discord’s channel type numbers; people only see the channels of those
types.

## Layout components

These go in messages with `IS_COMPONENTS_V2`. Such a message has up to **40 components**,
counting the ones inside others, and no `content`, `embeds`, stickers or polls.

### Section

| Field        | Type    | Notes                                                                              |
| ------------ | ------- | ---------------------------------------------------------------------------------- |
| `type`       | `9`     |                                                                                    |
| `id`         | integer |                                                                                    |
| `components` | array   | 1 to 3 [text displays](https://docs.klang.gg/bots/reference/components/#text-display).                                             |
| `accessory`  | object  | A [thumbnail](https://docs.klang.gg/bots/reference/components/#thumbnail) or a [button](https://docs.klang.gg/bots/reference/components/#button), shown beside the text. Required. |

### Text display

| Field     | Type    | Notes                                                                                                                                                                             |
| --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `type`    | `10`    |                                                                                                                                                                                   |
| `id`      | integer |                                                                                                                                                                                   |
| `content` | string  | 1 to 4,000 characters of Markdown, with mentions that notify as in `content` (`allowed_mentions` applies). All the text displays of a message or a modal add up to 4,000 at most. |

### Thumbnail

| Field         | Type                      | Notes                             |
| ------------- | ------------------------- | --------------------------------- |
| `type`        | `11`                      | Only as a section’s `accessory`.  |
| `id`          | integer                   |                                   |
| `media`       | [media item](https://docs.klang.gg/bots/reference/components/#media-item) | Required.                         |
| `description` | string                    | Alt text, up to 1,024 characters. |
| `spoiler`     | boolean                   | Blurred until someone clicks it.  |

### Media gallery

| Field   | Type    | Notes                                                                                              |
| ------- | ------- | -------------------------------------------------------------------------------------------------- |
| `type`  | `12`    |                                                                                                    |
| `id`    | integer |                                                                                                    |
| `items` | array   | 1 to 10, each with `media` ([media item](https://docs.klang.gg/bots/reference/components/#media-item)), `description` (up to 1,024) and `spoiler`. |

### File

| Field     | Type                      | Notes                                                            |
| --------- | ------------------------- | ---------------------------------------------------------------- |
| `type`    | `13`                      |                                                                  |
| `id`      | integer                   |                                                                  |
| `file`    | [media item](https://docs.klang.gg/bots/reference/components/#media-item) | Only `attachment://` and the name of one of the message’s files. |
| `spoiler` | boolean                   |                                                                  |
| `name`    | string                    | Sent back: the file’s name.                                      |
| `size`    | integer                   | Sent back: the file’s size in bytes.                             |

### Separator

| Field     | Type    | Notes                                       |
| --------- | ------- | ------------------------------------------- |
| `type`    | `14`    |                                             |
| `id`      | integer |                                             |
| `divider` | boolean | Whether a line is drawn. `true` by default. |
| `spacing` | integer | `1` small (by default) or `2` large.        |

### Container

| Field          | Type    | Notes                                                                                             |
| -------------- | ------- | ------------------------------------------------------------------------------------------------- |
| `type`         | `17`    | Only at the top level of a message.                                                               |
| `id`           | integer |                                                                                                   |
| `components`   | array   | At least one: action rows, text displays, sections, media galleries, separators and files.        |
| `accent_color` | integer | An RGB color from `0` to `16777215` (`0xFFFFFF`), shown as a bar on the left. Without it, no bar. |
| `spoiler`      | boolean | The whole box is blurred until someone clicks it.                                                 |

### Media item

What `media` and `file` hold. Your bot sends only `url`:

- `attachment://` and the name of one of the message’s files, uploaded with it or kept from
  before (see [Sending files](https://docs.klang.gg/bots/files/#pointing-at-a-file-attachment)). A name that matches no
  file answers `50035`;
- or, for thumbnails and galleries, an `https` address on the Internet, up to 2,048 characters.
  Private addresses and Discord’s image servers are refused.

Klang fills in the rest when it sends the message back:

| Field             | Type    | Present   | Notes                                                                                                     |
| ----------------- | ------- | --------- | --------------------------------------------------------------------------------------------------------- |
| `url`             | string  | Always    | The file’s address, or the address you gave.                                                              |
| `proxy_url`       | string  | Sometimes | The same image through Klang’s image proxy for an address on the Internet; the file’s address for a file. |
| `content_type`    | string  | Sometimes | Files: the type Klang read from the file’s bytes.                                                         |
| `attachment_id`   | ID      | Sometimes | Files: the [attachment](https://docs.klang.gg/bots/reference/objects/#attachment).                                             |
| `width`, `height` | integer | Sometimes | Images among the files.                                                                                   |

A v2 message shows only the files its components name. Its other files are kept, and come in
`attachments`, but aren’t displayed.

## Modal components

### Label

| Field         | Type    | Notes                                                                                                                                                                                      |
| ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `type`        | `18`    |                                                                                                                                                                                            |
| `id`          | integer |                                                                                                                                                                                            |
| `label`       | string  | 1 to 45 characters.                                                                                                                                                                        |
| `description` | string  | Up to 100 characters, under the label.                                                                                                                                                     |
| `component`   | object  | A [text input](https://docs.klang.gg/bots/reference/components/#text-input), a [select menu](https://docs.klang.gg/bots/reference/components/#select-menus), a [file upload](https://docs.klang.gg/bots/reference/components/#file-upload), a [radio group](https://docs.klang.gg/bots/reference/components/#radio-group), a [checkbox group](https://docs.klang.gg/bots/reference/components/#checkbox-group) or a [checkbox](https://docs.klang.gg/bots/reference/components/#checkbox). |

### Text input

| Field         | Type    | Notes                                                                                                                 |
| ------------- | ------- | --------------------------------------------------------------------------------------------------------------------- |
| `type`        | `4`     |                                                                                                                       |
| `id`          | integer |                                                                                                                       |
| `custom_id`   | string  | 1 to 100 characters, unique in the modal.                                                                             |
| `style`       | integer | `1` one line, `2` a paragraph.                                                                                        |
| `min_length`  | integer | 0 to 4,000. Not more than `max_length`.                                                                               |
| `max_length`  | integer | 1 to 4,000.                                                                                                           |
| `required`    | boolean | `true` by default.                                                                                                    |
| `value`       | string  | Text already filled in, up to 4,000 characters and within `max_length`.                                               |
| `placeholder` | string  | Up to 100 characters.                                                                                                 |
| `label`       | string  | Only in the older form: an [action row](https://docs.klang.gg/bots/reference/components/#action-row) with one text input that carries its label (1 to 45 characters). |

### File upload

| Field        | Type             | Notes                                                                                 |
| ------------ | ---------------- | ------------------------------------------------------------------------------------- |
| `type`       | `19`             |                                                                                       |
| `id`         | integer          |                                                                                       |
| `custom_id`  | string           | 1 to 100 characters, unique in the modal.                                             |
| `min_values` | integer          | 0 to 10. `1` by default. Not more than `max_values`.                                  |
| `max_values` | integer          | 1 to 10. `1` by default.                                                              |
| `required`   | boolean          | `true` by default. A required upload needs `min_values` of at least 1.                |
| `file_types` | array of strings | Up to 10, without repeats: groups (`image`, `video`, `audio`) or extensions (`.pdf`). |

`file_types` filters by the file’s extension, whatever its case. An extension in the list is a
dot and 1 to 31 letters or digits, and Klang keeps it in lowercase. Without `file_types`, or with
an empty list, any file goes. The groups take these extensions:

| Group   | Extensions                                                 |
| ------- | ---------------------------------------------------------- |
| `image` | `.png`, `.gif`, `.jpg`, `.jpeg`, `.jfif`, `.webp`, `.avif` |
| `video` | `.mp4`, `.mov`, `.qt`, `.webm`                             |
| `audio` | `.mp3`, `.m4a`, `.wav`, `.ogg`, `.opus`, `.flac`           |

Klang also reads each file’s first bytes, whatever the list says: see
[What Klang checks for you](https://docs.klang.gg/bots/modals/#what-klang-checks-for-you). In the submission, the
upload has `values`, the IDs of the files, which are in `resolved.attachments`.

### Radio group

| Field       | Type    | Notes                                                                |
| ----------- | ------- | -------------------------------------------------------------------- |
| `type`      | `21`    |                                                                      |
| `id`        | integer |                                                                      |
| `custom_id` | string  | 1 to 100 characters, unique in the modal.                            |
| `options`   | array   | 2 to 10 [choice options](https://docs.klang.gg/bots/reference/components/#choice-option), with unique values.        |
| `required`  | boolean | `true` by default. One not required can be sent with nothing picked. |

One option at most can be `default`. In the submission, the group has `value`: the option picked,
or `null`.

### Checkbox group

| Field        | Type    | Notes                                                                                                     |
| ------------ | ------- | --------------------------------------------------------------------------------------------------------- |
| `type`       | `22`    |                                                                                                           |
| `id`         | integer |                                                                                                           |
| `custom_id`  | string  | 1 to 100 characters, unique in the modal.                                                                 |
| `options`    | array   | 1 to 10 [choice options](https://docs.klang.gg/bots/reference/components/#choice-option), with unique values.                                             |
| `min_values` | integer | 0 to 10. `1` by default. Not more than `max_values` or the number of options.                             |
| `max_values` | integer | 1 to 10. All the options by default.                                                                      |
| `required`   | boolean | `true` by default. A required group needs `min_values` of at least 1; one not required can be sent empty. |

Not more options can be `default` than `max_values`. In the submission, the group has `values`:
the options ticked, in their order.

### Checkbox

| Field       | Type    | Notes                                     |
| ----------- | ------- | ----------------------------------------- |
| `type`      | `23`    |                                           |
| `id`        | integer |                                           |
| `custom_id` | string  | 1 to 100 characters, unique in the modal. |
| `default`   | boolean | Ticked when the modal opens.              |

A checkbox is never required. In the submission, it has `value`: `true` or `false`.

### Choice option

The options of radio and checkbox groups. Unlike a select menu’s, they have no emoji.

| Field         | Type    | Notes                           |
| ------------- | ------- | ------------------------------- |
| `label`       | string  | 1 to 100 characters.            |
| `value`       | string  | 1 to 100 characters.            |
| `description` | string  | Up to 100 characters, under it. |
| `default`     | boolean | Picked or ticked when it opens. |

### The modal

| Field        | Type   | Notes                                                                             |
| ------------ | ------ | --------------------------------------------------------------------------------- |
| `custom_id`  | string | 1 to 100 characters. Comes back in the submission.                                |
| `title`      | string | 1 to 45 characters.                                                               |
| `components` | array  | 1 to 5: labels, text displays, or action rows with a text input (the older form). |

## Limits

| What                                         | Limit                                      |
| -------------------------------------------- | ------------------------------------------ |
| Action rows, without `IS_COMPONENTS_V2`      | 5                                          |
| Components, with `IS_COMPONENTS_V2`          | 40, counting the ones inside others        |
| Buttons per action row                       | 5, or one select menu                      |
| `custom_id`                                  | 1 to 100 characters, unique in the message |
| `id`                                         | 1 to 2,147,483,647, unique in the message  |
| Button `label`                               | 80 characters                              |
| Link button `url`                            | 512 characters                             |
| Options of a string select                   | 1 to 25                                    |
| Option `label`, `value` and `description`    | 100 characters each                        |
| `placeholder` of a select menu               | 150 characters                             |
| `min_values` / `max_values`                  | 0 to 25 / 1 to 25                          |
| Text display                                 | 4,000 characters, and 4,000 in all         |
| Text displays in a section                   | 1 to 3                                     |
| Items in a media gallery                     | 1 to 10                                    |
| Alt text (`description` of media)            | 1,024 characters                           |
| A media item’s `url`                         | 2,048 characters                           |
| Modal `title`                                | 45 characters                              |
| Components in a modal                        | 1 to 5                                     |
| Label `label` / `description`                | 45 / 100 characters                        |
| Text input value, `min_length`, `max_length` | 4,000 characters                           |
| Text input `placeholder`                     | 100 characters                             |
| Files in a file upload                       | 0 to 10                                    |
| `file_types` of a file upload                | Up to 10                                   |
| Options of a radio group                     | 2 to 10                                    |
| Options of a checkbox group                  | 1 to 10                                    |

## Errors

| Message in `errors`                                                            | Why                                                               |
| ------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
| `Up to 5 action rows without the IS_COMPONENTS_V2 flag.`                       | More than 5 rows in a message without the flag.                   |
| `Only action rows without the IS_COMPONENTS_V2 flag.`                          | A layout component in a message without the flag.                 |
| `Up to 40 components in all, the nested ones included.`                        | Too many components in a v2 message.                              |
| `Not allowed with the IS_COMPONENTS_V2 flag.`                                  | `content`, `embeds`, stickers or a poll in a v2 message.          |
| `The IS_COMPONENTS_V2 flag can’t be removed from a message.`                   | An edit with `flags` that drops the flag.                         |
| `Custom IDs must be unique.`, `IDs must be unique.`                            | Two components with the same `custom_id` or `id`.                 |
| `An action row holds up to 5 buttons or a single select.`                      | A select menu with something else in its row.                     |
| `No file of this message is called …`                                          | An `attachment://` name that matches none of the message’s files. |
| `Premium buttons aren’t supported.`                                            | A button with style `6`.                                          |
| `A radio group can have only one default option.`                              | Two options with `default` in a radio group.                      |
| `A required field needs min_values of at least 1.`                             | A required file upload or checkbox group with `min_values` `0`.   |
| `A file type is image, video, audio or a dot-prefixed extension such as .pdf.` | Something else in `file_types`.                                   |

A full example is in [Buttons, menus and layouts](https://docs.klang.gg/bots/components/#what-klang-checks-for-you).

What a person sends in a modal is checked too, but those problems never reach your bot: the
person sees them under the field. See
[What Klang checks for you](https://docs.klang.gg/bots/modals/#what-klang-checks-for-you).
