Skip to content

Commit 656bfb8

Browse files
authored
Updated out-dated doc pages to now use slashcommands. (#771)
1 parent 4f1e4ee commit 656bfb8

21 files changed

+806
-393
lines changed

docpages/advanced_reference/coding_style_standards.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,28 @@ Enums and their values should be `snake_case` as with class, function and method
1010

1111

1212
## Curly Braces, Brackets etc
13+
This covers your standard Curly Braces (commonly known as squiggly brackets), and Lists.
1314

14-
Open curly braces on the same line as the keyword, for example:
15+
### Curly Braces
16+
Curly Braces should be on the same line as the keyword, for example:
1517

1618
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
17-
if (a == b) {
18-
c();
19+
void foo() {
20+
if (a == b) {
21+
c();
22+
}
23+
24+
while(true) {
25+
// ...
26+
}
1927
}
2028
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2129

22-
Use a space after the comma in parameter lists, and after opening brackets and before closing brackets except when calling a function, e.g.:
30+
This applies to functions, `while` statements, `if` statments, lambdas, nearly anything that uses curly braces with statements!
31+
32+
### Lists
33+
34+
Lists should have a space after the comma in parameter lists, and after opening brackets and before closing brackets except when calling a function, for example:
2335

2436
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
2537
std::vector<std::string> clowns = { "pennywise", "bobo" };

docpages/example_programs/interactions_and_components/components.md

+30-15
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,53 @@ D++ as an on_button_click event. To make use of this, use code as in this exampl
1010
1111
int main() {
1212
13-
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
13+
dpp::cluster bot("token");
1414
1515
bot.on_log(dpp::utility::cout_logger());
1616
17-
/* Message handler to look for a command called !button */
18-
bot.on_message_create([&bot](const dpp::message_create_t & event) {
19-
if (event.msg.content == "!button") {
20-
/* Create a message containing an action row, and a button within the action row. */
21-
bot.message_create(
22-
dpp::message(event.msg.channel_id, "this text has buttons").add_component(
23-
dpp::component().add_component(
24-
dpp::component().set_label("Click me!").
25-
set_type(dpp::cot_button).
26-
set_emoji(u8"😄").
27-
set_style(dpp::cos_danger).
28-
set_id("myid")
29-
)
17+
/* The event is fired when someone issues your commands */
18+
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
19+
/* Check which command they ran */
20+
if (event.command.get_command_name == "button") {
21+
22+
/* Create a message */
23+
dpp::message msg(event.command.channel_id, "this text has a button");
24+
25+
/* Add an action row, and then a button within the action row. */
26+
msg.add_component(
27+
dpp::component().add_component(
28+
dpp::component().
29+
set_label("Click me!").
30+
set_type(dpp::cot_button).
31+
set_emoji(u8"😄").
32+
set_style(dpp::cos_danger).
33+
set_id("myid")
3034
)
3135
);
36+
37+
/* Reply to the user with our message. */
38+
event.reply(msg);
3239
}
3340
});
3441
3542
/* When a user clicks your button, the on_button_click event will fire,
3643
* containing the custom_id you defined in your button.
3744
*/
38-
bot.on_button_click([&bot](const dpp::button_click_t & event) {
45+
bot.on_button_click([&bot](const dpp::button_click_t& event) {
3946
/* Button clicks are still interactions, and must be replied to in some form to
4047
* prevent the "this interaction has failed" message from Discord to the user.
4148
*/
4249
event.reply("You clicked: " + event.custom_id);
4350
});
4451
52+
bot.on_ready([&bot](const dpp::ready_t& event) {
53+
if (dpp::run_once<struct register_bot_commands>()) {
54+
55+
/* Create and register a command when the bot is ready */
56+
bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
57+
}
58+
});
59+
4560
bot.start(dpp::st_wait);
4661
4762
return 0;

docpages/example_programs/interactions_and_components/components2.md

+46-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
\page components2 Advanced components
1+
\page components2 Advanced button components
22

3-
This example demonstrates receiving button clicks and sending response messages.
3+
This example demonstrates adding multiple buttons, receiving button clicks and sending response messages.
44

55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
66
#include <dpp/dpp.h>
@@ -9,39 +9,58 @@ using json = nlohmann::json;
99
1010
int main() {
1111
12-
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); // Privileged intent required to receive message content
12+
dpp::cluster bot("token");
1313
14-
bot.on_log(dpp::utility::cout_logger());
14+
bot.on_log(dpp::utility::cout_logger());
15+
16+
/* The event is fired when someone issues your commands */
17+
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
18+
/* Check which command they ran */
19+
if (event.command.get_command_name == "math") {
20+
21+
/* Create a message */
22+
dpp::message msg(event.command.channel_id, "What is 5+5?");
23+
24+
/* Add an action row, and then 3 buttons within the action row. */
25+
msg.add_component(
26+
dpp::component().add_component(
27+
dpp::component().
28+
set_label("9").
29+
set_style(dpp::cos_primary).
30+
set_id("9")
31+
).add_component(
32+
dpp::component().
33+
set_label("10").
34+
set_style(dpp::cos_primary).
35+
set_id("10")
36+
).add_component(
37+
dpp::component().
38+
set_label("11").
39+
set_style(dpp::cos_primary).
40+
set_id("11")
41+
)
42+
);
43+
44+
/* Reply to the user with our message. */
45+
event.reply(msg);
46+
}
47+
});
1548
1649
bot.on_button_click([&bot](const dpp::button_click_t & event) {
1750
if (event.custom_id == "10") {
18-
event.reply(dpp::message("Correct").set_flags(dpp::m_ephemeral));
51+
event.reply(dpp::message("You got it right!").set_flags(dpp::m_ephemeral));
1952
} else {
20-
event.reply(dpp::message("Incorrect").set_flags(dpp::m_ephemeral));
53+
event.reply(dpp::message("Wrong! Try again.").set_flags(dpp::m_ephemeral));
2154
}
2255
});
2356
24-
bot.on_message_create([&bot](const dpp::message_create_t & event) {
25-
if (event.msg.content == "!ping2") {
26-
bot.message_create(
27-
dpp::message(event.msg.channel_id, "What is 5+5?").add_component(
28-
dpp::component().add_component(
29-
dpp::component().set_label("9").
30-
set_style(dpp::cos_primary).
31-
set_id("9")
32-
).add_component(
33-
dpp::component().set_label("10").
34-
set_style(dpp::cos_primary).
35-
set_id("10")
36-
).add_component(
37-
dpp::component().set_label("11").
38-
set_style(dpp::cos_primary).
39-
set_id("11")
40-
)
41-
)
42-
);
43-
}
44-
});
57+
bot.on_ready([&bot](const dpp::ready_t& event) {
58+
if (dpp::run_once<struct register_bot_commands>()) {
59+
60+
/* Create and register a command when the bot is ready */
61+
bot.global_command_create(dpp::slashcommand("math", "A quick maths question!", bot.me.id));
62+
}
63+
});
4564
4665
bot.start(dpp::st_wait);
4766

docpages/example_programs/interactions_and_components/components3.md

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
\page components3 Using select menu components
22

3-
This example demonstrates receiving select menu clicks and sending response messages.
3+
This example demonstrates creating a select menu, receiving select menu clicks and sending a response message.
44

55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
66
#include <dpp/dpp.h>
@@ -9,27 +9,35 @@ using json = nlohmann::json;
99
1010
int main() {
1111
12-
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
12+
dpp::cluster bot("token");
1313
14-
bot.on_log(dpp::utility::cout_logger());
14+
bot.on_log(dpp::utility::cout_logger());
1515
16-
/* Message handler to look for a command called !select */
17-
bot.on_message_create([&bot](const dpp::message_create_t & event) {
18-
if (event.msg.content == "!select") {
19-
/* Create a message containing an action row, and a select menu within the action row. */
20-
dpp::message m(event.msg.channel_id, "this text has a select menu");
21-
m.add_component(
16+
/* The event is fired when someone issues your commands */
17+
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
18+
/* Check which command they ran */
19+
if (event.command.get_command_name == "select") {
20+
21+
/* Create a message */
22+
dpp::message msg(event.command.channel_id, "This text has a select menu!");
23+
24+
/* Add an action row, and a select menu within the action row. */
25+
msg.add_component(
2226
dpp::component().add_component(
23-
dpp::component().set_type(dpp::cot_selectmenu).
27+
dpp::component().
28+
set_type(dpp::cot_selectmenu).
2429
set_placeholder("Pick something").
2530
add_select_option(dpp::select_option("label1","value1","description1").set_emoji(u8"😄")).
2631
add_select_option(dpp::select_option("label2","value2","description2").set_emoji(u8"🙂")).
27-
set_id("myselid")
32+
set_id("myselectid")
2833
)
2934
);
30-
bot.message_create(m);
35+
36+
/* Reply to the user with our message. */
37+
event.reply(msg);
3138
}
3239
});
40+
3341
/* When a user clicks your select menu , the on_select_click event will fire,
3442
* containing the custom_id you defined in your select menu.
3543
*/
@@ -40,6 +48,14 @@ int main() {
4048
event.reply("You clicked " + event.custom_id + " and chose: " + event.values[0]);
4149
});
4250
51+
bot.on_ready([&bot](const dpp::ready_t& event) {
52+
if (dpp::run_once<struct register_bot_commands>()) {
53+
54+
/* Create and register a command when the bot is ready */
55+
bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id));
56+
}
57+
});
58+
4359
bot.start(dpp::st_wait);
4460
4561
return 0;

docpages/example_programs/interactions_and_components/context_menus.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Context menus are application commands that appear on the context menu (right cl
44

55
\image html context_menu_user_command.png
66

7-
The following example shows how to create and handle **user context menus**.
7+
\note This example sets the command as the type dpp::ctxm_user which can only be used by right clicking on a user. To make it appear on a message, you'll want to switch the type to dpp::ctxm_message.
8+
9+
The following example shows how to create and handle **user context menus** for message context menus, read the notice above.
810

911
~~~~~~~~~~{.cpp}
1012
#include <dpp/dpp.h>
@@ -18,21 +20,21 @@ int main()
1820
1921
bot.on_ready([&bot](const dpp::ready_t &event) {
2022
if (dpp::run_once<struct register_bot_commands>()) {
23+
24+
/* Create the command */
25+
dpp::slashcommand command("High Five", "Send a High Five!", bot.me.id);
26+
27+
command.set_type(dpp::ctxm_user);
28+
2129
/* Register the command */
22-
bot.guild_command_create(
23-
dpp::slashcommand()
24-
.set_type(dpp::ctxm_user)
25-
.set_name("High Five")
26-
.set_application_id(bot.me.id),
27-
857692897221033129 // you need to put your guild-id in here
28-
);
30+
bot.guild_command_create(command, guild_id);
2931
}
3032
});
3133
3234
/* Use the on_user_context_menu event to look for user context menu actions */
33-
bot.on_user_context_menu([&](const dpp::user_context_menu_t &event) {
35+
bot.on_user_context_menu([&](const dpp::user_context_menu_t& event) {
3436
/* check if the context menu name is High Five */
35-
if (event.command.get_command_name() == "High Five") {
37+
if (event.command.get_command_name() == "high five") {
3638
dpp::user user = event.get_user(); // the user who the command has been issued on
3739
dpp::user author = event.command.get_issuing_user(); // the user who clicked on the context menu
3840
event.reply(author.get_mention() + " slapped " + user.get_mention());

docpages/example_programs/interactions_and_components/modal_dialog_interactions.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@ int main(int argc, char const *argv[])
1212
{
1313
dpp::cluster bot("token");
1414
15-
bot.on_log(dpp::utility::cout_logger());
15+
bot.on_log(dpp::utility::cout_logger());
1616
17-
bot.on_ready([&](const dpp::ready_t & event) {
18-
if (dpp::run_once<struct register_bot_commands>()) {
19-
/* Create a slash command and register it as a global command */
20-
bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));
21-
}
22-
});
23-
24-
bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
17+
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
2518
/* Check for our /dialog command */
2619
if (event.command.get_command_name() == "dialog") {
20+
2721
/* Instantiate an interaction_modal_response object */
2822
dpp::interaction_modal_response modal("my_modal", "Please enter stuff");
23+
2924
/* Add a text component */
3025
modal.add_component(
3126
dpp::component().
@@ -37,6 +32,7 @@ int main(int argc, char const *argv[])
3732
set_max_length(50).
3833
set_text_style(dpp::text_short)
3934
);
35+
4036
/* Add another text component in the next row, as required by Discord */
4137
modal.add_row();
4238
modal.add_component(
@@ -49,24 +45,36 @@ int main(int argc, char const *argv[])
4945
set_max_length(2000).
5046
set_text_style(dpp::text_paragraph)
5147
);
48+
5249
/* Trigger the dialog box. All dialog boxes are ephemeral */
5350
event.dialog(modal);
5451
}
5552
});
5653
5754
/* This event handles form submission for the modal dialog we create above */
5855
bot.on_form_submit([&](const dpp::form_submit_t & event) {
56+
5957
/* For this simple example we know the first element of the first row ([0][0]) is value type string.
6058
* In the real world it may not be safe to make such assumptions!
6159
*/
6260
std::string v = std::get<std::string>(event.components[0].components[0].value);
61+
6362
dpp::message m;
6463
m.set_content("You entered: " + v).set_flags(dpp::m_ephemeral);
64+
6565
/* Emit a reply. Form submission is still an interaction and must generate some form of reply! */
6666
event.reply(m);
6767
});
6868
69+
bot.on_ready([&](const dpp::ready_t & event) {
70+
if (dpp::run_once<struct register_bot_commands>()) {
71+
/* Create a slash command and register it as a global command */
72+
bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));
73+
}
74+
});
75+
6976
/* Start bot */
77+
7078
bot.start(dpp::st_wait);
7179
return 0;
7280
}

0 commit comments

Comments
 (0)