MCPcopy Create free account
hub / github.com/brainboxdotcc/DPP / main

Function main

docpages/example_code/timers_example3.cpp:5–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3std::map<dpp::snowflake, dpp::timer> user_timers{};
4
5int 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

Callers

nothing calls this directly

Calls 14

cout_loggerFunction · 0.85
get_command_nameMethod · 0.80
start_timerMethod · 0.80
message_createMethod · 0.80
emplaceMethod · 0.80
stop_timerMethod · 0.80
eraseMethod · 0.80
startMethod · 0.80
messageClass · 0.50
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected