Skip to content

Commit fb48e97

Browse files
feat: components v2 (#1381)
1 parent baeff20 commit fb48e97

File tree

8 files changed

+746
-92
lines changed

8 files changed

+746
-92
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <dpp/dpp.h>
2+
3+
int main() {
4+
dpp::cluster bot("token");
5+
6+
bot.on_log(dpp::utility::cout_logger());
7+
8+
bot.on_ready([&bot](const auto& event) {
9+
if (dpp::run_once<struct boot_t>()) {
10+
bot.global_bulk_command_create({ dpp::slashcommand("cats", "I love cats", bot.me.id) });
11+
}
12+
});
13+
14+
bot.on_button_click([](const dpp::button_click_t& event) {
15+
event.reply("You declared your love for cats by clicking button id: " + event.custom_id);
16+
});
17+
18+
/* This is a detailed example of using many different types of component. For a complete
19+
* list of supported components, see the Discord developer documentation and the definition
20+
* of dpp::component_type.
21+
*/
22+
bot.register_command("cats", [](const dpp::slashcommand_t& e) {
23+
e.reply(dpp::message()
24+
/* Remember to set the message flag for components v2 */
25+
.set_flags(dpp::m_using_components_v2).add_component_v2(
26+
/* Reply with a container... */
27+
dpp::component()
28+
.set_type(dpp::cot_container)
29+
.set_accent(dpp::utility::rgb(255, 0, 0))
30+
.set_spoiler(true)
31+
.add_component_v2(
32+
/* ...which contains a section... */
33+
dpp::component()
34+
.set_type(dpp::cot_section)
35+
.add_component_v2(
36+
/* ...with text... */
37+
dpp::component()
38+
.set_type(dpp::cot_text_display)
39+
.set_content("Click if you love cats")
40+
)
41+
.set_accessory(
42+
/* ...and an accessory button to the right */
43+
dpp::component()
44+
.set_type(dpp::cot_button)
45+
.set_label("Click me")
46+
.set_style(dpp::cos_danger)
47+
.set_id("button")
48+
)
49+
)
50+
).add_component_v2(
51+
/* ... with a large visible divider between... */
52+
dpp::component()
53+
.set_type(dpp::cot_separator)
54+
.set_spacing(dpp::sep_large)
55+
.set_divider(true)
56+
).add_component_v2(
57+
/* ... followed by a media gallery... */
58+
dpp::component()
59+
.set_type(dpp::cot_media_gallery)
60+
.add_media_gallery_item(
61+
/* ...containing one cat pic (obviously) */
62+
dpp::component()
63+
.set_type(dpp::cot_thumbnail)
64+
.set_description("A cat")
65+
.set_thumbnail("https://www.catster.com/wp-content/uploads/2023/11/Beluga-Cat-e1714190563227.webp")
66+
)
67+
));
68+
});
69+
70+
bot.start(dpp::st_wait);
71+
}

docpages/example_programs/interactions_and_components/components-menu.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Components are anything that can be attached to a message or a \ref modal-dialog
77
* \subpage components3
88
* \subpage default_select_value
99
* \subpage editing_message_after_click
10+
* \subpage components_v2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
\page components_v2 Components V2
2+
3+
From March 2025 onwards Discord have released a **new way to handle components** in a Discord application/bot. The previous methods of working with components remain, and are accessible without
4+
any changes in D++. If you want to use the new style of components you may do so, which gives far greater control over how message containing images, buttons, sections etc are formatted.
5+
6+
Components are attached to any message or interaction reply, via the dpp::message::add_component() or dpp::message::add_component_v2() function. You must also be sure to set the flag
7+
dpp::m_using_components_v2 on the message to allow the new features.
8+
9+
When using components v2, the following limits apply which do not apply with components v1 or traditional embeds:
10+
11+
* Setting the message `content` or `embeds` will not be allowed (components v2 replaces the functionality)
12+
* You can have a maximum of 10 top level components per message. The maximum number of nested components is 30.
13+
* Audio files are not supported at present
14+
* Text preview for text/plain files is not supported
15+
* URLs will not have embeds generated for them
16+
* The total length of the entire message, including all components within and the content of those components, is 4000 UTF-8 characters.
17+
18+
Here is a detailed example of how to create components v2 replies.
19+
20+
\warning Please note that where you would use add_component() previously, you **should** use add_component_v2() (on component or message objects). This is because the v2 version will not automatically add action rows, which is a limitation which has been removed in version 2 but is still required for version 1.
21+
22+
\include{cpp} components_v2.cpp
23+
24+
There are many new component types, for a complete list see the definition of dpp::component_type
25+
26+
If you run the example program above, you will be shown a message containing your components:
27+
28+
\image html components.gif

docpages/images/components.gif

2.93 MB
Loading

0 commit comments

Comments
 (0)