D++ (DPP)
C++ Discord API Bot Library
|
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:
D++ | Discord.js |
---|---|
#include <dpp/dpp.h>
const std::string BOT_TOKEN = "add your token here";
const dpp::snowflake MY_GUILD_ID = 825407338755653642;
int main() {
dpp::cluster bot(BOT_TOKEN);
bot.on_interaction_create([](const dpp::interaction_create_t& event) {
event.reply("Pong!");
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
bot.guild_command_create(
MY_GUILD_ID
);
}
});
bot.on_log(dpp::utility::cout_logger());
bot.start(false);
}
The cluster class represents a group of shards and a command queue for sending and receiving commands... Definition: cluster.h:392 std::string get_command_name() const Get the command name for a command interaction. Represents an application command, created by your bot either globally, or on a guild. Definition: appcommand.h:762 std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger() Get a default logger that outputs to std::cout. e.g. uint64_t snowflake A 64 bit unsigned value representing many things on discord. Discord calls the value a 'snowflake' va... Definition: snowflake.h:32 interaction command command interaction Definition: dispatcher.h:395 |
let Discord = require('discord.js');
let BOT_TOKEN = 'add your token here';
let MY_GUILD_ID = '825407338755653642';
let bot = new Discord.Client({ intents: [] });
bot.on('interactionCreate', (interaction) => {
if (interaction.isCommand() && interaction.commandName === 'ping') {
interaction.reply({content: 'Pong!'});
}
});
bot.once('ready', async () => {
let guild = await bot.guilds.fetch(MY_GUILD_ID);
await guild.commands.create({
name: 'ping',
description: "Ping pong!"
});
});
bot.login(BOT_TOKEN);
intents intents are a bitmask of allowed events on your websocket. Definition: intents.h:32 |
Let's break this program down step by step:
Make sure to include the header file for the D++ library with the instruction #include <dpp/dpp.h>
!
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 constant!
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. In this event, we will register a slash command called 'ping'. We register this slash command against a guild, as it takes an hour for global commands to appear. Note that we must wrap our registration of the command in a template called dpp::run_once
which prevents it from being re-run every time your bot does a full reconnection (e.g. if the connection fails).
If you want to handle a slash command, you should also attach your program to the on_interaction_create
event (dpp::cluster::on_interaction_create) which is the same as the Discord.js interactionCreate
event. Lets add this to the program before the on_ready
event:
Attaching to an event is a good start, but to make a bot you should actually put some program code into the interaction event. We will add some code to the on_interaction_create
to look for our slash command '/ping' and reply with Pong!
:
Let's break down the code in the on_interaction_create
event so that we can discuss what it is doing:
This code is simply comparing the command name event.command.get_command_name()
(dpp::interaction::get_command_name) against the value in a constant string value "ping"
. If they match, then the event.reply
method is called.
The event.reply
function (dpp::interaction_crete_t::reply) replies to a slash command with 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.
To make the bot start, we must call the cluster::start method, e.g. in our program by using bot.start(false)
.
We also add a line to tell the library to output all its log information to the console, bot.on_log(dpp::utility::cout_logger());
- if you wanted to do something more advanced, you can replace this parameter with a lambda just like all other events.
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.
Compile your bot using g++ -std=c++17 -o test test.cpp -ldpp
(if your .cpp file is called test.cpp
) and run it with ./test
.
When you invite your bot, you must use the applications.commands
and bots
scopes to ensure your bot can create guild slash commands. For example:
Replace YOUR-BOTS-ID-HERE
with your bot's user ID, and BOT-PERMISSIONS-HERE
with the permissions your bot requires.
Congratulations - you now have a working bot written using the D++ library!