Debian Codegraph @

docpages/example_code/modal_dialog_interactions.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <iostream>
3
4 int main() {
5 dpp::cluster bot("token");
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 bot.on_slashcommand([](const dpp::slashcommand_t& event) {
10 /* Check for our /dialog command */
11 if (event.command.get_command_name() == "dialog") {
12 /* Instantiate an interaction_modal_response object */
13 dpp::interaction_modal_response modal("my_modal", "Please enter stuff");
14
15 /* Add a text component */
16 modal.add_component(
17 dpp::component()
18 .set_label("Short type rammel")
19 .set_id("field_id")
20 .set_type(dpp::cot_text)
21 .set_placeholder("gumd")
22 .set_min_length(5)
23 .set_max_length(50)
24 .set_text_style(dpp::text_short)
25 );
26
27 /* Add a channel selection */
28 modal.add_component(
29 dpp::component()
30 .set_label("Select channel")
31 .set_id("field_id2")
32 .set_type(dpp::cot_channel_selectmenu)
33 );
34
35
36 /* Trigger the dialog box. All dialog boxes are ephemeral */
37 event.dialog(modal);
38 }
39 });
40
41 /* This event handles form submission for the modal dialog we create above */
42 bot.on_form_submit([](const dpp::form_submit_t& event) {
43 /* For this simple example, we know the elements value type is string.
44 * We also know the indices of each element.
45 * In the real world, it may not be safe to make such assumptions!
46 */
47 std::string message_text = std::get<std::string>(event.components[0].value);
48 std::string channel_id = std::get<std::string>(event.components[1].value);
49
50 dpp::message m;
51 m.set_content(
52 std::string("You entered '") + message_text +
53 "', picked channel: <#" + channel_id + ">"
54 )
55 .set_flags(dpp::m_ephemeral);
56
57 /* Emit a reply. Form submission is still an interaction and must generate some form of reply! */
58 event.reply(m);
59 });
60
61 bot.on_ready([&bot](const dpp::ready_t& event) {
62 if (dpp::run_once<struct register_bot_commands>()) {
63 /* Create a slash command and register it as a global command */
64 bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id));
65 }
66 });
67
68 /* Start bot */
69 bot.start(dpp::st_wait);
70
71 return 0;
72 }