using_timers

# 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>

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

    bot.on_log(dpp::utility::cout_logger());

    bot.on_ready([&bot](constdpp::ready_t&event){
    /*Createatimerwhenthebotstarts.*/
    bot.start_timer([&bot](constdpp::timer&timer){
    /*Createatimerwhenthebotstarts.*/
    bot.request("https://dpp.dev/DPP-Logo.png",dpp::m_get,[&bot](constdpp::http_request_completion_t&callback){
    /*Createamessagetoourdesiredchannel,withtheD++logo.*/
    bot.message_create(dpp::message(1140010849432522843,"").add_file("image.png",callback.body));
    });
    },10);/*Doitevery10seconds.Timersalsostartwiththisdelay.*/
    });

    bot.start(dpp::st_wait);
    }

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>

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

    bot.on_log(dpp::utility::cout_logger());

    bot.on_ready([&bot](constdpp::ready_t&event){
    /*Createatimerwhenthebotstarts.*/
    bot.start_timer([&bot](constdpp::timer&timer){
    /*Createatimerwhenthebotstarts.*/
    bot.request("https://dpp.dev/DPP-Logo.png",dpp::m_get,[&bot,timer](constdpp::http_request_completion_t&callback){
    /*Createamessagetoourdesiredchannel,withtheD++logo.*/
    bot.message_create(dpp::message(1140010849432522843,"").add_file("image.png",callback.body));
    /*Stopthetimerbypassingthetimerhandlein.*/
    bot.stop_timer(timer);
    });
    },10);/*Doitevery10seconds.Timersalsostartwiththisdelay.*/
    });

    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{};

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

    bot.on_log(dpp::utility::cout_logger());

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="start_timer"){
    /*Doesuser_timerscontaintheuserid?*/
    if(user_timers.find(event.command.usr.id)!=user_timers.end()){
    event.reply("You'vealreadygotanin-progresstimer!");
    return;
    }

    /*Createacopyofthechannel_idtocopyintothetimerlambda.*/
    dpp::snowflakechannel_id=event.command.channel_id;

    /*Startthetimerandsaveittoalocalvariable.*/
    dpp::timertimer=bot.start_timer([&bot,channel_id](constdpp::timer&timer){
    bot.message_create(dpp::message(channel_id,"Thisisatimedmessage!Use/stop_timertostopthis!"));
    },10);

    /*
    *Addthetimertouser_timers.
    *Asdpp::timerisjustsize_t(essentiallythetimer'sID),it'sperfectlysafetocopyitin.
    */
    user_timers.emplace(event.command.usr.id,timer);

    event.reply("Startedatimerevery10seconds!");
    }

    if(event.command.get_command_name()=="stop_timer"){
    /*Isuser_timersempty?*/
    if(user_timers.empty()){
    event.reply("Therearenotimerscurrentlyin-progress!");
    return;
    }elseif(user_timers.find(event.command.usr.id)==user_timers.end()){/*Doesuser_timersnotcontaintheuserid?*/
    event.reply("You'vedon'tcurrentlyhaveatimerin-progress!");
    return;
    }

    /*Stopthetimer.*/
    bot.stop_timer(user_timers[event.command.usr.id]);
    /*Removethetimerfromuser_timers.*/
    user_timers.erase(event.command.usr.id);

    event.reply("Stoppedyourtimer!");
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createanewglobalcommandonreadyevent.*/
    dpp::slashcommandstart_timer("start_timer","Starta10secondtimer!",bot.me.id);
    dpp::slashcommandstop_timer("stop_timer","Stopyour10secondtimer!",bot.me.id);

    /*Registerthecommands.*/
    bot.global_bulk_command_create({start_timer,stop_timer});
    }
    });

    bot.start(dpp::st_wait);
    }

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!