|
| 1 | +--- |
| 2 | +uid: Guides.ComponentsV2.Advanced |
| 3 | +title: Create Components V2 |
| 4 | +--- |
| 5 | + |
| 6 | +# Component types |
| 7 | + |
| 8 | +As denoted in [Intro], This framework supports a lot of component types. |
| 9 | + |
| 10 | +A full list of components available: |
| 11 | + |
| 12 | +| Type | Name | Description | Components V2 only (flag) |
| 13 | +|------|---------------------|-------------------------------------------------------------------|---------------------------| |
| 14 | +| 1 | Action Row | Container to display a row of interactive components | No | |
| 15 | +| 2 | Button | Button object | No | |
| 16 | +| 3 | String Select | Select menu for picking from defined text options | No | |
| 17 | +| 4 | Text Input | Text input object | No | |
| 18 | +| 5 | User Select | Select menu for users | No | |
| 19 | +| 6 | Role Select | Select menu for roles | No | |
| 20 | +| 7 | Mentionable Select | Select menu for mentionables (users and roles) | No | |
| 21 | +| 8 | Channel Select | Select menu for channels | No | |
| 22 | +| 9 | Section | Container to display text alongside an accessory component | Yes | |
| 23 | +| 10 | Text Display | Markdown text | Yes | |
| 24 | +| 11 | Thumbnail | Small image that can be used as an accessory | Yes | |
| 25 | +| 12 | Media Gallery | Display images and other media | Yes | |
| 26 | +| 13 | File | Displays an attached file | Yes | |
| 27 | +| 14 | Separator | Component to add vertical padding between other components | Yes | |
| 28 | +| 17 | Container | Container that visually groups a set of components | Yes |
| 29 | + |
| 30 | +All components have an id field (generated by default) and must be unique within the message. Generation of ids won't use another id that exists in the message if you have one defined for another component. Sending components with an id of `0` is allowed but will be treated as empty and replaced by the API. You may want to manually assign one if you want to later identify a component this way. Some (interactive) components can be identified using a `customId` instead, which is a unique string (max 100 chars). |
| 31 | + |
| 32 | +**[Action Row](https://discord.com/developers/docs/components/reference#action-row)** |
| 33 | + |
| 34 | +The `ActionRow` component is a parent that contains either |
| 35 | +- 1 to 5 `Buttons` |
| 36 | +- 1 `TextInput` |
| 37 | +- 1 of (`StringSelect`, `UserSelect`, `RoleSelect`, `MentionableSelect`, `ChannelSelect`) |
| 38 | + |
| 39 | +**[Button](https://discord.com/developers/docs/components/reference#button)** |
| 40 | + |
| 41 | +- Must be inside an `ActionRow` or in the accessory field of a `Section` |
| 42 | +- Non-link and non-premium buttons must have a custom_id, and cannot have a url or a sku_id. |
| 43 | +- Link buttons must have a url, and cannot have a custom_id |
| 44 | +- Link buttons do not send an interaction to your app when clicked |
| 45 | +- Premium buttons must contain a sku_id, and cannot have a custom_id, label, url, or emoji. |
| 46 | +- Premium buttons do not send an interaction to your app when clicked |
| 47 | + |
| 48 | +**[String Select](https://discord.com/developers/docs/components/reference#string-select)** (also other Select variants) |
| 49 | +- Must be inside an `ActionRow` |
| 50 | +- 1 to 25 `options` |
| 51 | +- Placeholder (if specified, up to 150 chars) |
| 52 | + |
| 53 | +It supports multi-select. If you want to use this functionality, you must set `min_values` (0 - 25) and `max_values` (1 - 25). |
| 54 | + |
| 55 | +For all select options: |
| 56 | +- Label, value and optionally the description may not exceed `100` chars |
| 57 | + |
| 58 | +**[Text Input](https://discord.com/developers/docs/components/reference#text-input)** |
| 59 | +- Must be inside an `ActionRow` in a `Modal` |
| 60 | +- Label has a max length of `45` |
| 61 | + |
| 62 | +By default it will accept up to `4000` characters (this is also the maximum). You can change this using the `min_values` and `max_values` |
| 63 | + |
| 64 | +**[Section](https://discord.com/developers/docs/components/reference#section)** |
| 65 | + |
| 66 | +A parent component, this allows you to put `TextDisplays` (1 - 3) next to eachother. It includes an accessory field, which can be used to display a `Thumbnail` or `Button`. |
| 67 | + |
| 68 | +**[Text Display](https://discord.com/developers/docs/components/reference#text-display)** |
| 69 | + |
| 70 | +Just text lol. (With markdown support, just like a regular user; up to 4000 characters) |
| 71 | + |
| 72 | +**[Thumbnail](https://discord.com/developers/docs/components/reference#thumbnail)** |
| 73 | + |
| 74 | +A small image (to use in a `Section`). |
| 75 | +- Optionally has a description of max `1024` chars |
| 76 | + |
| 77 | +**[Media Gallery](https://discord.com/developers/docs/components/reference#media-gallery)** |
| 78 | + |
| 79 | +A component used to display up to `10` images. |
| 80 | +- Each image can have a description of max `1024` chars |
| 81 | + |
| 82 | +**[File](https://discord.com/developers/docs/components/reference#file)** |
| 83 | + |
| 84 | +Used to send a single file. Only supports the `attachment://` protocol. |
| 85 | + |
| 86 | +**[Separator](https://discord.com/developers/docs/components/reference#separator)** |
| 87 | + |
| 88 | +Just something to put space between components (Y axis). |
| 89 | +- Spacing can be set to either `1` (small space) or `2` (large space) |
| 90 | +- Visibility can be toggled too. (`IsDivider`) |
| 91 | + |
| 92 | +**[Container](https://discord.com/developers/docs/components/reference#container)** |
| 93 | + |
| 94 | +A parent component with a side bar of customisable colour (like embeds). |
| 95 | + |
| 96 | +In Discord.NET, you typically use these components in conjunction with a `ComponentBuilderV2`. The V2 specific components can be added to the builder using the `WithX` fluent/chain methods whereas the other supported components are mostly children of `ActionRows` and can be added as a component array like used below. You need to know what components can be added to which component though to prevent errors (this is why the above sections exist). If your component structure is wrong, Discord.NET will throw an exception. |
| 97 | + |
| 98 | +This example offers some more insight on how to use them. Below is a component with `TextDisplay`, `MediaGallery` and `ActionRow` (with `Buttons` or `SelectMenu`). |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +## Code |
| 103 | + |
| 104 | +Some code will not be included here as it is not relevant to this framework. If you want to see the full code, it is [here](https://github.com/Adrigorithm/Adribot/). |
| 105 | + |
| 106 | +The main component container generation method: |
| 107 | +[!code-csharp[ComponentBuilderV2 Sample](samples/component.cs)] |
| 108 | + |
| 109 | +## Interactions |
| 110 | + |
| 111 | +The button triggers the following modal |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +[!code-csharp[Modal Sample](samples/recipe-servings-modal.cs)] |
| 116 | + |
| 117 | +Interactions used by this message: |
| 118 | + |
| 119 | +[!code-csharp[Interaction Sample](samples/recipe-interactions.cs)] |
| 120 | + |
| 121 | +After the **SET SERVINGS** modal is submitted (or the COMBOXBOX is changed) the UI is updated: |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## Troubleshooting |
| 128 | + |
| 129 | +### Common issues |
| 130 | +- Your interaction may not work if you use components v2 using `ModifyAsync`/`UpdateAsync`/..., if that is the case you must set the MessageFlags.ComponentsV2 flag on the message as mentioned in [CV2_Flag]. |
| 131 | +- Currently there may be some rare instances where the flag may not be set even when it should, as per [CV2_Flag]. An example is when more than 5 action rows are used. This might get fixed later though. You should set it manually in this case (You can identify this using method described below). |
| 132 | + |
| 133 | +If you run into any trouble (appliction not responding when sending components), a debugger is (like usually) a useful tool to have at your disposal. More specifically within this context: dnet will do some checks before sending the component configuration to discord, so on building the component array, you can check for errors thrown. If this does not help, setting a breakpoint on the line that sends your component to discord (ModifyAsync/RespondAsync/...) and stepping to the next line (within 3 seconds of triggering it) may yield a more specific error returned by Discord itself. |
| 134 | + |
| 135 | +[Intro]: xref:Guides.ComponentsV2.Intro |
| 136 | +[CV2_Flag]: xref:Guides.Breakings.V3_18 |
0 commit comments