detecting-messages

# Listening to messages

Sometimes, you may want to listen out for a message, rather than a command. This could be used for many cases like a spam filter, a bot that would respond to movie quotes with gifs, or a chat bot! However, in this page, we'll be using this to create a moderation filter (detect bad words).

As of August 30th, 2022, Discord made Message Content a privileged intent. Whilst this means you can still use prefixed messages as commands, Discord does not encourage this and heavily suggests you use slash commands. If you wish to create commands, use slash commands, not messages.

    #include<dpp/dpp.h>

    intmain(){
    /*Createthebot,butwithourintentssowecanusemessages.*/
    dpp::clusterbot("token",dpp::i_default_intents|dpp::i_message_content);

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

    /*Theeventisfiredwhenthebotdetectsamessageinanyserverandanychannelithasaccessto.*/
    bot.on_message_create([&bot](constdpp::message_create_t&event){
    /*Seeifthemessagecontainsthephrasewewanttocheckfor.
    *Ifthere'satleastasinglematch,wereplyandsayit'snotallowed.
    */
    if(event.msg.content.find("badword")!=std::string::npos){
    event.reply("Thatisnotallowedhere.Please,mindyourlanguage!",true);
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

If all went well, you should have something like this!