MCPcopy Create free account
hub / github.com/brainboxdotcc/DPP / main

Function main

docpages/example_code/private_messaging.cpp:3–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include <dpp/dpp.h>
2
3int main() {
4 /* Create the bot */
5 dpp::cluster bot("token");
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 /* The event is fired when someone issues your commands */
10 bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
11 /* Check which command they ran */
12 if (event.command.get_command_name() == "pm") {
13 dpp::snowflake user;
14
15 /* If there was no specified user, we set the "user" variable to the command author (issuing user). */
16 if (event.get_parameter("user").index() == 0) {
17 user = event.command.get_issuing_user().id;
18 } else { /* Otherwise, we set it to the specified user! */
19 user = std::get<dpp::snowflake>(event.get_parameter("user"));
20 }
21
22 /* Send a message to the user set above. */
23 bot.direct_message_create(user, dpp::message("Here's a private message!"), [event, user](const dpp::confirmation_callback_t& callback){
24 /* If the callback errors, we want to send a message telling the author that something went wrong. */
25 if (callback.is_error()) {
26 /* Here, we want the error message to be different if the user we're trying to send a message to is the command author. */
27 if (user == event.command.get_issuing_user().id) {
28 event.reply(dpp::message("I couldn't send you a message.").set_flags(dpp::m_ephemeral));
29 } else {
30 event.reply(dpp::message("I couldn't send a message to that user. Please check that is a valid user!").set_flags(dpp::m_ephemeral));
31 }
32
33 return;
34 }
35
36 /* We do the same here, so the message is different if it's to the command author or if it's to a specified user. */
37 if (user == event.command.get_issuing_user().id) {
38 event.reply(dpp::message("I've sent you a private message.").set_flags(dpp::m_ephemeral));
39 } else {
40 event.reply(dpp::message("I've sent a message to that user.").set_flags(dpp::m_ephemeral));
41 }
42 });
43 }
44 });
45
46 bot.on_ready([&bot](const dpp::ready_t& event) {
47 if (dpp::run_once<struct register_bot_commands>()) {
48 /* Register the command */
49 dpp::slashcommand command("pm", "Send a private message.", bot.me.id);
50
51 /* Add the option for a user mention that isn't required */
52 command.add_option(dpp::command_option(dpp::co_mentionable, "user", "The user to message", false));
53
54 /* Register the command */
55 bot.global_command_create(command);
56 }
57 });
58
59 bot.start(dpp::st_wait);
60

Callers

nothing calls this directly

Calls 10

cout_loggerFunction · 0.85
get_command_nameMethod · 0.80
get_parameterMethod · 0.80
direct_message_createMethod · 0.80
is_errorMethod · 0.80
global_command_createMethod · 0.80
startMethod · 0.80
messageClass · 0.50
indexMethod · 0.45
replyMethod · 0.45

Tested by

no test coverage detected