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[])
{
newcommand.
set_name(
"dialog").set_description(
"Make a modal dialog box").set_application_id(bot.me.id);
bot.global_command_create(newcommand);
});
dpp::command_interaction cmd_data = std::get<dpp::command_interaction>(event.command.data);
if (cmd_data.name == "dialog") {
dpp::interaction_modal_response modal("my_modal", "Please enter stuff");
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)
);
event.dialog(modal);
}
}
});
std::string v = std::get<std::string>(event.
components[0].components[0].value);
});
});
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:384
uint8_t type
the type of interaction
Definition: slashcommand.h:559
Represents an application command, created by your bot either globally, or on a guild.
Definition: slashcommand.h:661
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:900
@ it_application_command
application command (slash command)
Definition: slashcommand.h:455
@ ir_channel_message_with_source
respond to an interaction with a message
Definition: slashcommand.h:218
Create interaction.
Definition: dispatcher.h:259
interaction command
command interaction
Definition: dispatcher.h:351
Log messages.
Definition: dispatcher.h:92
std::string message
Definition: dispatcher.h:102
Represents messages sent and received on Discord.
Definition: message.h:1010
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:552
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: