Skip to content

Commit f87c0f7

Browse files
authored
docs: added a timers page, added a note in setting_status for timers (#1120)
1 parent 3ac877c commit f87c0f7

File tree

8 files changed

+143
-0
lines changed

8 files changed

+143
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <dpp/dpp.h>
2+
3+
int main() {
4+
/* Create the bot */
5+
dpp::cluster bot("token");
6+
7+
bot.on_log(dpp::utility::cout_logger());
8+
9+
bot.on_ready([&bot](const dpp::ready_t& event) {
10+
/* Create a timer when the bot starts. */
11+
bot.start_timer([&bot](const dpp::timer& timer){
12+
/* Create a timer when the bot starts. */
13+
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot](const dpp::http_request_completion_t& callback) {
14+
/* Create a message to our desired channel, with the D++ logo. */
15+
bot.message_create(dpp::message(1140010849432522843, "").add_file("image.png", callback.body));
16+
});
17+
}, 10); /* Do it every 10 seconds. Timers also start with this delay. */
18+
});
19+
20+
bot.start(dpp::st_wait);
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <dpp/dpp.h>
2+
3+
int main() {
4+
/* Create the bot */
5+
dpp::cluster bot("token");
6+
7+
bot.on_log(dpp::utility::cout_logger());
8+
9+
bot.on_ready([&bot](const dpp::ready_t& event) {
10+
/* Create a timer when the bot starts. */
11+
bot.start_timer([&bot](const dpp::timer& timer) {
12+
/* Create a timer when the bot starts. */
13+
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot, timer](const dpp::http_request_completion_t& callback) {
14+
/* Create a message to our desired channel, with the D++ logo. */
15+
bot.message_create(dpp::message(1140010849432522843, "").add_file("image.png", callback.body));
16+
/* Stop the timer by passing the timer handle in. */
17+
bot.stop_timer(timer);
18+
});
19+
}, 10); /* Do it every 10 seconds. Timers also start with this delay. */
20+
});
21+
22+
bot.start(dpp::st_wait);
23+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <dpp/dpp.h>
2+
3+
std::map<dpp::snowflake, dpp::timer> user_timers{};
4+
5+
int main() {
6+
/* Create the bot */
7+
dpp::cluster bot("token");
8+
9+
bot.on_log(dpp::utility::cout_logger());
10+
11+
/* The event is fired when someone issues your commands */
12+
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
13+
/* Check which command they ran */
14+
if (event.command.get_command_name() == "start_timer") {
15+
/* Does user_timers contain the user id? */
16+
if (user_timers.find(event.command.usr.id) != user_timers.end()) {
17+
event.reply("You've already got an in-progress timer!");
18+
return;
19+
}
20+
21+
/* Create a copy of the channel_id to copy in to the timer lambda. */
22+
dpp::snowflake channel_id = event.command.channel_id;
23+
24+
/* Start the timer and save it to a local variable. */
25+
dpp::timer timer = bot.start_timer([&bot, channel_id](const dpp::timer& timer) {
26+
bot.message_create(dpp::message(channel_id, "This is a timed message! Use /stop_timer to stop this!"));
27+
}, 10);
28+
29+
/*
30+
* Add the timer to user_timers.
31+
* As dpp::timer is just size_t (essentially the timer's ID), it's perfectly safe to copy it in.
32+
*/
33+
user_timers.emplace(event.command.usr.id, timer);
34+
35+
event.reply("Started a timer every 10 seconds!");
36+
}
37+
38+
if(event.command.get_command_name() == "stop_timer") {
39+
/* Is user_timers empty? */
40+
if (user_timers.empty()) {
41+
event.reply("There are no timers currently in-progress!");
42+
return;
43+
} else if (user_timers.find(event.command.usr.id) == user_timers.end()) { /* Does user_timers not contain the user id? */
44+
event.reply("You've don't currently have a timer in-progress!");
45+
return;
46+
}
47+
48+
/* Stop the timer. */
49+
bot.stop_timer(user_timers[event.command.usr.id]);
50+
/* Remove the timer from user_timers. */
51+
user_timers.erase(event.command.usr.id);
52+
53+
event.reply("Stopped your timer!");
54+
}
55+
});
56+
57+
bot.on_ready([&bot](const dpp::ready_t& event) {
58+
if (dpp::run_once<struct register_bot_commands>()) {
59+
/* Create a new global command on ready event. */
60+
dpp::slashcommand start_timer("start_timer", "Start a 10 second timer!", bot.me.id);
61+
dpp::slashcommand stop_timer("stop_timer", "Stop your 10 second timer!", bot.me.id);
62+
63+
/* Register the commands. */
64+
bot.global_bulk_command_create({ start_timer, stop_timer });
65+
}
66+
});
67+
68+
bot.start(dpp::st_wait);
69+
}

docpages/example_programs/misc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This section lists examples that do not fit neatly into any of the categories ab
1212
* \subpage checking-member-permissions
1313
* \subpage setting_status
1414
* \subpage using-emojis
15+
* \subpage using_timers

docpages/example_programs/misc/setting_status.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ You can also play around with dpp::at_game, changing it to something like dpp::a
1717

1818
Now, let's cover setting the bot status to say `Playing with x guilds!` every two minutes.
1919

20+
\note This example uses timers to update the status every 2 minutes. If you aren't familiar with D++'s own timers, please read \ref using_timers "this page on timers" before you continue.
21+
2022
\include{cpp} setting_status2.cpp
2123

2224
If you followed that well, your bot should now say this on members list!
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
\page using_timers Using Timers
2+
3+
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!
4+
5+
First, we'll cover sending the D++ logo every 10 seconds!
6+
7+
\include{cpp} timers_example1.cpp
8+
9+
If all went well, you should get the D++ logo sent every 10 seconds to your desired channel!
10+
11+
\image html timers_example1.png
12+
13+
Now, let's make the same timer a one-shot timer, meaning it will only run once!
14+
15+
\include{cpp} timers_example2.cpp
16+
17+
Great! Now we've learnt the basics of timers and how to stop them!
18+
19+
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!
20+
21+
\include{cpp} timers_example3.cpp
22+
23+
If that went well, it should work something like below!
24+
25+
\image html timers_example3.png
26+
27+
Great, now you've learnt how to store timers to manage at a later point!

docpages/images/timers_example1.png

18.3 KB
Loading

docpages/images/timers_example3.png

47.4 KB
Loading

0 commit comments

Comments
 (0)