diff --git a/docpages/example_code/cache_messages.cpp b/docpages/example_code/cache_messages.cpp new file mode 100644 index 0000000000..8267971bb1 --- /dev/null +++ b/docpages/example_code/cache_messages.cpp @@ -0,0 +1,60 @@ +#include +#include + +int main() { + /* Create bot */ + dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); /* Because we're handling messages, we need to use the "i_message_content" intent! */ + + /* Create a cache to contain types of dpp::message */ + dpp::cache message_cache; + + bot.on_log(dpp::utility::cout_logger()); + + /* Message handler */ + bot.on_message_create([&](const dpp::message_create_t &event) { + /* Make a permanent pointer using new, for each message to be cached */ + dpp::message* m = new dpp::message(); + + /* Store the message into the pointer by copying it */ + *m = event.msg; + + /* Store the new pointer to the cache using the store() method */ + message_cache.store(m); + }); + + /* The event is fired when someone issues your commands */ + bot.on_slashcommand([&bot, &message_cache](const dpp::slashcommand_t& event) { + /* Check which command they ran */ + if (event.command.get_command_name() == "get") { + + dpp::message* find_msg = message_cache.find(std::get(event.get_parameter("message_id"))); + + /* If find_msg is null, tell the user and return. */ + if (!find_msg) { + event.reply("There is no message cached with this ID"); + return; + } + + event.reply("This message had the following content: " + find_msg->content); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + + /* Create a new command. */ + dpp::slashcommand newcommand("get", "Get the contents of a message that was cached via an id", bot.me.id); + + /* Add a parameter option. */ + newcommand.add_option(dpp::command_option(dpp::co_string, "message_id", "The ID of the message you want to find", true)); + + /* Register the command */ + bot.global_command_create(newcommand); + } + }); + + /* Start bot */ + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/collect_reactions.cpp b/docpages/example_code/collect_reactions.cpp new file mode 100644 index 0000000000..4d796643cf --- /dev/null +++ b/docpages/example_code/collect_reactions.cpp @@ -0,0 +1,46 @@ +#include + +/* To create a collector we must derive from dpp::collector. As dpp::collector is a complicated template, + * various pre-made forms exist such as this one, reaction_collector. + */ +class react_collector : public dpp::reaction_collector { +public: + /* Collector will run for 20 seconds */ + react_collector(dpp::cluster* cl, dpp::snowflake id) : dpp::reaction_collector(cl, 20, id) { } + + /* Override the "completed" event and then output the number of collected reactions as a message. */ + virtual void completed(const std::vector& list) override { + if (list.size()) { + owner->message_create(dpp::message(list[0].react_channel->id, "I collected " + std::to_string(list.size()) + " reactions!")); + } else { + owner->message_create(dpp::message("... I got nothin'.")); + } + } +}; + + +int main() { + /* Create bot */ + dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + + /* Pointer to reaction collector */ + react_collector* r = nullptr; + + bot.on_log(dpp::utility::cout_logger()); + + /* Message handler */ + bot.on_message_create([&r, &bot](const dpp::message_create_t& event) { + + /* If someone sends a message that has the text 'collect reactions!' start a reaction collector */ + if (event.msg.content == "collect reactions!" && r == nullptr) { + /* Create a new reaction collector to collect reactions */ + r = new react_collector(&bot, event.msg.id); + } + + }); + + /* Start bot */ + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/http_request.cpp b/docpages/example_code/http_request.cpp new file mode 100644 index 0000000000..67252bdc88 --- /dev/null +++ b/docpages/example_code/http_request.cpp @@ -0,0 +1,28 @@ +#include +#include + +int main() { + dpp::cluster bot("TOKEN GOES HERE"); + + bot.on_log(dpp::utility::cout_logger()); + + bot.on_ready([&bot](const dpp::ready_t& event) { + // Arbitrary post data as a string + std::string mypostdata = "{\"value\": 42}"; + // Make a HTTP POST request. HTTP and HTTPS are supported here. + bot.request( + "http://www.somebotlist.com/api/servers", dpp::m_post, [](const dpp::http_request_completion_t & cc) { + // This callback is called when the HTTP request completes. See documentation of + // dpp::http_request_completion_t for information on the fields in the parameter. + std::cout << "I got reply: " << cc.body << " with HTTP status code: " << cc.status << "\n"; + }, + mypostdata, + "application/json", + { + {"Authorization", "Bearer tokengoeshere"} + } + ); + }); + + bot.start(dpp::st_wait); +} diff --git a/docpages/example_code/setting_status1.cpp b/docpages/example_code/setting_status1.cpp new file mode 100644 index 0000000000..0c1cc0a451 --- /dev/null +++ b/docpages/example_code/setting_status1.cpp @@ -0,0 +1,20 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + bot.on_ready([&bot](const dpp::ready_t& event) { + /* We don't need the run_once here as we're not registering commands! */ + + /* Set the bot presence as online and "Playing..."! */ + bot.set_presence(dpp::presence(dpp::ps_online, dpp::at_game, "games!")); + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/setting_status2.cpp b/docpages/example_code/setting_status2.cpp new file mode 100644 index 0000000000..947ac4c72c --- /dev/null +++ b/docpages/example_code/setting_status2.cpp @@ -0,0 +1,26 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + bot.on_ready([&bot](const dpp::ready_t& event) { + /* We put our status updating inside "run_once" so that multiple shards don't try do this as "set_presence" updates all the shards. */ + if (dpp::run_once()) { + /* We update the presence now as the timer will do the first execution after the x amount of seconds we specify */ + bot.set_presence(dpp::presence(dpp::presence_status::ps_online, dpp::activity_type::at_game, "with " + std::to_string(dpp::get_guild_cache()->count()) + " guilds!")); + + /* Create a timer that runs every 120 seconds, that sets the status */ + bot.start_timer([&bot](const dpp::timer& timer) { + bot.set_presence(dpp::presence(dpp::presence_status::ps_online, dpp::activity_type::at_game, "with " + std::to_string(dpp::get_guild_cache()->count()) + " guilds!")); + }, 120); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_programs/misc/cache_messages.md b/docpages/example_programs/misc/cache_messages.md index 865cedd8f5..25c9782724 100644 --- a/docpages/example_programs/misc/cache_messages.md +++ b/docpages/example_programs/misc/cache_messages.md @@ -8,66 +8,4 @@ This can be adjusted to cache any type derived from dpp::managed including types you should use the dpp::cache::remove() method periodically to remove stale items. This is left out of this example as a learning exercise to the reader. For further reading please see the documentation of dpp::cache -~~~~~~~~~~{.cpp} -#include -#include - -int main() { - /* Create bot */ - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); /* Because we're handling messages, we need to use the "i_message_content" intent! */ - - /* Create a cache to contain types of dpp::message */ - dpp::cache message_cache; - - bot.on_log(dpp::utility::cout_logger()); - - /* Message handler */ - bot.on_message_create([&](const dpp::message_create_t &event) { - /* Make a permanent pointer using new, for each message to be cached */ - dpp::message* m = new dpp::message(); - - /* Store the message into the pointer by copying it */ - *m = event.msg; - - /* Store the new pointer to the cache using the store() method */ - message_cache.store(m); - }); - - /* 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() == "get") { - - dpp::message* find_msg = message_cache.find(std::get(event.get_parameter("message_id"))); - - /* If find_msg is null, tell the user and return. */ - if (!find_msg) { - event.reply("There is no message cached with this ID"); - return; - } - - event.reply("This message had the following content: " + find_msg->content); - } - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - - /* Create a new command. */ - dpp::slashcommand newcommand("get", "Get the contents of a message that was cached via an id", bot.me.id); - - /* Add a parameter option. */ - newcommand.add_option(dpp::command_option(dpp::co_string, "message_id", "The ID of the message you want to find", true)); - - /* Register the command */ - bot.global_command_create(newcommand); - } - }); - - /* Start bot */ - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ - +\include{cpp} cache_messages.cpp diff --git a/docpages/example_programs/misc/collect_reactions.md b/docpages/example_programs/misc/collect_reactions.md index 6a290e43d5..cbd671e5fa 100644 --- a/docpages/example_programs/misc/collect_reactions.md +++ b/docpages/example_programs/misc/collect_reactions.md @@ -4,52 +4,5 @@ D++ comes with many useful helper classes, but amongst these is something called In the example below we will use it to collect all reactions on a message. -~~~~~~~~~~{.cpp} -#include - -/* To create a collector we must derive from dpp::collector. As dpp::collector is a complicated template, - * various pre-made forms exist such as this one, reaction_collector. - */ -class react_collector : public dpp::reaction_collector { -public: - /* Collector will run for 20 seconds */ - react_collector(dpp::cluster* cl, snowflake id) : dpp::reaction_collector(cl, 20, id) { } - - /* Override the "completed" event and then output the number of collected reactions as a message. */ - virtual void completed(const std::vector& list) override { - if (list.size()) { - owner->message_create(dpp::message(list[0].react_channel->id, "I collected " + std::to_string(list.size()) + " reactions!")); - } else { - owner->message_create(dpp::message("... I got nothin'.")); - } - } -}; - - -int main() { - /* Create bot */ - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); - - /* Pointer to reaction collector */ - react_collector* r = nullptr; - - bot.on_log(dpp::utility::cout_logger()); - - /* Message handler */ - bot.on_message_create([&](const dpp::message_create_t& event) { - - /* If someone sends a message that has the text 'collect reactions!' start a reaction collector */ - if (event.msg.content == "collect reactions!" && r == nullptr) { - /* Create a new reaction collector to collect reactions */ - r = new react_collector(&bot, event.msg.id); - } - - }); - - /* Start bot */ - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} collect_reactions.cpp diff --git a/docpages/example_programs/misc/http_request.md b/docpages/example_programs/misc/http_request.md index ff01ba5e0c..523a781fda 100644 --- a/docpages/example_programs/misc/http_request.md +++ b/docpages/example_programs/misc/http_request.md @@ -2,32 +2,4 @@ If you wish to make arbitrary HTTP(S) requests to websites and APIs, e.g. to update statistics on bot lists, you can use code similar to the code below. You may pass any arbitrary POST data: -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() { - dpp::cluster bot("TOKEN GOES HERE"); - - bot.on_log(dpp::utility::cout_logger()); - - bot.on_ready([&bot](const dpp::ready_t& event) { - // Arbitrary post data as a string - std::string mypostdata = "{\"value\": 42}"; - // Make a HTTP POST request. HTTP and HTTPS are supported here. - bot.request( - "http://www.somebotlist.com/api/servers", dpp::m_post, [](const dpp::http_request_completion_t & cc) { - // This callback is called when the HTTP request completes. See documentation of - // dpp::http_request_completion_t for information on the fields in the parameter. - std::cout << "I got reply: " << cc.body << " with HTTP status code: " << cc.status << "\n"; - }, - mypostdata, - "application/json", - { - {"Authorization", "Bearer tokengoeshere"} - } - ); - }); - - bot.start(dpp::st_wait); -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} http_request.cpp diff --git a/docpages/example_programs/misc/setting_status.md b/docpages/example_programs/misc/setting_status.md index b4f6dc0253..a487ec8a54 100644 --- a/docpages/example_programs/misc/setting_status.md +++ b/docpages/example_programs/misc/setting_status.md @@ -4,28 +4,7 @@ A bot status is pretty cool, and it'd be cooler if you knew how to do it! This t First, we'll cover setting the bot status to `Playing games!`. -~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - bot.on_ready([&bot](const dpp::ready_t& event) { - /* We don't need the run_once here as we're not registering commands! */ - - /* Set the bot presence as online and "Playing..."! */ - bot.set_presence(dpp::presence(dpp::presence_status::ps_online, dpp::activity_type::at_game, "games!")); - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} setting_status1.cpp If all went well, your bot should now be online and say this on members list! @@ -33,34 +12,7 @@ If all went well, your bot should now be online and say this on members list! Now, let's cover setting the bot status to say `Playing with x guilds!` every two minutes. -~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - bot.on_ready([&bot](const dpp::ready_t& event) { - /* We put our status updating inside "run_once" so that multiple shards don't try do this as "set_presence" updates all the shards. */ - if (dpp::run_once()) { - /* We update the presence now as the timer will do the first execution after the x amount of seconds we specify */ - bot.set_presence(dpp::presence(dpp::presence_status::ps_online, dpp::activity_type::at_game, "with " + std::to_string(dpp::get_guild_cache()->count()) + " guilds!")); - - /* Create a timer that runs every 120 seconds, that sets the status */ - bot.start_timer([&bot](const dpp::timer& timer) { - bot.set_presence(dpp::presence(dpp::presence_status::ps_online, dpp::activity_type::at_game, "with " + std::to_string(dpp::get_guild_cache()->count()) + " guilds!")); - }, 120); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} setting_status2.cpp If you followed that well, your bot should now say this on members list!