editing-channels-and-messages

# Editing Channels and Messages

Sometimes we need to update an object, such as a message (whether it's plain text or an embed) or a channel. At first, it might seem confusing, but it's actually really simple! You need an object with all the properties being identical to the existing one. Say you're editing a message. You need to have an object with its ID the same as the one in Discord. Then you replace what you need, such as its content.

This example uses callback functions and embeds. To see more information about them, visit Using Callback Functions and Sending Embeds.

## Editing messages

Here we send a message and edit it after. To do so, we first reply to the command `msg-send` with some text, "This is a message" in our case. As described above, on the next step the message object is taken and the text is replaced with whatever the user desires.

    #include<dpp/dpp.h>

    intmain(){
    /*thesecondargumentisabitmaskofintents-i_message_contentisneededtogetmessages*/
    dpp::clusterbot("Token",dpp::i_default_intents|dpp::i_message_content);

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    if(event.command.get_command_name()=="msg-send"){
    event.reply("That'samessage");
    }elseif(event.command.get_command_name()=="msg-edit"){
    constautocontent=std::get<std::string>(event.get_parameter("content"));

    /*getthemessagetoedititafter.herestringwillautomaticallybeconvertedtosnowflake*/
    constdpp::snowflakemsg_id=std::get<std::string>(event.get_parameter("msg-id"));

    bot.message_get(msg_id,event.command.channel_id,[&bot,content,event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("error");
    return;
    }
    automessage=callback.get<dpp::message>();

    /*changethemessagecontentandeditthemessageitself*/
    message.set_content(content);
    bot.message_edit(message);
    event.reply("Messagecontentisnow`"+content+"`.");
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once <struct register_global_commands>()){
    dpp::slashcommandmsg_edit("msg-edit","Editamessagesentbythebot",bot.me.id);

    msg_edit.add_option(dpp::command_option(dpp::co_string,"msg-id","IDofthemessagetoedit",true));/*trueforrequiredoption*/
    msg_edit.add_option(dpp::command_option(dpp::co_string,"content","Newcontentforthemessage",true));/*samehere*/

    dpp::slashcommandmsg_send("msg-send","Sendmymessage",bot.me.id);

    bot.global_bulk_command_create({msg_edit,msg_send});
    }
    });

    bot.start(dpp::st_wait);
    return0;
    }

Your bot can't edit messages sent by others!Sending Embeds.

Before editing the message:

After editing the message:

## Editing channels

Now we'll want to edit an existing channel - its name in this case. This works similarly to how messages are edited.

    #include<dpp/dpp.h>

    intmain(){
    /*thesecondargumentisabitmaskofintents-i_message_contentisneededtogetmessages*/
    dpp::clusterbot("Token",dpp::i_default_intents|dpp::i_message_content);

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    if(event.command.get_command_name()=="channel-edit"){
    constautoname=std::get<std::string>(event.get_parameter("name"));

    /*getthechanneltoedititafter*/
    constautochannel_id=std::get<dpp::snowflake>(event.get_parameter("channel"));
    bot.channel_get(channel_id,[&bot,name,event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("error");
    return;
    }
    autochannel=callback.get<dpp::channel>();

    /*changethechannelnameandeditthechannelitself*/
    channel.set_name(name);
    bot.channel_edit(channel);
    event.reply("Channelnameisnow`"+name+"`.");
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){

    if(dpp::run_once <struct register_global_commands>()){
    dpp::slashcommandchannel_edit("channel-edit","Editthenameofchannelspecified",bot.me.id);

    channel_edit.add_option(dpp::command_option(dpp::co_channel,"channel","Channeltoedit",true));
    channel_edit.add_option(dpp::command_option(dpp::co_string,"name","Newnameforthechannel",true));

    bot.global_command_create(channel_edit);
    }
    });

    bot.start(dpp::st_wait);
    return0;
    }

Before editing the channel:

After editing the channel:

## Editing embeds

Now let's send an embed and edit it. If a message has one `content` field, it can have a few `embed` fields, up to 10 to be precise. So we first get the embed we want and edit and change its description.

    #include<dpp/dpp.h>

    intmain(){
    /*thesecondargumentisabitmaskofintents-i_message_contentisneededtogetmessages*/
    dpp::clusterbot("Token",dpp::i_default_intents|dpp::i_message_content);

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    if(event.command.get_command_name()=="embed-send"){
    dpp::embedembed=dpp::embed()
    .set_color(dpp::colors::sti_blue)
    .set_title("likeandsubscribe")
    .set_url("https://dpp.dev/")
    .set_author("Someauthor","https://dpp.dev/","https://dpp.dev/DPP-Logo.png")
    .set_description("Creatoris<creatorname>");
    event.reply(embed);
    }elseif(event.command.get_command_name()=="embed-edit"){
    constautodescription=std::get<std::string>(event.get_parameter("desc"));

    /*getthemessagetoedititsembedafter.herestringwillautomaticallybeconvertedtosnowflake*/
    constdpp::snowflakemsg_id=std::get<std::string>(event.get_parameter("msg-id"));

    bot.message_get(msg_id,event.command.channel_id,[&bot,description,event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("error");
    return;
    }
    automessage=callback.get<dpp::message>();
    auto&embeds=message.embeds;

    /*changetheembeddescriptionandeditthemessageitself.
    *sincewe'reusingareference,whatchangesinembedschangesinmessage.embeds
    */
    embeds[0].set_description(description);

    bot.message_edit(message);
    event.reply("Embeddescriptionisnow`"+description+"`.");
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once <struct register_global_commands>()){
    dpp::slashcommandembed_send("embed-send","Sendmyembed",bot.me.id);

    dpp::slashcommandembed_edit("embed-edit","Editanembedsentbythebot",bot.me.id);

    embed_edit.add_option(dpp::command_option(dpp::co_string,"msg-id","IDoftheembedtoedit",true));/*trueforrequiredoption*/
    embed_edit.add_option(dpp::command_option(dpp::co_string,"desc","Newdescriptionfortheembed",true));/*samehere*/

    bot.global_bulk_command_create({embed_send,embed_edit});
    }
    });

    bot.start(dpp::st_wait);
    return0;
    }

Before editing the embed:

Finally, after editing the embed: