D++ (DPP)
C++ Discord API Bot Library
Building a discord bot in Linux using CLion

This tutorial teaches you how to create a working skeleton project you can build upon, using the JetBrains-IDE CLion.

Note
This tutorial will use Ubuntu! You might use other Distros if you prefer, but keep in mind the setup process might be different!

Make sure you have CLion installed and works fine (run a hello-world program). You can download CLion here.

Setup a project

Create a new project. Select C++17 as the Language standard, or C++20 if you want something more recent.

We'll use the following file structure as a skeleton project you can build upon:

- your_project/
    |-- libs/
    |-- src/
        |-- main.cpp
    |-- CMakeLists.txt

Create the directories in your project and move the by CLion generated hello-world main.cpp in the src/ directory.

In the libs/ directory, clone D++ with: git clone https://github.com/brainboxdotcc/DPP.git. You can also clone spdlog into it if you need a logger.

Your project directory should look like this:

Configure CMake file

Paste this CMake configuration in the CMakeLists.txt and adapt it according to your needs:

# minimum CMake version required
cmake_minimum_required(VERSION 3.15)
# Project name, version and description
project(discord-bot VERSION 1.0 DESCRIPTION "A discord bot")
# Add DPP as dependency
add_subdirectory(libs/DPP)
add_subdirectory(libs/spdlog) # if you need a logger. Don't forget to clone sources
# in the `libs/` directory
# Create an executable
add_executable(${PROJECT_NAME}
src/main.cpp
# your others files...
)
# Linking libraries
target_link_libraries(${PROJECT_NAME}
dpp
spdlog # Like before, if you need spdlog
)
# Specify includes
target_include_directories(${PROJECT_NAME} PRIVATE
libs/DPP/include
libs/spdlog/include # Like before, if you need spdlog
)
# Set C++ version
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 17 # or 20 if you want something more recent
CXX_STANDARD_REQUIRED ON
)

Then open the "File" menu and click on "Reload CMake Project" to reload the CMake configuration.

Add an example program

The next step is to write the bot. Copy and paste the following example program in the main.cpp and set your bot token (see Creating a Bot Token) and guild ID to the example program:

#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)
);
}
});
bot.start(false);
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:469
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:809
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
interaction command
command interaction
Definition: dispatcher.h:488
Session ready.
Definition: dispatcher.h:783
User has issued a slash command.
Definition: dispatcher.h:499

Hit the green "Run" button in the top-right to run the bot.

Congratulations, you've successfully set up a bot!

Troubleshooting

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