Creating Your First Bot - D++ - The lightweight C++ Discord API Library (2024)

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";

int main() {

dpp::cluster bot(BOT_TOKEN);

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

bot.on_slashcommand([](const dpp::slashcommand_t& event) {

if (event.command.get_command_name() == "ping") {

event.reply("Pong!");

}

});

bot.on_ready([&bot](const dpp::ready_t& event) {

if (dpp::run_once<struct register_bot_commands>()) {

bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));

}

});

bot.start(dpp::st_wait);

}

dpp::cluster

The cluster class represents a group of shards and a command queue for sending and receiving commands...

Definition: cluster.h:80

dpp::interaction::get_command_name

std::string get_command_name() const

Get the command name for a command interaction.

dpp::slashcommand

Represents an application command, created by your bot either globally, or on a guild.

Definition: appcommand.h:1339

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.

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:63

dpp::interaction_create_t::command

interaction command

command interaction

Definition: dispatcher.h:678

dpp::ready_t

Session ready.

Definition: dispatcher.h:961

dpp::slashcommand_t

User has issued a slash command.

Definition: dispatcher.h:695

let Discord = require('discord.js');

let BOT_TOKEN = 'add your token here';

let bot = new Discord.Client({ intents: [] });

bot.on('interactionCreate', (interaction) => {

if (interaction.isCommand() && interaction.commandName === 'ping') {

interaction.reply({content: 'Pong!'});

}

});

bot.once('ready', async () => {

await client.commands.create({

name: 'ping',

description: "Ping pong!"

});

});

bot.login(BOT_TOKEN);‍

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>

int main() {

}

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 constant!

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "add your token here";

int main() {

dpp::cluster bot(BOT_TOKEN);

}

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. In this event, we will register a slash command called 'ping'. 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).

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "add your token here";

int main() {

dpp::cluster bot(BOT_TOKEN);

bot.on_ready([&bot](const dpp::ready_t& event) {

if (dpp::run_once<struct register_bot_commands>()) {

bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));

}

});

}

4. Attach to another event to receive slash commands

If you want to handle a slash command, you should also attach your program to the on_slashcommand event (dpp::cluster::on_slashcommand) which is basically the same as the Discord.js interactionCreate event. Lets add this to the program before the on_ready event:

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "add your token here";

int main() {

dpp::cluster bot(BOT_TOKEN);

bot.on_slashcommand([](const dpp::slashcommand_t& event) {

});

bot.on_ready([&bot](const dpp::ready_t& event) {

if (dpp::run_once<struct register_bot_commands>()) {

bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));

}

});

}

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 interaction event. We will add some code to the on_slashcommand to look for our slash command '/ping' and reply with Pong!:

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "add your token here";

int main() {

dpp::cluster bot(BOT_TOKEN);

bot.on_slashcommand([](const dpp::slashcommand_t& event) {

if (event.command.get_command_name() == "ping") {

event.reply("Pong!");

}

});

bot.on_ready([&bot](const dpp::ready_t& event) {

if (dpp::run_once<struct register_bot_commands>()) {

bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));

}

});

}

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

bot.on_slashcommand([](const dpp::slashcommand_t& event) {

if (event.command.get_command_name() == "ping") {

event.reply("Pong!");

}

});

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::slashcommand_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.

6. Add code to start the bot!

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

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 dpp::st_wait here tells the program you do not need to do anything once bot.start is called.

#include <dpp/dpp.h>

const std::string BOT_TOKEN = "add your token here";

int main() {

dpp::cluster bot(BOT_TOKEN);

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

bot.on_slashcommand([](const dpp::slashcommand_t& event) {

if (event.command.get_command_name() == "ping") {

event.reply("Pong!");

}

});

bot.on_ready([&bot](const dpp::ready_t& event) {

if (dpp::run_once<struct register_bot_commands>()) {

bot.global_command_create(dpp::slashcommand("ping", "Ping pong!", bot.me.id));

}

});

bot.start(dpp::st_wait);

}

7. Compile and run your bot

Compile your bot using g++ -std=c++17 -o bot bot.cpp -ldpp (if your .cpp file is called bot.cpp) and run it with ./bot.

8. Inviting your bot to your server

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:

https://discord.com/oauth2/authorize?client_id=YOUR-BOTS-ID-HERE&scope=bot+applications.commands&permissions=BOT-PERMISSIONS-HERE

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 using the D++ library!

Creating Your First Bot - D++ - The lightweight C++ Discord API Library (2024)

FAQs

Creating Your First Bot - D++ - The lightweight C++ Discord API Library? ›

But if you want extra features and more control over what the bot does, consider developing a custom bot from scratch. Programming languages like JavaScript, Python, and C++ have numerous built-in libraries you can use to create Discord bots.

Can you create a Discord bot with C++? ›

But if you want extra features and more control over what the bot does, consider developing a custom bot from scratch. Programming languages like JavaScript, Python, and C++ have numerous built-in libraries you can use to create Discord bots.

What is the best C++ library for Discord? ›

D++ is a lightweight and simple library for Discord written in modern C++. It is designed to cover as much of the API specification as possible and to have a incredibly small memory footprint, even when caching large amounts of data.

How do I make a Discord bot API? ›

How to make your own Discord bot:
  1. Turn on “Developer mode” in your Discord account.
  2. Click on “Discord API”.
  3. In the Developer portal, click on “Applications”. ...
  4. Name the bot and then click “Create”.
  5. Go to the “Bot” menu and generate a token using “Add Bot”.
  6. Program your bot using the bot token and save the file.
Nov 30, 2022

Is making a Discord bot illegal? ›

No, bots are allowed if they are on a bot account. Using a bot on a user account is against the Discord Terms of Service.

What is the easiest programming language for Discord bot? ›

You can code a Discord bot using JavaScript or Python. Since the language doesn't affect the bot's functionality, choose one according to your expertise and preferences. For beginners, we recommend Python as it is easier to learn, read, and maintain than JavaScript, simplifying the bot development process.

Where should I put C++ libraries? ›

Usually, there is '/lib' folder on Windows or '/usr/lib' folder on Linux that contains all the libraries. Once the library is installed, the compiler and the linker know the path of the library to use, and the library is ready for use.

Which Discord library is best? ›

js (21.4k GitHub stars)

js is a powerful JavaScript library. It allows moderators to interact with the Discord API easily. It is object-oriented. It contains predictable abstractions.

What is the largest coding Discord? ›

1. The Programmer's Hangout. A place where a fresher can get in touch with an industry veteran, the Programmer's Hangout Discord server is home to over 90k members.

Is Discord bot API free? ›

DiscordBot offers all of its endpoints for free, but the price for other APIs in the Discord collection can vary. While most of the big games represented on the list have free APIs, others are paid or freemium.

Do Discord bots use API? ›

The Discord REST API is used by bots to perform most actions, such as sending messages, kicking/banning users, and updating user permissions (broadly analogous to the events received from the WebSocket API).

What are Discord bots coded in? ›

If you've been searching around and looking at some other Discord bot creation guides, you've likely noticed that nearly all of them are written in… JavaScript. Using JavaScript (with node.

Can I have 2 bots on Discord? ›

Our bot hosting plan allows for you to run multiple Python or NodeJS based bots on one plan! You can extend this as much as you'd like, until the combination of bots being run exceeds the resources of your plan (such as RAM usage).

How to create a Discord bot without coding? ›

Part 2: How to Making a Discord Bot without Coding?
  1. 1 Create an App on Discord. ...
  2. 2 Turn this Discord application into a bot. ...
  3. 3 Generate a Bot Token. ...
  4. 4 Set Gateway Intents. ...
  5. 5 Link the Created Bot to your Bot Platform. ...
  6. 6 Invite your Discord Bot.
Nov 24, 2023

How to start your own bot? ›

To create your own chatbot:
  1. Identify your business goals and customer needs.
  2. Choose a chatbot builder that you can use on your desired channels.
  3. Design your bot conversation flow by using the right nodes.
  4. Test your chatbot and collect messages to get more insights.
  5. Use data and feedback from customers to train your bot.
Apr 12, 2024

What languages can you make a Discord bot in? ›

js as well as Discord.NET (Maybe the Java ones too). But in the end, you can also integrate other programming languages into your code. There's a lot of "good" libraries, node. js is one of the most commonly used but both the discord.go (golang) and discord.net (C#) libraries are very performant.

What language is best for Discord bot? ›

JavaScript: The Most Popular Way to Make a Discord Bot.

What coding language does bot designer for discord use? ›

Besides using the official BDFD script language (i.e BDScript) to develop Discord Bots. Bot Designer For Discord also supports developing bots using JavaScript.

How to make a Discord bot without Python? ›

Once your Discord application is created on Discord, you need to convert it into a bot. You can turn this application into a bot by clicking the 'Bot tab' in order to go to the bot page. Once in the Bot tab, you need to click on 'Add Bot'. As a result, your Discord bot will be created after action confirmation.

Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6590

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.