D++ (DPP)
C++ Discord API Bot Library
Separating Events into New Classes

If you're someone that loves file organisation (or you hate how cluttered your main.cpp has become) then you may be interested in moving events into separate classes outside of the main.cpp file. This is a great way to improve readability and can be helpful in many cases! For example, you can have two classes on the same event, except one could be reading messages for spam and one could be reading messages for bad words!

In this tutorial, we'll be taking the Listening to messages example and moving the on_message_create event into a different class.

To get started, you can create a folder called listeners inside src (where your main.cpp is) if you'd like to put it there! We'll be doing exactly that so, if you'd like to stick along with the tutorial, get creating that folder!

Now, you can create a new header and cpp file in this folder! For this tutorial, we'll be naming both these files message_listener!

If you're using CMake, you'll need to add this to your CMakeLists.txt. Some IDEs automatically do this but it's always worth double-checking!

Once that's done, it should look similar to this (this screenshot has more files in, so it won't be identical!):

First, we need to define the function that will be called when the event fires. We do this in the message_listener.h, like so:

#pragma once
#include <dpp/dpp.h>
class message_listener {
public:
/* Create a static function that can be called anywhere. */
static void on_message_create(const dpp::message_create_t& event);
};
Create message.
Definition: dispatcher.h:1635

Then we need to add our code for what should happen when this event fires. We do this in the message_listener.cpp, like so:

#include "message_listener.h"
void message_listener::on_message_create(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);
}
}
message msg
message that was created (sent).
Definition: dispatcher.h:1642
std::string content
Contents of the message.
Definition: message.h:2116

Now, you'll have a nice area where you can easily see the code, without scrolling through all of your main.cpp file just to get to this event!

However, we've not finished yet! If you thought "How does the `main.cpp` file actually know to call this?" then, 10 points to you! It doesn't know! We need to go do that now. So, let's do exactly that.

#include <dpp/dpp.h>
#include "listeners/message_listener.h"
int main() {
/* Create the bot, but with our intents so we can use messages. */
/* Fires our event that is located in MessageListener
* when the bot detects a message in any server and any channel it has access to.
*/
bot.on_message_create(&message_listener::on_message_create);
bot.start(dpp::st_wait);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:80
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:112
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition: intents.h:132
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:63

And there we go! How tidy is that?

Now, the possibilities to this are not limited. If you wish to do this twice (as I explained at first), you can simply have another class and just copy the bot.on_message_create line below in the main.cpp file and then you can change it to reference the second class, meaning you have two message events firing in two separate classes!

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ 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