From c2a91870710d4bef3c3acb8bc983ed8f903efbed Mon Sep 17 00:00:00 2001 From: Lyra Date: Sun, 31 May 2026 23:22:58 -0400 Subject: [PATCH] 01_01 --- .gitignore | 2 +- CMakeLists.txt | 1 + cmake/dotenv.cmake | 6 +++ src/00_project/CMakeLists.txt | 2 + src/00_project/src/main.cpp | 7 ++- src/01_basics/01_first_bot/CMakeLists.txt | 14 ++++++ src/01_basics/01_first_bot/src/main.cpp | 60 +++++++++++++++++++++++ 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 cmake/dotenv.cmake create mode 100644 src/01_basics/01_first_bot/CMakeLists.txt create mode 100644 src/01_basics/01_first_bot/src/main.cpp diff --git a/.gitignore b/.gitignore index 9456119..b231abf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ build/ .cache bin - +.env diff --git a/CMakeLists.txt b/CMakeLists.txt index 770b258..b25e5c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,4 @@ foreach(cmake_file ${CMAKE_FILES}) endforeach() add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/00_project") +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/01_first_bot") diff --git a/cmake/dotenv.cmake b/cmake/dotenv.cmake new file mode 100644 index 0000000..9feb3e7 --- /dev/null +++ b/cmake/dotenv.cmake @@ -0,0 +1,6 @@ +CPMAddPackage ( + NAME dotenv-cpp + GIT_REPOSITORY "https://github.com/laserpants/dotenv-cpp" + GIT_TAG master + OPTIONS "BUILD_DOCS OFF" +) diff --git a/src/00_project/CMakeLists.txt b/src/00_project/CMakeLists.txt index dae7a67..d770b9e 100644 --- a/src/00_project/CMakeLists.txt +++ b/src/00_project/CMakeLists.txt @@ -4,8 +4,10 @@ message("Setting up 00_project") add_executable(00_project "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp") target_include_directories(00_project PUBLIC "${dpp_SOURCE_DIR}/include" + "${dotenv-cpp_SOURCE_DIR}/include/laserpants" ) target_link_libraries(00_project dpp + dotenv ) SET_OUTPUT_NAMES(00_project) diff --git a/src/00_project/src/main.cpp b/src/00_project/src/main.cpp index db0690c..6192e70 100644 --- a/src/00_project/src/main.cpp +++ b/src/00_project/src/main.cpp @@ -1,10 +1,15 @@ #include +#include #include #include +#include #include - int main() { + dotenv::init(); + std::cout << "Using dpp version: " << dpp::utility::version() << std::endl; + std::cout << "Loaded DISCORD_TOKEN: " << std::getenv("DISCORD_TOKEN") + << std::endl; return 0; } diff --git a/src/01_basics/01_first_bot/CMakeLists.txt b/src/01_basics/01_first_bot/CMakeLists.txt new file mode 100644 index 0000000..72023bd --- /dev/null +++ b/src/01_basics/01_first_bot/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.28) +project(dppbots) +message("Setting up 01_01_first_bot") +add_executable(01_01_first_bot "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp") +target_include_directories(01_01_first_bot PUBLIC + "${dpp_SOURCE_DIR}/include" + "${dotenv-cpp_SOURCE_DIR}/include/laserpants" + +) +target_link_libraries(01_01_first_bot + dpp + dotenv +) +SET_OUTPUT_NAMES(01_01_first_bot) diff --git a/src/01_basics/01_first_bot/src/main.cpp b/src/01_basics/01_first_bot/src/main.cpp new file mode 100644 index 0000000..cdd17f6 --- /dev/null +++ b/src/01_basics/01_first_bot/src/main.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include + +#include +#include + +int main() { + // Load in the .env file into the environment + dotenv::init(); + std::string botToken = std::getenv("DISCORD_TOKEN"); + std::cout << "Using discord bot token: " << botToken << std::endl; + + // Create a bot cluster, passing in the bot token + dpp::cluster bot(botToken); + + /// DPP mostly works on callback functions + /// You can create a normal function and just + /// pass it in as long as it supports the + /// function structure, or just use a lambda, + /// as youre probably just calling the function + /// once... + + // Pass a default cout logger as the logging + // function + bot.on_log(dpp::utility::cout_logger()); + + // When a slashcommand is received + bot.on_slashcommand([](const dpp::slashcommand_t& event) { + // If the slashcommands name is ping + if (event.command.get_command_name() == "ping") { + // reply back with "Pong!" + event.reply("Pong!"); + } + }); + + // When the bot is ready, meaning all gateway + // connections are made, all shards and threading + // are up, etc + bot.on_ready([&bot](const dpp::ready_t& event) { + // Register global slash commands + if (dpp::run_once()) { + std::cout << "Registering bot commands" << std::endl; + // Create the "/ping" command + bot.global_command_create( + dpp::slashcommand("ping", "Ping pong", bot.me.id)); + } + }); + + // Start the bot + /// There are two options for the arg here, + /// dpp::st_wait and dpp::st_return + /// st_wait: Waits forever, blocking execution + /// st_return: Returns immediately, you have to manage lifetime + /// Taking out of scope or deleting its pointer, kills bot. + bot.start(dpp::st_wait); + + return 0; +}