D++ (DPP)
C++ Discord API Bot Library
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).

Warning
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>
int main() {
/* Create the bot, but with our intents so we can use messages. */
/* The event is fired when the bot detects a message in any server and any channel it has access to. */
bot.on_message_create([&bot](const dpp::message_create_t& event) {
/* See if the message contains the phrase we want to check for.
* If there's at least a single match, we reply and say it's not allowed.
*/
if (event.msg.content.find("bad word") != std::string::npos) {
event.reply("That is not allowed here. Please, mind your language!", true);
}
});
bot.start(dpp::st_wait);
return 0;
}

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

dpp::message_create_t::msg
message msg
message that was created (sent).
Definition: dispatcher.h:1662
dpp::i_default_intents
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition: intents.h:172
dpp::message::content
std::string content
Contents of the message.
Definition: message.h:2080
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:101
dpp::message_create_t
Create message.
Definition: dispatcher.h:1655
dpp::i_message_content
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:152
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
Definition: dispatcher.h:228
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0