D++ (DPP)
C++ Discord API Bot Library
Modal Dialog Interactions

Modal dialog interactions are a new Discord API feature that allow you to have pop-up windows which prompt the user to input information. Once the user has filled in this information, your program will receive an on_form_submit event which will contain the data which was input. You must use a slash command interaction response to submit your modal form data to Discord, via the on_interaction_create event. From here calling the dialog method of the interaction_create_t event object will trigger the dialog to appear.

Each dialog box may have up to five rows of input fields. The example below demonstrates a simple setup with just one text input:

#include <dpp/dpp.h>
#include <iostream>
int main(int argc, char const *argv[])
{
dpp::cluster bot("token");
bot.on_ready([&](const dpp::ready_t & event) {
/* Create a slash command and register it as a global command */
dpp::slashcommand newcommand;
newcommand.set_name("dialog").set_description("Make a modal dialog box").set_application_id(bot.me.id);
bot.global_command_create(newcommand);
});
bot.on_interaction_create([&bot](const dpp::interaction_create_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().
set_label("Type rammel").
set_id("field_id").
set_type(dpp::cot_text).
set_placeholder("gumf").
set_min_length(1).
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);
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);
});
/* Budget brand logger */
bot.on_log([&](const dpp::log_t & log) {
std::cout << log.message << "\n";
});
/* Start bot */
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:392
std::string get_command_name() const
Get the command name for a command interaction.
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:762
slashcommand & set_name(const std::string &n)
Set the name of the command.
@ m_ephemeral
this message is only visible to the user who invoked the Interaction
Definition: message.h:913
Definition: dispatcher.h:431
std::vector< component > components
Message components for form reply.
Definition: dispatcher.h:452
Create interaction.
Definition: dispatcher.h:283
interaction command
command interaction
Definition: dispatcher.h:395
Log messages.
Definition: dispatcher.h:103
std::string message
Definition: dispatcher.h:113
Represents messages sent and received on Discord.
Definition: message.h:1023
message & set_content(const std::string &c)
Set the message content.
message & set_flags(uint8_t f)
Set the flags.
Session ready.
Definition: dispatcher.h:601

If you compile and run this program and wait for the global command to register, typing /dialog will present you with a dialog box like the one below:

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0