editing_message_after_click

# Editing The Message From a Button Click

This page expects you to be familiar with Button Clicks and extends from the Using Button Components page. Please visit the Using Button Components page if you are not already familiar with Button Clicks.

Editing messages where you had a button click can be done in a couple ways.

If you want to edit the message that had the buttons on, instead of doing `event.reply("message");`, you would do `event.reply(dpp::ir_update_message, "message");`, like so:

You are still limited to the default interaction time (3 seconds) this way. Read on if you need more time!

    #include<dpp/dpp.h>
    #include<dpp/unicode_emoji.h>

    intmain(){
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="button"){
    /*Createamessage*/
    dpp::messagemsg(event.command.channel_id,"thistexthasabutton");

    /*Addanactionrow,andthenabuttonwithintheactionrow.*/
    msg.add_component(
    dpp::component().add_component(
    dpp::component()
    .set_label("Clickme!")
    .set_type(dpp::cot_button)
    .set_emoji(dpp::unicode_emoji::smile)
    .set_style(dpp::cos_danger)
    .set_id("myid")
    )
    );

    /*Replytotheuserwithourmessage.*/
    event.reply(msg);
    }
    });

    /*Whenauserclicksyourbutton,theon_button_clickeventwillfire,
    *containingthecustom_idyoudefinedinyourbutton.
    */
    bot.on_button_click([](constdpp::button_click_t&event){
    /*Insteadofreplyingtothebuttonclickitself,
    *wewanttoupdatethemessagethathadthebuttonsonit.
    */
    event.reply(dpp::ir_update_message,"Youclicked:"+event.custom_id);
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisteracommandwhenthebotisready*/
    bot.global_command_create(dpp::slashcommand("button","Sendamessagewithabutton!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

However, if you're going to take longer than 3 seconds to respond, you need to tell Discord to wait. The usual method is `event.thinking(true);` and then `event.edit_response("I have thought long and hard about my actions.");`, however, `event.thinking()` will create a new response if called from `on_button_click`, meaning you can no longer respond to the original response as you already did a response!

Instead, you want to do `event.reply(dpp::ir_deferred_channel_message_with_source, "");` to tell Discord that you intend on editing the message that the button click came from, but you need time. The user will be informed that you've processed the button click (as required) via a loading state and then you have 15 minutes to do everything you need. To then edit the message, you need to do `event.edit_response("new message!");`, like so:

If you want to silently acknowledge the button click (no thinking message), replace dpp::ir_deferred_channel_message_with_source with dpp::ir_deferred_update_message. You will still have 15 minutes to make a response.

    #include<dpp/dpp.h>
    #include<dpp/unicode_emoji.h>

    intmain(){
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="button"){
    /*Createamessage*/
    dpp::messagemsg(event.command.channel_id,"thistexthasabutton");

    /*Addanactionrow,andthenabuttonwithintheactionrow.*/
    msg.add_component(
    dpp::component().add_component(
    dpp::component()
    .set_label("Clickme!")
    .set_type(dpp::cot_button)
    .set_emoji(dpp::unicode_emoji::smile)
    .set_style(dpp::cos_danger)
    .set_id("myid")
    )
    );

    /*Replytotheuserwithourmessage.*/
    event.reply(msg);
    }
    });

    /*Whenauserclicksyourbutton,theon_button_clickeventwillfire,
    *containingthecustom_idyoudefinedinyourbutton.
    */
    bot.on_button_click([](constdpp::button_click_t&event){
    /*Insteadofreplyingtothebuttonclickitself,
    *wewanttoupdatethemessagethathadthebuttonsonit.
    */
    event.reply(dpp::ir_deferred_channel_message_with_source,"");

    /*Pretendyou'redoinglongcallsherethatmaytakelongerthan3seconds.*/

    /*Now,edittheresponse!*/
    event.edit_response("Afterawhile,Icanconfirmyouclicked:"+event.custom_id);
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisteracommandwhenthebotisready*/
    bot.global_command_create(dpp::slashcommand("button","Sendamessagewithabutton!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }