01_01
All checks were successful
Build / Build (push) Successful in 3m33s

This commit is contained in:
2026-05-31 23:22:58 -04:00
parent 77f8afb1f3
commit c2a9187071
7 changed files with 90 additions and 2 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
build/
.cache
bin
.env

View File

@@ -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")

6
cmake/dotenv.cmake Normal file
View File

@@ -0,0 +1,6 @@
CPMAddPackage (
NAME dotenv-cpp
GIT_REPOSITORY "https://github.com/laserpants/dotenv-cpp"
GIT_TAG master
OPTIONS "BUILD_DOCS OFF"
)

View File

@@ -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)

View File

@@ -1,10 +1,15 @@
#include <discordvoiceclient.h>
#include <dotenv/dotenv.h>
#include <dpp/dpp.h>
#include <utility.h>
#include <cstdlib>
#include <iostream>
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;
}

View File

@@ -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)

View File

@@ -0,0 +1,60 @@
#include <cluster.h>
#include <dispatcher.h>
#include <dotenv/dotenv.h>
#include <once.h>
#include <cstdlib>
#include <iostream>
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<struct register_bot_commands>()) {
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;
}