Skip to content

Commit

Permalink
docs: interactions and components examples split
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Sep 14, 2023
1 parent af6ce15 commit 97c59df
Show file tree
Hide file tree
Showing 30 changed files with 865 additions and 882 deletions.
53 changes: 53 additions & 0 deletions docpages/example_code/commandhandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <dpp/dpp.h>

int main()
{
/* If your bot only uses the "/" prefix, you can remove the intents here. */
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);

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. Remove the .add_prefix(".") if you wish to only make it a slash command */
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 guild 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;
}
57 changes: 57 additions & 0 deletions docpages/example_code/components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <dpp/dpp.h>
#include <dpp/unicode_emoji.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() == "button") {

/* Create a message */
dpp::message msg(event.command.channel_id, "this text has a button");

/* Add an action row, and then a button within the action row. */
msg.add_component(
dpp::component().add_component(
dpp::component().
set_label("Click me!").
set_type(dpp::cot_button).
set_emoji(dpp::unicode_emoji::smile).
set_style(dpp::cos_danger).
set_id("myid")
)
);

/* Reply to the user with our message. */
event.reply(msg);
}
});

/* 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.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {

/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
62 changes: 62 additions & 0 deletions docpages/example_code/components2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#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() == "math") {

/* Create a message */
dpp::message msg(event.command.channel_id, "What is 5+5?");

/* Add an action row, and then 3 buttons within the action row. */
msg.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")
)
);

/* Reply to the user with our message. */
event.reply(msg);
}
});

bot.on_button_click([&bot](const dpp::button_click_t & event) {
if (event.custom_id == "10") {
event.reply(dpp::message("You got it right!").set_flags(dpp::m_ephemeral));
} else {
event.reply(dpp::message("Wrong! Try again.").set_flags(dpp::m_ephemeral));
}
});

bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {

/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("math", "A quick maths question!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
57 changes: 57 additions & 0 deletions docpages/example_code/components3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <dpp/dpp.h>
#include <dpp/unicode_emoji.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() == "select") {

/* Create a message */
dpp::message msg(event.command.channel_id, "This text has a select menu!");

/* Add an action row, and a select menu within the action row. */
msg.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(dpp::unicode_emoji::smile)).
add_select_option(dpp::select_option("label2","value2","description2").set_emoji(dpp::unicode_emoji::slight_smile)).
set_id("myselectid")
)
);

/* Reply to the user with our message. */
event.reply(msg);
}
});

/* 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.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {

/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
39 changes: 39 additions & 0 deletions docpages/example_code/context_menus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <dpp/dpp.h>
#include <iostream>

int main()
{
dpp::cluster bot("token");

bot.on_log(dpp::utility::cout_logger());

/* 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());
}
});

bot.on_ready([&bot](const dpp::ready_t &event) {
if (dpp::run_once<struct register_bot_commands>()) {

/* Create the command */
dpp::slashcommand command;
command.set_name("High Five");
command.set_application_id(bot.me.id);
command.set_type(dpp::ctxm_user);

/* Register the command */
bot.guild_command_create(command, 857692897221033129); /* Replace this with the guild id you want */
}
});

/* Start bot */
bot.start(dpp::st_wait);

return 0;
}
24 changes: 24 additions & 0 deletions docpages/example_code/detecting_messages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <dpp/dpp.h>

int main()
{
/* Create the bot, but with our intents so we can use messages. */
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);

bot.on_log(dpp::utility::cout_logger());

/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_message_create([&bot](const dpp::message_create_t& event) {

/* See if the message contains the phrase we want to check for.
* If there's at least a single match, we reply and say it's not allowed.
*/
if (event.msg.content.find("bad word") != std::string::npos) {
event.reply("That is not allowed here. Please, mind your language!", true);
}
});

bot.start(dpp::st_wait);

return 0;
}
32 changes: 32 additions & 0 deletions docpages/example_code/ephemeral.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <dpp/dpp.h>

int main()
{
/* Create the bot */
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() == "hello") {

/* Reply to the user, but only let them see the response. */
event.reply(dpp::message("Hello! How are you today?").set_flags(dpp::m_ephemeral));
}
});

bot.on_ready([&bot](const dpp::ready_t & event) {
if (dpp::run_once<struct register_bot_commands>()) {

/* Create and Register the command */
bot.global_command_create(dpp::slashcommand("hello", "Hello there!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
36 changes: 36 additions & 0 deletions docpages/example_code/making_threads1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <dpp/dpp.h>

int main()
{
/* Create the bot */
dpp::cluster bot("token");

bot.on_log(dpp::utility::cout_logger());

/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "create-thread") {
/* Here we create a thread in the current channel. It will expire after 60 minutes of inactivity. We'll also allow other mods to join, and we won't add a slowdown timer. */
bot.thread_create("Cool thread!", event.command.channel_id, 60, dpp::channel_type::CHANNEL_PUBLIC_THREAD, true, 0, [event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("Failed to create a thread!");
return;
}

event.reply("Created a thread for you!");
});
}
});

bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register the command */
bot.global_command_create(dpp::slashcommand("create-thread", "Create a thread!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
Loading

0 comments on commit 97c59df

Please sign in to comment.