D++ (DPP)
C++ Discord API Bot Library
Loading...
Searching...
No Matches
Using D++ with C++ modules

Warning
D++ modules are currently experimental and are only supported by D++ on g++ 15, clang/LLVM 17, and MSVC 17.6 or above. Additionally, your program has to support C++20.

D++ is offered as a C++ module, which offers improved compile times over a traditional header.

In order to enable support, you must use C++20 (or later) with any module-supporting compiler. To activate the feature, pass the DPP_MODULES flag to CMake. Ensure that the generated build system supports modules (for CMake, this is usually Ninja; note CMake does not currently support modules with Makefile).

The module interface unit is attached to the D++ library target itself, and find_package exports that target under a namespace. This means that your bot must link against dpp::dpp, and not a bare dpp:

# Module support requires CMake 3.28 or above.
cmake_minimum_required(VERSION 3.28)
project(discord-bot)
find_package(dpp REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
# dpp::dpp carries the module interface unit along with it. A bare dpp does not!
target_link_libraries(${PROJECT_NAME} PRIVATE dpp::dpp)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
CXX_SCAN_FOR_MODULES ON
)
Warning
Writing a bare dpp instead of dpp::dpp will not give you an error from CMake. A name is only treated as a target if it contains ::, so a plain dpp is quietly handed to the linker as -ldpp instead. Your build will then fail later on with an error saying that the module dpp cannot be found, because your compiler was never told where the module interface unit lives.

The other properties in the example above are just as important as the link line:

  • CXX_SCAN_FOR_MODULES tells CMake to scan your sources for import statements. Without it, CMake never notices that your bot depends on the D++ module at all.
  • We set CXX_EXTENSIONS OFF, as compilers will refuse to load a module with extensions enabled on another module without extensions enabled.

Once this is done, simply import dpp; and we're good to go!

#include <string>
import dpp;
int main() {
dpp::cluster bot("YOUR_BOT_TOKEN_HERE");
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> void {
if (event.command.get_command_name() == "ping") {
event.reply("Pong!");
}
});
bot.on_ready([&bot](const dpp::ready_t& event) -> void {
bot.global_command_create(
dpp::slashcommand("ping", "Ping pong!", bot.me.id)
);
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
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:1498
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
interaction command
command interaction
Definition dispatcher.h:789
Session ready.
Definition dispatcher.h:1072
User has issued a slash command.
Definition dispatcher.h:806
D++ 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