private-messaging

# Sending private messages

Sometimes it's simply not enough to ping someone in a server with a message, and we get that. That's why you can private message people! This tutorial will cover how to make a command that will either message the author of the command or message a specified user!

This tutorial makes use of callbacks. For more information about that, visit Using Callback Functions.

    #include<dpp/dpp.h>

    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()=="pm"){
    dpp::snowflakeuser;

    /*Iftherewasnospecifieduser,wesetthe"user"variabletothecommandauthor(issuinguser).*/
    if(event.get_parameter("user").index()==0){
    user=event.command.get_issuing_user().id;
    }else{/*Otherwise,wesetittothespecifieduser!*/
    user=std::get<dpp::snowflake>(event.get_parameter("user"));
    }

    /*Sendamessagetotheusersetabove.*/
    bot.direct_message_create(user,dpp::message("Here'saprivatemessage!"),[event,user](constdpp::confirmation_callback_t&callback){
    /*Ifthecallbackerrors,wewanttosendamessagetellingtheauthorthatsomethingwentwrong.*/
    if(callback.is_error()){
    /*Here,wewanttheerrormessagetobedifferentiftheuserwe'retryingtosendamessagetoisthecommandauthor.*/
    if(user==event.command.get_issuing_user().id){
    event.reply(dpp::message("Icouldn'tsendyouamessage.").set_flags(dpp::m_ephemeral));
    }else{
    event.reply(dpp::message("Icouldn'tsendamessagetothatuser.Pleasecheckthatisavaliduser!").set_flags(dpp::m_ephemeral));
    }

    return;
    }

    /*Wedothesamehere,sothemessageisdifferentifit'stothecommandauthororifit'stoaspecifieduser.*/
    if(user==event.command.get_issuing_user().id){
    event.reply(dpp::message("I'vesentyouaprivatemessage.").set_flags(dpp::m_ephemeral));
    }else{
    event.reply(dpp::message("I'vesentamessagetothatuser.").set_flags(dpp::m_ephemeral));
    }
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Registerthecommand*/
    dpp::slashcommandcommand("pm","Sendaprivatemessage.",bot.me.id);

    /*Addtheoptionforausermentionthatisn'trequired*/
    command.add_option(dpp::command_option(dpp::co_mentionable,"user","Theusertomessage",false));

    /*Registerthecommand*/
    bot.global_command_create(command);
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

That's it! Now, you should have something like this: