Skip to content

Commit

Permalink
misc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Sep 13, 2023
1 parent acfd7e4 commit af6ce15
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 190 deletions.
60 changes: 60 additions & 0 deletions docpages/example_code/cache_messages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <dpp/dpp.h>
#include <sstream>

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<dpp::message> 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<std::string>(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<struct register_bot_commands>()) {

/* 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;
}
46 changes: 46 additions & 0 deletions docpages/example_code/collect_reactions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <dpp/dpp.h>

/* 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<dpp::collected_reaction>& 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;
}
28 changes: 28 additions & 0 deletions docpages/example_code/http_request.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <dpp/dpp.h>
#include <iostream>

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);
}
20 changes: 20 additions & 0 deletions docpages/example_code/setting_status1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <dpp/dpp.h>

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;
}
26 changes: 26 additions & 0 deletions docpages/example_code/setting_status2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <dpp/dpp.h>

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<struct register_bot_commands>()) {
/* 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;
}
64 changes: 1 addition & 63 deletions docpages/example_programs/misc/cache_messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dpp/dpp.h>
#include <sstream>
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<dpp::message> 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<std::string>(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<struct register_bot_commands>()) {
/* 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
49 changes: 1 addition & 48 deletions docpages/example_programs/misc/collect_reactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dpp/dpp.h>
/* 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<dpp::collected_reaction>& 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

30 changes: 1 addition & 29 deletions docpages/example_programs/misc/http_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dpp/dpp.h>
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
Loading

0 comments on commit af6ce15

Please sign in to comment.