Skip to content
This repository was archived by the owner on Oct 5, 2025. It is now read-only.

Commit a115a3d

Browse files
PakjeBecelJiralite
andauthored
Adding ComponentsV2 page (Popular Topics) (#1620)
Co-authored-by: Jiralite <[email protected]>
1 parent 1fa2480 commit a115a3d

File tree

9 files changed

+251
-0
lines changed

9 files changed

+251
-0
lines changed

guide/.vuepress/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export default {
9494
'/popular-topics/audit-logs.md',
9595
'/popular-topics/canvas.md',
9696
'/popular-topics/collectors.md',
97+
'/popular-topics/components-v2.md',
9798
'/popular-topics/embeds.md',
9899
'/popular-topics/errors.md',
99100
'/popular-topics/formatters.md',
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
# Components V2
2+
3+
While you might be familiar with [embeds](/popular-topics/embeds.md) in Discord, there are more ways to style and format your messages using **Components V2 (CV2)**, a comprehensive set of layout and content components.
4+
5+
To use the CV2 components, you need to pass in the `IsComponentsV2` message flag from the <DocsLink path="MessageFlags:Enum" /> enum when sending a message. The flag should only be added to the message's `flags` field when the message contains CV2 components, unlike the `Ephemeral` message flag that can be added when you defer an interaction response.
6+
7+
::: warning
8+
Once a message is sent or edited with the `IsComponentsV2` message flag, the flag **cannot** be removed from that message.
9+
:::
10+
11+
All components have an `id` field (which should not be confused with the `custom_id` field for interactive components), which is an optional 32-bit integer identifier for a component presented in a message. It is used to identify non-interactive components in the response from an interaction. More information about these can be found [here](https://discord.com/developers/docs/components/reference#anatomy-of-a-component).
12+
13+
In the following section, we will explain all CV2 components in detail, how they work together with interactive components, and the limitations Discord has set when using CV2 components in your message.
14+
15+
## Text Display
16+
17+
A Text Display is a content component for adding markdown-formatted text to your message. This component is very similar to the `content` field of a message, but by using multiple Text Display components, you gain greater control over the layout of your message. You can use the <DocsLink path="TextDisplayBuilder:Class" /> class to easily create a Text Display component.
18+
19+
::: tip
20+
In addition to the available markdown in Text Display components, you can also mention users and roles. In components, mentioned users and roles _will_ receive notifications, unlike when adding mentions to embeds. You can add the `allowedMentions` field to your message payload to control who will be notified.
21+
:::
22+
23+
The example below shows how you can send a Text Display component in a channel.
24+
25+
```js
26+
const { TextDisplayBuilder, MessageFlags } = require('discord.js');
27+
28+
const exampleTextDisplay = new TextDisplayBuilder()
29+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.');
30+
31+
await channel.send({
32+
components: [exampleTextDisplay],
33+
flags: MessageFlags.IsComponentsV2,
34+
});
35+
```
36+
37+
![Text display component preview](./images/textdisplay-preview.png)
38+
39+
## Section
40+
41+
A Section is a layout component that places between one and three left-aligned Text Display components next to a right-aligned accessory (Thumbnail or Button component). At least one Text Display and the accessory are required. You can use the <DocsLink path="SectionBuilder:Class" /> class to easily create a Section component.
42+
43+
The example below shows how you can send a Section component in a channel containing three Text Display components with a Button component on the right, next to the text.
44+
45+
```js
46+
const { SectionBuilder, ButtonStyle, MessageFlags } = require('discord.js');
47+
48+
const exampleSection = new SectionBuilder()
49+
.addTextDisplayComponents(
50+
textDisplay => textDisplay
51+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.'),
52+
textDisplay => textDisplay
53+
.setContent('Using a section, you may only use up to three Text Display components.'),
54+
textDisplay => textDisplay
55+
.setContent('And you can place one button or one thumbnail component next to it!'),
56+
)
57+
.setButtonAccessory(
58+
button => button
59+
.setCustomId('exampleButton')
60+
.setLabel('Button inside a Section')
61+
.setStyle(ButtonStyle.Primary),
62+
);
63+
64+
await channel.send({
65+
components: [exampleSection],
66+
flags: MessageFlags.IsComponentsV2,
67+
});
68+
```
69+
70+
![Section component preview](./images/section-preview.png)
71+
72+
## Thumbnail
73+
74+
A Thumbnail is a content component that is visually similar to the `thumbnail` field inside an embed. Thumbnails are added an accessory inside a [Section](/popular-topics/components-v2.md#section) component, support alt text for accessibility, and can be marked as a spoiler. You can use the <DocsLink path="ThumbnailBuilder:Class" /> class to easily create a Thumbnail component.
75+
76+
The example below shows how you can send a Thumbnail component as a Section component accessory in a channel.
77+
78+
```js
79+
const { AttachmentBuilder, SectionBuilder, MessageFlags } = require('discord.js');
80+
81+
const file = new AttachmentBuilder('../assets/image.png');
82+
83+
const exampleSection = new SectionBuilder()
84+
.addTextDisplayComponents(
85+
textDisplay => textDisplay
86+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.'),
87+
)
88+
.setThumbnailAccessory(
89+
thumbnail => thumbnail
90+
.setDescription('alt text displaying on the image')
91+
.setURL('attachment://image.png'), // Supports arbitrary URLs such as 'https://i.imgur.com/AfFp7pu.png' as well.
92+
);
93+
94+
await channel.send({
95+
components: [exampleSection],
96+
files: [file],
97+
flags: MessageFlags.IsComponentsV2,
98+
});
99+
```
100+
101+
![Thumbnail component preview](./images/thumbnail-preview.png)
102+
103+
For more information how to set up custom attachments to use in your Thumbnail component URL, you can look at the guide for [attaching images in embeds](/popular-topics/embeds.md#attaching-images).
104+
105+
## Media Gallery
106+
107+
A Media Gallery is a content component that can display up to 10 media attachments formatted in a structured gallery. Each attachment in the Media Gallery component can have an optional alt text (description) and can be marked as a spoiler. You can use the <DocsLink path="MediaGalleryBuilder:Class" /> and <DocsLink path="MediaGalleryItemBuilder:Class" /> classes to easily create a Media Gallery component and its items.
108+
109+
The example below shows how you can send a Media Gallery component in a channel.
110+
111+
```js
112+
const { AttachmentBuilder, MediaGalleryBuilder, MessageFlags } = require('discord.js');
113+
114+
const file = new AttachmentBuilder('../assets/image.png');
115+
116+
const exampleGallery = new MediaGalleryBuilder()
117+
.addItems(
118+
mediaGalleryItem => mediaGalleryItem
119+
.setDescription('alt text displaying on an image from the AttachmentBuilder')
120+
.setURL('attachment://image.png'),
121+
mediaGalleryItem => mediaGalleryItem
122+
.setDescription('alt text displaying on an image from an external URL')
123+
.setURL('https://i.imgur.com/AfFp7pu.png')
124+
.setSpoiler(true), // Will display as a blurred image
125+
);
126+
127+
await channel.send({
128+
components: [exampleGallery],
129+
files: [file],
130+
flags: MessageFlags.IsComponentsV2,
131+
});
132+
```
133+
134+
![Media gallery component preview](./images/mediagallery-preview.png)
135+
136+
## File
137+
138+
A File is a content component that can display a single uploaded file attachment within the body of the message. By using multiple File components, you can upload and display multiple files in a single message. File components cannot have alt text (description), unlike a Thumbnail or Media Gallery component, but can be marked as a spoiler. You can use the <DocsLink path="FileBuilder:Class" /> class to easily create a File component.
139+
140+
The example below shows how you can send a File component in a channel.
141+
142+
```js
143+
const { AttachmentBuilder, FileBuilder, MessageFlags } = require('discord.js');
144+
145+
const file = new AttachmentBuilder('../assets/guide.pdf');
146+
147+
const exampleFile = new FileBuilder()
148+
.setURL('attachment://guide.pdf');
149+
150+
await channel.send({
151+
components: [exampleFile],
152+
files: [file],
153+
flags: MessageFlags.IsComponentsV2,
154+
});
155+
```
156+
157+
![File component preview](./images/file-preview.png)
158+
159+
## Separator
160+
161+
A Separator is a layout component that adds vertical padding and optional visual division between components. You can select the amount of padding used for the Separator component (small or large) as well as whether a visual divider should be displayed (defaults to `true`). You can use the <DocsLink path="SeparatorBuilder:Class" /> class to easily create a Separator component.
162+
163+
::: warning
164+
When a Separator component is used without any non-Separator components in the message payload, the message will not have any visible content.
165+
:::
166+
167+
The example below shows how you can send a Separator component in a channel, separating two Text Display components.
168+
169+
```js
170+
const { TextDisplayBuilder, SeparatorBuilder, SeparatorSpacingSize, MessageFlags } = require('discord.js');
171+
172+
const exampleTextDisplay = new TextDisplayBuilder()
173+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.');
174+
175+
const exampleSeparator = new SeparatorBuilder()
176+
.setDivider(false) // No line displayed
177+
.setSpacing(SeparatorSpacingSize.Large);
178+
179+
await channel.send({
180+
components: [exampleTextDisplay, exampleSeparator, exampleTextDisplay],
181+
flags: MessageFlags.IsComponentsV2,
182+
});
183+
```
184+
185+
![Separator component preview](./images/separator-preview.png)
186+
187+
## Container
188+
189+
A Container is a layout component which groups its child components inside a visually distinct rounded box with an optional accent color on the left, just like embeds. However, unlike embeds, not specifying a color will make the left side of the Container component match the background color. You can also mark the Container component as a spoiler, which blurs all content inside the container. You can use the <DocsLink path="ContainerBuilder:Class" /> class to easily create a Container component.
190+
191+
The example below shows how to send a Container component in a channel. It contains:
192+
- a Text Display component;
193+
- an Action Row component with a User Select component;
194+
- a Separator component;
195+
- a Section component with two Text Display components and a Button component accessory.
196+
197+
```js
198+
const { ContainerBuilder, UserSelectMenuBuilder, ButtonStyle, MessageFlags } = require('discord.js');
199+
200+
const exampleContainer = new ContainerBuilder()
201+
.setAccentColor(0x0099FF)
202+
.addTextDisplayComponents(
203+
textDisplay => textDisplay
204+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.'),
205+
)
206+
.addActionRowComponents(
207+
actionRow => actionRow
208+
.setComponents(
209+
new UserSelectMenuBuilder()
210+
.setCustomId('exampleSelect')
211+
.setPlaceholder('Select users'),
212+
),
213+
)
214+
.addSeparatorComponents(
215+
separator => separator,
216+
)
217+
.addSectionComponents(
218+
section => section
219+
.addTextDisplayComponents(
220+
textDisplay => textDisplay
221+
.setContent('This text is inside a Text Display component! You can use **any __markdown__** available inside this component too.'),
222+
textDisplay => textDisplay
223+
.setContent('And you can place one button or one thumbnail component next to it!'),
224+
)
225+
.setButtonAccessory(
226+
button => button
227+
.setCustomId('exampleButton')
228+
.setLabel('Button inside a Section')
229+
.setStyle(ButtonStyle.Primary),
230+
),
231+
);
232+
233+
await channel.send({
234+
components: [exampleContainer],
235+
flags: MessageFlags.IsComponentsV2,
236+
});
237+
```
238+
239+
![Container component preview](./images/container-preview.png)
240+
241+
## Limitations
242+
243+
There are a few limits set by the Discord API to be aware of when using Components V2 in your messages. These limits are:
244+
245+
- To use any of the components listed under [New components](/popular-topics/components-v2.md#new-components) you must set the `IsComponentsV2` message flag in your message payload.
246+
- Setting the `IsComponentsV2` message flag will disable the `content` and `embeds` fields. You can use [Text Display](/popular-topics/components-v2.md#text-display) and [Container](/popular-topics/components-v2.md#container) components as replacements.
247+
- Setting the `IsComponentsV2` message flag will disable the `poll` and `stickers` fields, and there are no CV2 replacements for these fields you can use.
248+
- When upgrading a message to a CV2 flagged message by editing the message with the `IsComponentsV2` flag, all mentioned fields in the message object (`content`, `embeds`, `poll` and `stickers`) need to be set to `null`.
249+
- Attachments won't show by default, they must be set through the available media components ([Thumbnail](/popular-topics/components-v2.md#thumbnail), [Media Gallery](/popular-topics/components-v2.md#media-gallery) and [File](/popular-topics/components-v2.md#file)).
250+
- Messages allow up to 40 total components.
269 KB
Loading
16.6 KB
Loading
274 KB
Loading
253 KB
Loading
202 KB
Loading
113 KB
Loading
140 KB
Loading

0 commit comments

Comments
 (0)