Skip to content

Commit cf66ab4

Browse files
Docs/components v2 :wires: (#3162)
* new pages :3 * fimished intro page * fimished interaction page * remove unused shit * I think we are done lmao * I lied, fixed some small mistakes * Update docs/guides/components_v2/interaction.md Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> * misha quality assurance :3 + breakings pages * Apply suggestions from code review Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> * component types guide expanded * :3 * Apply suggestions from code review Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> --------- Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
1 parent 80b4328 commit cf66ab4

20 files changed

+455
-5
lines changed

.github/ISSUE_TEMPLATE/bugreport.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ body:
1010
attributes:
1111
label: Check The Docs
1212
description: Please refer to our [FAQs](https://discordnet.dev/faq/basics/getting-started.html), [Documentation](https://discordnet.dev/api/index.html),
13-
and [Migration Guide](https://discordnet.dev/guides/v2_v3_guide/v2_to_v3_guide.html) before reporting issues.
13+
and [Migration Guide](https://discordnet.dev/guides/breakings/v2_to_v3_guide.html) before reporting issues.
1414
options:
1515
- label: "I double checked the docs and couldn't find any useful information."
1616
required: true

docs/guides/v2_v3_guide/v2_to_v3_guide.md renamed to docs/guides/breakings/v2_to_v3_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
uid: Guides.V2V3Guide
2+
uid: Guides.Breakings.V2V3Guide
33
title: V2 -> V3 Guide
44
---
55

docs/guides/breakings/v3.18.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
uid: Guides.Breakings.V3_18
3+
title: V3.18
4+
---
5+
6+
# V3.18
7+
8+
Among the changes in V3.18 was components V2, while splendid, it did bring with it a few changes you should be aware of.
9+
10+
- IMessage
11+
12+
Messages components type has changed. As a patch you can convert it to an `IEnumerable<ActionRowComponent>` again like so:
13+
```cs
14+
IMessage message = component.Message;
15+
IReadOnlyCollection<IMessageComponent>? components = message.Components;
16+
IEnumerable<ActionRowComponent>? componentsLegacy = components.OfType<ActionRowComponent>();
17+
```
18+
19+
- Nested components
20+
21+
The usage of components with `ActionRowBuilder` and `ComponentBuilder` has a type change as well. You can find examples in [Components_V2_Advanced].
22+
23+
- Components V2 Flag
24+
25+
To use Components V2 in a message (through `ModifyAsync`/`UpdateAsync`/...), a specific flag has to be set. This cannot be undone though :^). You only have to set it if you modify a message that didn't have components v2 in them to begin with (or if you already set the flag manually - obviously). This means that if you created a message with your bot that has components in it, the flag will automatically be set.
26+
27+
Otherwise, you can manually set it like so:
28+
29+
```cs
30+
MessageFlags? flags = component.Message.Flags ?? MessageFlags.None;
31+
flags = flags | MessageFlags.ComponentsV2;
32+
33+
await component.UpdateAsync(m =>
34+
{
35+
m.Flags = flags;
36+
m.Components = cv2builder.Build();
37+
});
38+
```
39+
40+
[Components_V2_Advanced]: xref:Guides.ComponentsV2.Advanced

docs/guides/components_v2/advanced.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
![](images/interaction-response.png)
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+
![](images/modal.png)
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+
![](images/updated-ingredients.png)
124+
125+
![](images/updated-oven.png)
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
362 KB
Loading
371 KB
Loading
367 KB
Loading
81.9 KB
Loading
105 KB
Loading
22.2 KB
Loading

0 commit comments

Comments
 (0)