| 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 |
nothing calls this directly
no test coverage detected