Skip to content

Commit

Permalink
Updated out-dated doc pages to now use slashcommands. (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 authored Aug 13, 2023
1 parent 4f1e4ee commit 656bfb8
Show file tree
Hide file tree
Showing 21 changed files with 806 additions and 393 deletions.
20 changes: 16 additions & 4 deletions docpages/advanced_reference/coding_style_standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@ Enums and their values should be `snake_case` as with class, function and method


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

Open curly braces on the same line as the keyword, for example:
### Curly Braces
Curly Braces should be on the same line as the keyword, for example:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
if (a == b) {
c();
void foo() {
if (a == b) {
c();
}
while(true) {
// ...
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Use a space after the comma in parameter lists, and after opening brackets and before closing brackets except when calling a function, e.g.:
This applies to functions, `while` statements, `if` statments, lambdas, nearly anything that uses curly braces with statements!

### Lists

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:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
std::vector<std::string> clowns = { "pennywise", "bobo" };
Expand Down
45 changes: 30 additions & 15 deletions docpages/example_programs/interactions_and_components/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,53 @@ D++ as an on_button_click event. To make use of this, use code as in this exampl
int main() {
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* Message handler to look for a command called !button */
bot.on_message_create([&bot](const dpp::message_create_t & event) {
if (event.msg.content == "!button") {
/* Create a message containing an action row, and a button within the action row. */
bot.message_create(
dpp::message(event.msg.channel_id, "this text has buttons").add_component(
dpp::component().add_component(
dpp::component().set_label("Click me!").
set_type(dpp::cot_button).
set_emoji(u8"😄").
set_style(dpp::cos_danger).
set_id("myid")
)
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name == "button") {
/* Create a message */
dpp::message msg(event.command.channel_id, "this text has a button");
/* Add an action row, and then a button within the action row. */
msg.add_component(
dpp::component().add_component(
dpp::component().
set_label("Click me!").
set_type(dpp::cot_button).
set_emoji(u8"😄").
set_style(dpp::cos_danger).
set_id("myid")
)
);
/* Reply to the user with our message. */
event.reply(msg);
}
});
/* When a user clicks your button, the on_button_click event will fire,
* containing the custom_id you defined in your button.
*/
bot.on_button_click([&bot](const dpp::button_click_t & event) {
bot.on_button_click([&bot](const dpp::button_click_t& event) {
/* Button clicks are still interactions, and must be replied to in some form to
* prevent the "this interaction has failed" message from Discord to the user.
*/
event.reply("You clicked: " + event.custom_id);
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\page components2 Advanced components
\page components2 Advanced button components

This example demonstrates receiving button clicks and sending response messages.
This example demonstrates adding multiple buttons, receiving button clicks and sending response messages.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
#include <dpp/dpp.h>
Expand All @@ -9,39 +9,58 @@ using json = nlohmann::json;
int main() {
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); // Privileged intent required to receive message content
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name == "math") {
/* Create a message */
dpp::message msg(event.command.channel_id, "What is 5+5?");
/* Add an action row, and then 3 buttons within the action row. */
msg.add_component(
dpp::component().add_component(
dpp::component().
set_label("9").
set_style(dpp::cos_primary).
set_id("9")
).add_component(
dpp::component().
set_label("10").
set_style(dpp::cos_primary).
set_id("10")
).add_component(
dpp::component().
set_label("11").
set_style(dpp::cos_primary).
set_id("11")
)
);
/* Reply to the user with our message. */
event.reply(msg);
}
});
bot.on_button_click([&bot](const dpp::button_click_t & event) {
if (event.custom_id == "10") {
event.reply(dpp::message("Correct").set_flags(dpp::m_ephemeral));
event.reply(dpp::message("You got it right!").set_flags(dpp::m_ephemeral));
} else {
event.reply(dpp::message("Incorrect").set_flags(dpp::m_ephemeral));
event.reply(dpp::message("Wrong! Try again.").set_flags(dpp::m_ephemeral));
}
});
bot.on_message_create([&bot](const dpp::message_create_t & event) {
if (event.msg.content == "!ping2") {
bot.message_create(
dpp::message(event.msg.channel_id, "What is 5+5?").add_component(
dpp::component().add_component(
dpp::component().set_label("9").
set_style(dpp::cos_primary).
set_id("9")
).add_component(
dpp::component().set_label("10").
set_style(dpp::cos_primary).
set_id("10")
).add_component(
dpp::component().set_label("11").
set_style(dpp::cos_primary).
set_id("11")
)
)
);
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("math", "A quick maths question!", bot.me.id));
}
});
bot.start(dpp::st_wait);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\page components3 Using select menu components

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
#include <dpp/dpp.h>
Expand All @@ -9,27 +9,35 @@ using json = nlohmann::json;
int main() {
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
bot.on_log(dpp::utility::cout_logger());
/* Message handler to look for a command called !select */
bot.on_message_create([&bot](const dpp::message_create_t & event) {
if (event.msg.content == "!select") {
/* Create a message containing an action row, and a select menu within the action row. */
dpp::message m(event.msg.channel_id, "this text has a select menu");
m.add_component(
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name == "select") {
/* Create a message */
dpp::message msg(event.command.channel_id, "This text has a select menu!");
/* Add an action row, and a select menu within the action row. */
msg.add_component(
dpp::component().add_component(
dpp::component().set_type(dpp::cot_selectmenu).
dpp::component().
set_type(dpp::cot_selectmenu).
set_placeholder("Pick something").
add_select_option(dpp::select_option("label1","value1","description1").set_emoji(u8"😄")).
add_select_option(dpp::select_option("label2","value2","description2").set_emoji(u8"🙂")).
set_id("myselid")
set_id("myselectid")
)
);
bot.message_create(m);
/* Reply to the user with our message. */
event.reply(msg);
}
});
/* When a user clicks your select menu , the on_select_click event will fire,
* containing the custom_id you defined in your select menu.
*/
Expand All @@ -40,6 +48,14 @@ int main() {
event.reply("You clicked " + event.custom_id + " and chose: " + event.values[0]);
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Context menus are application commands that appear on the context menu (right cl

\image html context_menu_user_command.png

The following example shows how to create and handle **user context menus**.
\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.

The following example shows how to create and handle **user context menus** for message context menus, read the notice above.

~~~~~~~~~~{.cpp}
#include <dpp/dpp.h>
Expand All @@ -18,21 +20,21 @@ int main()
bot.on_ready([&bot](const dpp::ready_t &event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create the command */
dpp::slashcommand command("High Five", "Send a High Five!", bot.me.id);
command.set_type(dpp::ctxm_user);
/* Register the command */
bot.guild_command_create(
dpp::slashcommand()
.set_type(dpp::ctxm_user)
.set_name("High Five")
.set_application_id(bot.me.id),
857692897221033129 // you need to put your guild-id in here
);
bot.guild_command_create(command, guild_id);
}
});
/* Use the on_user_context_menu event to look for user context menu actions */
bot.on_user_context_menu([&](const dpp::user_context_menu_t &event) {
bot.on_user_context_menu([&](const dpp::user_context_menu_t& event) {
/* check if the context menu name is High Five */
if (event.command.get_command_name() == "High Five") {
if (event.command.get_command_name() == "high five") {
dpp::user user = event.get_user(); // the user who the command has been issued on
dpp::user author = event.command.get_issuing_user(); // the user who clicked on the context menu
event.reply(author.get_mention() + " slapped " + user.get_mention());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ int main(int argc, char const *argv[])
{
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
bot.on_log(dpp::utility::cout_logger());
bot.on_ready([&](const dpp::ready_t & event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a slash command and register it as a global command */
bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));
}
});
bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check for our /dialog command */
if (event.command.get_command_name() == "dialog") {
/* Instantiate an interaction_modal_response object */
dpp::interaction_modal_response modal("my_modal", "Please enter stuff");
/* Add a text component */
modal.add_component(
dpp::component().
Expand All @@ -37,6 +32,7 @@ int main(int argc, char const *argv[])
set_max_length(50).
set_text_style(dpp::text_short)
);
/* Add another text component in the next row, as required by Discord */
modal.add_row();
modal.add_component(
Expand All @@ -49,24 +45,36 @@ int main(int argc, char const *argv[])
set_max_length(2000).
set_text_style(dpp::text_paragraph)
);
/* Trigger the dialog box. All dialog boxes are ephemeral */
event.dialog(modal);
}
});
/* This event handles form submission for the modal dialog we create above */
bot.on_form_submit([&](const dpp::form_submit_t & event) {
/* For this simple example we know the first element of the first row ([0][0]) is value type string.
* In the real world it may not be safe to make such assumptions!
*/
std::string v = std::get<std::string>(event.components[0].components[0].value);
dpp::message m;
m.set_content("You entered: " + v).set_flags(dpp::m_ephemeral);
/* Emit a reply. Form submission is still an interaction and must generate some form of reply! */
event.reply(m);
});
bot.on_ready([&](const dpp::ready_t & event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a slash command and register it as a global command */
bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
Expand Down
Loading

0 comments on commit 656bfb8

Please sign in to comment.