mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-27 04:27:11 +02:00
Add D++ library.
This commit is contained in:
72
vendor/DPP/docpages/example_programs/interactions_and_components/autocomplete.md
vendored
Normal file
72
vendor/DPP/docpages/example_programs/interactions_and_components/autocomplete.md
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
\page application-command-autocomplete Slash command auto completion
|
||||
|
||||
Discord now supports sending auto completion lists for slash command choices. To use this feature you can use code such as the example below:
|
||||
|
||||
~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
bot.on_ready([&bot](const dpp::ready_t & event) {
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
/* Create a new global command once on ready event */
|
||||
bot.global_command_create(dpp::slashcommand("blep", "Send a random adorable animal photo", bot.me.id)
|
||||
.add_option(
|
||||
/* If you set the auto complete setting on a command option, it will trigger the on_autocomplete
|
||||
* event whenever discord needs to fill information for the choices. You cannot set any choices
|
||||
* here if you set the auto complete value to true.
|
||||
*/
|
||||
dpp::command_option(dpp::co_string, "animal", "The type of animal").set_auto_complete(true)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
/* The interaction create 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() == "blep") {
|
||||
/* Fetch a parameter value from the command parameters */
|
||||
std::string animal = std::get<std::string>(event.get_parameter("animal"));
|
||||
/* Reply to the command. There is an overloaded version of this
|
||||
* call that accepts a dpp::message so you can send embeds.
|
||||
*/
|
||||
event.reply("Blep! You chose " + animal);
|
||||
}
|
||||
});
|
||||
|
||||
/* The on_autocomplete event is fired whenever discord needs information to fill in a command options's choices.
|
||||
* You must reply with a REST event within 500ms, so make it snappy!
|
||||
*/
|
||||
bot.on_autocomplete([&bot](const dpp::autocomplete_t & event) {
|
||||
for (auto & opt : event.options) {
|
||||
/* The option which has focused set to true is the one the user is typing in */
|
||||
if (opt.focused) {
|
||||
/* In a real world usage of this function you should return values that loosely match
|
||||
* opt.value, which contains what the user has typed so far. The opt.value is a variant
|
||||
* and will contain the type identical to that of the slash command parameter.
|
||||
* Here we can safely know it is string.
|
||||
*/
|
||||
std::string uservalue = std::get<std::string>(opt.value);
|
||||
bot.interaction_response_create(event.command.id, event.command.token, dpp::interaction_response(dpp::ir_autocomplete_reply)
|
||||
.add_autocomplete_choice(dpp::command_option_choice("squids", "lots of squids"))
|
||||
.add_autocomplete_choice(dpp::command_option_choice("cats", "a few cats"))
|
||||
.add_autocomplete_choice(dpp::command_option_choice("dogs", "bucket of dogs"))
|
||||
.add_autocomplete_choice(dpp::command_option_choice("elephants", "bottle of elephants"))
|
||||
);
|
||||
bot.log(dpp::ll_debug, "Autocomplete " + opt.name + " with value '" + uservalue + "' in field " + event.name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~
|
||||
|
68
vendor/DPP/docpages/example_programs/interactions_and_components/commandhandler.md
vendored
Normal file
68
vendor/DPP/docpages/example_programs/interactions_and_components/commandhandler.md
vendored
Normal file
@ -0,0 +1,68 @@
|
||||
\page commandhandler Using a command handler object
|
||||
|
||||
If you have many commands in your bot, and want to handle commands from multiple sources (for example modern slash commands, and more regular
|
||||
prefixed channel messages) you should consider instantiating a dpp::commandhandler object. This object can be used to automatically route
|
||||
commands and their parameters to functions in your program. A simple example of using this object to route commands is shown below, and will
|
||||
route both the /ping (global slash command) and .ping (prefixed channel message command) to a lambda where a reply can be generated.
|
||||
|
||||
\note This example automatically hooks the dpp::cluster::on_message_create and dpp::cluster::on_slashcommand events. This can be overridden if needed to allow you to still make use of these functions for your own code, if you need to do this please see the constructor documentation for dpp::commandhandler.
|
||||
|
||||
Note that because the dpp::commandhandler::add_command method accepts a std::function as the command handler, you may point a command handler
|
||||
at a simple lambda (as shown in this example), a function pointer, or an instantiated class method of an object. This is extremely flexible
|
||||
and allows you to decide how and where commands should be routed, either to an object oriented system or to a lambda based system.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
/* Create command handler, and specify prefixes */
|
||||
dpp::commandhandler command_handler(&bot);
|
||||
/* Specifying a prefix of "/" tells the command handler it should also expect slash commands */
|
||||
command_handler.add_prefix(".").add_prefix("/");
|
||||
|
||||
bot.on_ready([&command_handler](const dpp::ready_t &event) {
|
||||
|
||||
command_handler.add_command(
|
||||
/* Command name */
|
||||
"ping",
|
||||
|
||||
/* Parameters */
|
||||
{
|
||||
{"testparameter", dpp::param_info(dpp::pt_string, true, "Optional test parameter") }
|
||||
},
|
||||
|
||||
/* Command handler */
|
||||
[&command_handler](const std::string& command, const dpp::parameter_list_t& parameters, dpp::command_source src) {
|
||||
std::string got_param;
|
||||
if (!parameters.empty()) {
|
||||
got_param = std::get<std::string>(parameters[0].second);
|
||||
}
|
||||
command_handler.reply(dpp::message("Pong! -> " + got_param), src);
|
||||
},
|
||||
|
||||
/* Command description */
|
||||
"A test ping command",
|
||||
|
||||
/* Guild id (omit for a global command) */
|
||||
819556414099554344
|
||||
);
|
||||
|
||||
/* NOTE: We must call this to ensure slash commands are registered.
|
||||
* This does a bulk register, which will replace other commands
|
||||
* that are registered already!
|
||||
*/
|
||||
command_handler.register_commands();
|
||||
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
54
vendor/DPP/docpages/example_programs/interactions_and_components/components.md
vendored
Normal file
54
vendor/DPP/docpages/example_programs/interactions_and_components/components.md
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
\page components Using button components
|
||||
|
||||
Discord's newest features support sending buttons alongside messages, which when clicked by the user trigger an interaction which is routed by
|
||||
D++ as an on_button_click event. To make use of this, use code as in this example.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
#include <iostream>
|
||||
#include <dpp/message.h>
|
||||
|
||||
int main() {
|
||||
|
||||
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
|
||||
|
||||
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")
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
/* 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) {
|
||||
/* 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.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
When the feature is functioning, the code below will produce buttons on the reply message like in the image below:
|
||||
|
||||
\image html button.png
|
||||
|
55
vendor/DPP/docpages/example_programs/interactions_and_components/components2.md
vendored
Normal file
55
vendor/DPP/docpages/example_programs/interactions_and_components/components2.md
vendored
Normal file
@ -0,0 +1,55 @@
|
||||
\page components2 Advanced components
|
||||
|
||||
This example demonstrates receiving button clicks and sending response messages.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
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
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
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));
|
||||
} else {
|
||||
event.reply(dpp::message("Incorrect").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.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This code will send a different message for correct and incorrect answers.
|
||||
|
||||
\image html button_2.png
|
||||
|
47
vendor/DPP/docpages/example_programs/interactions_and_components/components3.md
vendored
Normal file
47
vendor/DPP/docpages/example_programs/interactions_and_components/components3.md
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
\page components3 Using select menu components
|
||||
|
||||
This example demonstrates receiving select menu clicks and sending response messages.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main() {
|
||||
|
||||
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
|
||||
|
||||
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(
|
||||
dpp::component().add_component(
|
||||
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")
|
||||
)
|
||||
);
|
||||
bot.message_create(m);
|
||||
}
|
||||
});
|
||||
/* When a user clicks your select menu , the on_select_click event will fire,
|
||||
* containing the custom_id you defined in your select menu.
|
||||
*/
|
||||
bot.on_select_click([&bot](const dpp::select_click_t & event) {
|
||||
/* Select 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 + " and chose: " + event.values[0]);
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
51
vendor/DPP/docpages/example_programs/interactions_and_components/context_menus.md
vendored
Normal file
51
vendor/DPP/docpages/example_programs/interactions_and_components/context_menus.md
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
\page context-menu Context Menus
|
||||
|
||||
Context menus are application commands that appear on the context menu (right click or tap) of users or messages to perform context-specific actions. They can be created using `dpp::slashcommand`. Once you create a context menu, try right-clicking either a user or message to see it in your server!
|
||||
|
||||
\image html context_menu_user_command.png
|
||||
|
||||
The following example shows how to create and handle **user context menus**.
|
||||
|
||||
~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
bot.on_ready([&bot](const dpp::ready_t &event) {
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
/* 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
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
/* 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) {
|
||||
/* check if the context menu name is 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());
|
||||
}
|
||||
});
|
||||
|
||||
/* Start bot */
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~
|
||||
|
||||
It registers a guild command that can be called by right-click a user and click on the created menu.
|
||||
|
||||
\image html context_menu_user_command_showcase.png
|
78
vendor/DPP/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md
vendored
Normal file
78
vendor/DPP/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
\page modal-dialog-interactions 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_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:
|
||||
|
||||
~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
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) {
|
||||
/* 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 another text component in the next row, as required by Discord */
|
||||
modal.add_row();
|
||||
modal.add_component(
|
||||
dpp::component().
|
||||
set_label("Type rammel").
|
||||
set_id("field_id2").
|
||||
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);
|
||||
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);
|
||||
});
|
||||
|
||||
/* Start bot */
|
||||
bot.start(dpp::st_wait);
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~
|
||||
|
||||
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:
|
||||
|
||||
\image html modal_dialog.png
|
||||
|
61
vendor/DPP/docpages/example_programs/interactions_and_components/slashcommands.md
vendored
Normal file
61
vendor/DPP/docpages/example_programs/interactions_and_components/slashcommands.md
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
\page slashcommands Using Slash Commands and Interactions
|
||||
|
||||
Slash commands and interactions are a newer feature of Discord which allow bot's commands to be registered centrally within the system and for users to easily explore and get help with available commands through the client itself.
|
||||
|
||||
To add a slash command you should use the dpp::cluster::global_command_create method for global commands (available to all guilds) or dpp::cluster::guild_command_create to create a local command (available only to one guild).
|
||||
|
||||
When a user issues these commands the reply will arrive via the `on_slashcommand` event which you can hook, and take action when you see your commands. It is possible to reply to an interaction by using either the dpp::interaction_create_t::reply method, or by manually instantiating an object of type dpp::interaction_response and attaching a dpp::message object to it.
|
||||
|
||||
dpp::interaction_create_t::reply has two overloaded versions of the method, one of which accepts simple std::string replies, for basic text-only messages (if your message is 'ephemeral' you must use this) and one which accepts a dpp::message for more advanced replies. Please note that at present, Discord only supports a small subset of message and embed features within an interaction response object.
|
||||
|
||||
\note You can also use the unified command handler, which lets you combine channel based message commands and slash commands under the same lambda with the same code like they were one and the same. Note that after August of 2022 Discord will be discouraging bots from using commands that are prefixed messages via means of a privileged message intent. It is advised that you exclusively use slash commands, or the unified handler with only a prefix of "/" going forward for any new bots you create and look to migrating existing bots to this setup.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
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() == "blep") {
|
||||
/* Fetch a parameter value from the command parameters */
|
||||
std::string animal = std::get<std::string>(event.get_parameter("animal"));
|
||||
/* Reply to the command. There is an overloaded version of this
|
||||
* call that accepts a dpp::message so you can send embeds.
|
||||
*/
|
||||
event.reply(std::string("Blep! You chose") + animal);
|
||||
}
|
||||
});
|
||||
|
||||
bot.on_ready([&bot](const dpp::ready_t & event) {
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
|
||||
/* Create a new global command on ready event */
|
||||
dpp::slashcommand newcommand("blep", "Send a random adorable animal photo", bot.me.id);
|
||||
newcommand.add_option(
|
||||
dpp::command_option(dpp::co_string, "animal", "The type of animal", true).
|
||||
add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))).
|
||||
add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))).
|
||||
add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin")
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
/* Register the command */
|
||||
bot.global_command_create(newcommand);
|
||||
}
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
\note For demonstration purposes, and small bots, this code is OK, but in the real world once your bot gets big, it's not recommended to create slash commands in the `on_ready` event because it gets called often (discord forces reconnections and sometimes these do not resume). You could for example add a commandline parameter to your bot (`argc`, `argv`) so that if you want the bot to register commands it must be launched with a specific command line argument.
|
||||
|
78
vendor/DPP/docpages/example_programs/interactions_and_components/subcommands.md
vendored
Normal file
78
vendor/DPP/docpages/example_programs/interactions_and_components/subcommands.md
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
\page subcommands Using sub-commands in slash commands
|
||||
|
||||
This demonstrates how to use sub-commands within slash commands. Also shown below is an example of how to get a "resolved" parameter without having to use the cache or an extra API call.
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
|
||||
dpp::cluster bot("token");
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
/* Executes on ready. */
|
||||
bot.on_ready([&bot](const dpp::ready_t & event) {
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
/* Define a slash command. */
|
||||
dpp::slashcommand image("image", "Send a specific image.", bot.me.id);
|
||||
image.add_option(
|
||||
/* Create a subcommand type option for "dog". */
|
||||
dpp::command_option(dpp::co_sub_command, "dog", "Send a picture of a dog.").
|
||||
add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a dog.", false))
|
||||
);
|
||||
image.add_option(
|
||||
/* Create another subcommand type option for "cat". */
|
||||
dpp::command_option(dpp::co_sub_command, "cat", "Send a picture of a cat.").
|
||||
add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a cat.", false))
|
||||
);
|
||||
/* Create command */
|
||||
bot.global_command_create(image);
|
||||
}
|
||||
});
|
||||
|
||||
/* Use the on_slashcommand event to look for commands */
|
||||
bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
|
||||
dpp::interaction interaction = event.command;
|
||||
dpp::command_interaction cmd_data = interaction.get_command_interaction();
|
||||
/* Check if the command is the image command. */
|
||||
if (interaction.get_command_name() == "image") {
|
||||
/* Get the sub command */
|
||||
auto subcommand = cmd_data.options[0];
|
||||
/* Check if the subcommand is "dog" */
|
||||
if (subcommand.name == "dog") {
|
||||
/* Checks if the subcommand has any options. */
|
||||
if (!subcommand.options.empty()) {
|
||||
/* Get the user from the parameter */
|
||||
dpp::user user = interaction.get_resolved_user(
|
||||
subcommand.get_value<dpp::snowflake>(0)
|
||||
);
|
||||
event.reply(user.get_mention() + " has now been turned into a dog.");
|
||||
} else {
|
||||
/* Reply if there were no options.. */
|
||||
event.reply("No user specified");
|
||||
}
|
||||
}
|
||||
/* Check if the subcommand is "cat" */
|
||||
if (subcommand.name == "cat") {
|
||||
/* Checks if the subcommand has any options. */
|
||||
if (!subcommand.options.empty()) {
|
||||
/* Get the user from the parameter */
|
||||
dpp::user user = interaction.get_resolved_user(
|
||||
subcommand.get_value<dpp::snowflake>(0)
|
||||
);
|
||||
event.reply(user.get_mention() + " has now been turned into a cat.");
|
||||
} else {
|
||||
/* Reply if there were no options.. */
|
||||
event.reply("No user specified");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
48
vendor/DPP/docpages/example_programs/interactions_and_components/upload_parameter.md
vendored
Normal file
48
vendor/DPP/docpages/example_programs/interactions_and_components/upload_parameter.md
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
\page discord-application-command-file-upload Using file parameters for application commands (slash commands)
|
||||
|
||||
The program below demonstrates how to use the 'file' type parameter to an application command (slash command).
|
||||
You must first get the file_id via std::get, and then you can find the attachment details in the 'resolved'
|
||||
section, `event.command.resolved`.
|
||||
|
||||
The file is uploaded to Discord's CDN so if you need it locally you should fetch the `.url` value, e.g. by using
|
||||
something like `dpp::cluster::request()`.
|
||||
|
||||
~~~~~~~~~~~~~~~~{.cpp}
|
||||
#include <dpp/dpp.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
dpp::cluster bot("token");
|
||||
|
||||
bot.on_log(dpp::utility::cout_logger());
|
||||
|
||||
bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
|
||||
if (event.command.type == dpp::it_application_command) {
|
||||
dpp::command_interaction cmd_data = std::get<dpp::command_interaction>(event.command.data);
|
||||
if (cmd_data.name == "show") {
|
||||
dpp::snowflake file_id = std::get<dpp::snowflake>(event.get_parameter("file"));
|
||||
auto iter = event.command.resolved.attachments.find(file_id);
|
||||
if (iter != event.command.resolved.attachments.end()) {
|
||||
const dpp::attachment& att = iter->second;
|
||||
event.reply(att.url);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
bot.on_ready([&bot](const dpp::ready_t & event) {
|
||||
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
dpp::slashcommand newcommand("show", "Show an uploaded file", bot.me.id);
|
||||
|
||||
newcommand.add_option(dpp::command_option(dpp::co_attachment, "file", "Select an image"));
|
||||
|
||||
bot.global_command_create(newcommand);
|
||||
}
|
||||
});
|
||||
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
~~~~~~~~~~~~~~~~
|
Reference in New Issue
Block a user