attach-file

# Attaching a File to a Message

To attach a local file to an message, you can use dpp::utility::read_file. It's a helper function from D++ that allows you to read the file's content and sent it to discord.

An example of this:

    #include<dpp/dpp.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()=="file"){
    dpp::messagemsg(event.command.channel_id,"Heythere,I'vegotanewfile!");

    /*attachthefiletothemessage*/
    msg.add_file("foobar.txt",dpp::utility::read_file("path_to_your_file.txt"));

    /*Replytotheuserwiththemessage,withourfileattached.*/
    event.reply(msg);
    }
    });

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

    bot.start(dpp::st_wait);

    return0;
    }

Attachments via URL aren't possible. But there's a workaround for this! You can download the file and then attach it to the message.

Amazingly, D++ also has a function for this! You can use dpp::cluster::request to make HTTP requests, allowing you to go ahead and pull the content from a URL.

The following example program shows how to request a file and attach it to a message:

    #include<dpp/dpp.h>

    intmain(){
    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()=="file"){

    /*RequesttheimagefromtheURLspecifiedandcapturetheeventinalambda.*/
    bot.request("https://dpp.dev/DPP-Logo.png",dpp::m_get,[event](constdpp::http_request_completion_t&httpRequestCompletion){

    /*Createamessage*/
    dpp::messagemsg(event.command.channel_id,"Thisismynewattachment:");

    /*Attachtheimagetothemessage,onlyonsuccess(Code200).*/
    if(httpRequestCompletion.status==200){
    msg.add_file("logo.png",httpRequestCompletion.body);
    }

    /*Sendthemessage,withourattachment.*/
    event.reply(msg);
    });
    }
    });

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

    bot.start(dpp::st_wait);

    return0;
    }

Here's another example of how to add a local image to an embed.

Upload the image in the same message as the embed and then reference it in the embed.

    #include<dpp/dpp.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()=="file"){
    /*Createamessage.*/
    dpp::messagemsg(event.command.channel_id,"");

    /*Attachtheimagetothemessagewejustcreated.*/
    msg.add_file("image.jpg",dpp::utility::read_file("path_to_your_image.jpg"));

    /*Createanembed.*/
    dpp::embedembed;
    embed.set_image("attachment://image.jpg");/*Settheimageoftheembedtotheattachedimage.*/

    /*Addtheembedtothemessage.*/
    msg.add_embed(embed);

    event.reply(msg);
    }
    });

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

    bot.start(dpp::st_wait);

    return0;
    }