D++ (DPP)
C++ Discord API Bot Library
Using Timers

Timers are a great way to run something every x seconds, from setting the bot's status, to maybe even doing a http request! Luckily, D++ makes this incredibly easy by providing an easy-to-use timer system! This tutorial will show you a couple examples on how to use timers!

First, we'll cover sending the D++ logo every 10 seconds!

#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t& event) {
/* Create a timer when the bot starts. */
bot.start_timer([&bot](const dpp::timer& timer){
/* Create a timer when the bot starts. */
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot](const dpp::http_request_completion_t& callback) {
/* Create a message to our desired channel, with the D++ logo. */
bot.message_create(dpp::message(1140010849432522843, "").add_file("image.png", callback.body));
});
}, 10); /* Do it every 10 seconds. Timers also start with this delay. */
});
bot.start(dpp::st_wait);
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:80
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ m_get
GET.
Definition: queues.h:185
size_t timer
Represents a timer handle. Returned from cluster::start_timer and used by cluster::stop_timer....
Definition: timer.h:39
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:63
The result of any HTTP request. Contains the headers, vital rate limit figures, and returned request ...
Definition: queues.h:110
std::string body
Reply body.
Definition: queues.h:161
Represents messages sent and received on Discord.
Definition: message.h:1988
Session ready.
Definition: dispatcher.h:961

If all went well, you should get the D++ logo sent every 10 seconds to your desired channel!

Now, let's make the same timer a one-shot timer, meaning it will only run once!

#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t& event) {
/* Create a timer when the bot starts. */
bot.start_timer([&bot](const dpp::timer& timer) {
/* Create a timer when the bot starts. */
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot, timer](const dpp::http_request_completion_t& callback) {
/* Create a message to our desired channel, with the D++ logo. */
bot.message_create(dpp::message(1140010849432522843, "").add_file("image.png", callback.body));
/* Stop the timer by passing the timer handle in. */
bot.stop_timer(timer);
});
}, 10); /* Do it every 10 seconds. Timers also start with this delay. */
});
bot.start(dpp::st_wait);
}

Great! Now we've learnt the basics of timers and how to stop them!

To finish off, let's make a timer that you can start and stop with commands. This example will store the timer in a map where the user is the owner of the timer!

#include <dpp/dpp.h>
std::map<dpp::snowflake, dpp::timer> user_timers{};
int main() {
/* Create the bot */
dpp::cluster bot("token");
/* 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() == "start_timer") {
/* Does user_timers contain the user id? */
if (user_timers.find(event.command.usr.id) != user_timers.end()) {
event.reply("You've already got an in-progress timer!");
return;
}
/* Create a copy of the channel_id to copy in to the timer lambda. */
dpp::snowflake channel_id = event.command.channel_id;
/* Start the timer and save it to a local variable. */
dpp::timer timer = bot.start_timer([&bot, channel_id](const dpp::timer& timer) {
bot.message_create(dpp::message(channel_id, "This is a timed message! Use /stop_timer to stop this!"));
}, 10);
/*
* Add the timer to user_timers.
* As dpp::timer is just size_t (essentially the timer's ID), it's perfectly safe to copy it in.
*/
user_timers.emplace(event.command.usr.id, timer);
event.reply("Started a timer every 10 seconds!");
}
if(event.command.get_command_name() == "stop_timer") {
/* Is user_timers empty? */
if (user_timers.empty()) {
event.reply("There are no timers currently in-progress!");
return;
} else if (user_timers.find(event.command.usr.id) == user_timers.end()) { /* Does user_timers not contain the user id? */
event.reply("You've don't currently have a timer in-progress!");
return;
}
/* Stop the timer. */
bot.stop_timer(user_timers[event.command.usr.id]);
/* Remove the timer from user_timers. */
user_timers.erase(event.command.usr.id);
event.reply("Stopped your timer!");
}
});
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 start_timer("start_timer", "Start a 10 second timer!", bot.me.id);
dpp::slashcommand stop_timer("stop_timer", "Stop your 10 second timer!", bot.me.id);
/* Register the commands. */
bot.global_bulk_command_create({ start_timer, stop_timer });
}
});
bot.start(dpp::st_wait);
}
snowflake channel_id
Optional: the channel it was sent from.
Definition: appcommand.h:975
std::string get_command_name() const
Get the command name for a command interaction.
user usr
User object for the invoking user.
Definition: appcommand.h:1005
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:39
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1339
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition: snowflake.h:50
void reply(command_completion_event_t callback=utility::log_error()) const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
interaction command
command interaction
Definition: dispatcher.h:678
User has issued a slash command.
Definition: dispatcher.h:695

If that went well, it should work something like below!

Great, now you've learnt how to store timers to manage at a later point!

D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0