D++ (DPP)
C++ Discord API Bot Library
Creating Your First Bot

In this example we will create a C++ version of the discord.js example program.

The two programs can be seen side by side below:

C++/DPP JavaScript/Discord.js

Let's break this program down step by step:

1. Start with an empty C++ program

Make sure to include the header file for the D++ library with the instruction #include <dpp/dpp.h>!

#include <dpp/dpp.h>
#include <iostream>
int main()
{
return 0;
}

2. Create an instance of dpp::cluster

To make use of the library you must create a dpp::cluster object. This object is the main object in your program like the Discord.Client object in Discord.js.

You can instantiate this class as shown below. Remember to put your bot token in the code where it says "token"!

#include <dpp/dpp.h>
#include <iostream>
int main()
{
dpp::cluster bot("token");
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:221

3. Attach to an event

To have a bot that does something, you should attach to some events. Let's start by attaching to the on_ready event (dpp::cluster::on_ready) which will notify your program when the bot is connected:

#include <dpp/dpp.h>
#include <iostream>
int main()
{
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t & event) {
});
return 0;
}
Session ready.
Definition: dispatcher.h:291

4. Attach to another event to reveice messages

If you want to receive messages, you should also attach your program to the on_message_create event (dpp::cluster::on_message_create) which is the same as the Discord.js message event. You add this to your program after the on_ready event:

#include <dpp/dpp.h>
#include <iostream>
int main()
{
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t & event) {
});
bot.on_message_create([&bot](const dpp::message_create_t & event) {
});
return 0;
}
Create message.
Definition: dispatcher.h:667

5 . Add some content to the events

Attaching to an event is a good start, but to make a bot you should actually put some program code into the events. Lets add some simple things into the events. We will add some code to the on_ready event to output the bot's nickname (dpp::cluster::me) and some code into the on_message_create to look for messages that are the text !ping and reply with !pong:

#include <dpp/dpp.h>
#include <iostream>
int main()
{
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t & event) {
std::cout << "Logged in as " << bot.me.username << "!\n";
});
bot.on_message_create([&bot](const dpp::message_create_t & event) {
if (event.msg->content == "!ping") {
bot.message_create(dpp::message(event.msg->channel_id, "Pong!"));
}
});
bot.start(false);
return 0;
}
message * msg
Definition: dispatcher.h:673
std::string content
Definition: message.h:847

Let's break down the code in the on_message_create event so that we can discuss what it is doing:

if (event.msg->content == "!ping") {
bot.message_create(dpp::message(event.msg->channel_id, "Pong!"));
}
Represents messages sent and received on Discord.
Definition: message.h:835
snowflake channel_id
Definition: message.h:839

This code is simply comparing the message content event.msg->content (dpp::message_create_t::content) against the value in a constant string value "!ping". If they match, then the message_create function is called.

The message_create function (dpp::cluster::message_create) sends a message. There are many ways to call this function to send embed messages, upload files, and more, but for this simple demonstration we will just send some message text. The message_create function accepts a dpp::message object, which we create using two parameters:

  • The channel ID to send to, which we get from event.msg->channel_id (dpp::message_create_t::channel_id)
  • The message content, which for this demonstration we just make the static text "Pong!".

6. Add code to start the bot!

To make the bot start, we must call the cluster::start method, e.g. in our program by using bot.start(false).

The parameter which we set to false indicates if the function should return once all shards are created. Passing false here tells the program you do not need to do anything once bot.start is called, so the return statement directly afterwards is never reached:

#include <dpp/dpp.h>
#include <iostream>
int main()
{
dpp::cluster bot("token");
bot.on_ready([&bot](const dpp::ready_t & event) {
std::cout << "Logged in as " << bot.me.username << "!\n";
});
bot.on_message_create([&bot](const dpp::message_create_t & event) {
if (event.msg->content == "!ping") {
bot.message_create(dpp::message(event.msg->channel_id, "Pong!"));
}
});
bot.start(false);
return 0;
}

7. Run your bot

Compile your bot using cmake .. and make from the build directory, and run it with ./test - Congratulations, you now have a working bot written using the D++ library!

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