| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Modals

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_slashcommand 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() {
dpp::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
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()
.set_label("Short type rammel")
.set_id("field_id")
.set_type(dpp::cot_text)
.set_placeholder("gumd")
.set_min_length(5)
.set_max_length(50)
.set_text_style(dpp::text_short)
);
/* Add a channel selection */
modal.add_component(
dpp::component()
.set_label("Select channel")
.set_id("field_id2")
.set_type(dpp::cot_channel_selectmenu)
);
/* 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 elements value type is string.
* We also know the indices of each element.
* In the real world, it may not be safe to make such assumptions!
*/
std::string message_text = std::get<std::string>(event.components[0].value);
std::string channel_id = std::get<std::string>(event.components[1].value);
dpp::message m;
m.set_content(
std::string("You entered '") + message_text +
"', picked channel: <#" + channel_id + ">"
)
.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([&bot](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;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::component
Represents the component object. A component is a clickable button or drop down list within a discord...
Definition message.h:497
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::text_short
@ text_short
Intended for short single-line text.
Definition message.h:213
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::m_ephemeral
@ m_ephemeral
This message is only visible to the user who invoked the Interaction.
Definition message.h:1970
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::cot_channel_selectmenu
@ cot_channel_selectmenu
Select menu for channels.
Definition message.h:78
dpp::cot_text
@ cot_text
Text input.
Definition message.h:58
dpp::form_submit_t
On form submitted.
Definition dispatcher.h:835
dpp::form_submit_t::components
std::vector< component > components
Message components for form reply.
Definition dispatcher.h:850
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::interaction_modal_response
Represents a modal dialog box response to an interaction.
Definition appcommand.h:555
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2350
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

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 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0