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() {
bot.message_create(
dpp::message(1140010849432522843,
"").add_file(
"image.png", callback.
body));
});
}, 10);
});
}
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() {
bot.message_create(
dpp::message(1140010849432522843,
"").add_file(
"image.png", callback.
body));
bot.stop_timer(timer);
});
}, 10);
});
}
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() {
if (user_timers.find(event.command.usr.id) != user_timers.end()) {
event.reply("You've already got an in-progress timer!");
return;
}
bot.message_create(dpp::message(channel_id, "This is a timed message! Use /stop_timer to stop this!"));
}, 10);
event.
reply(
"Started a timer every 10 seconds!");
}
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()) {
event.reply("You've don't currently have a timer in-progress!");
return;
}
event.
reply(
"Stopped your timer!");
}
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_bulk_command_create({ start_timer, stop_timer });
}
});
}
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!