This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
build/
|
||||
.cache
|
||||
bin
|
||||
|
||||
.env
|
||||
|
||||
@@ -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
6
cmake/dotenv.cmake
Normal file
@@ -0,0 +1,6 @@
|
||||
CPMAddPackage (
|
||||
NAME dotenv-cpp
|
||||
GIT_REPOSITORY "https://github.com/laserpants/dotenv-cpp"
|
||||
GIT_TAG master
|
||||
OPTIONS "BUILD_DOCS OFF"
|
||||
)
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
14
src/01_basics/01_first_bot/CMakeLists.txt
Normal file
14
src/01_basics/01_first_bot/CMakeLists.txt
Normal 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)
|
||||
60
src/01_basics/01_first_bot/src/main.cpp
Normal file
60
src/01_basics/01_first_bot/src/main.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user