Rework env loading + comments
This commit is contained in:
@@ -21,3 +21,4 @@ add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/00_project")
|
|||||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/01_first_bot")
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/01_first_bot")
|
||||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/02_embeds")
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/02_embeds")
|
||||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/03_private_messages")
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/03_private_messages")
|
||||||
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/04_adding_files")
|
||||||
|
|||||||
@@ -7,8 +7,16 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Load in the .env file into the environment
|
// Load the .env file into the environment
|
||||||
dotenv::init();
|
dotenv::init();
|
||||||
|
// Make sure the DISCORD_TOKEN was loaded
|
||||||
|
if (!std::getenv("DISCORD_TOKEN")) {
|
||||||
|
std::cout
|
||||||
|
<< "Failed to load the discord token from .env or the environment"
|
||||||
|
<< std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Get the bot token
|
||||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
std::cout << "Using discord bot token: " << botToken << std::endl;
|
std::cout << "Using discord bot token: " << botToken << std::endl;
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,16 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Load in the .env file
|
// Load the .env file into the environment
|
||||||
dotenv::init();
|
dotenv::init();
|
||||||
// Get the discord token from the environment
|
// Check to make sure the token was loaded
|
||||||
|
if (!std::getenv("DISCORD_TOKEN")) {
|
||||||
|
std::cout
|
||||||
|
<< "Failed to load the discord token from .env or the environment"
|
||||||
|
<< std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Load in the token
|
||||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
// Create a bot cluster
|
// Create a bot cluster
|
||||||
|
|||||||
@@ -11,33 +11,55 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
// Load in the .env file
|
||||||
dotenv::init();
|
dotenv::init();
|
||||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
// Check to make sure the token was loaded
|
||||||
if (botToken.empty()) {
|
if (!std::getenv("DISCORD_TOKEN")) {
|
||||||
std::cout
|
std::cout
|
||||||
<< "Failed to load the discord token from .env or the environment"
|
<< "Failed to load the discord token from .env or the environment"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
// Load in the token into a string
|
||||||
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
|
// Create the bot
|
||||||
dpp::cluster bot(botToken);
|
dpp::cluster bot(botToken);
|
||||||
|
|
||||||
|
// Setup a default cout_logger
|
||||||
bot.on_log(dpp::utility::cout_logger());
|
bot.on_log(dpp::utility::cout_logger());
|
||||||
|
|
||||||
|
// Setup a lamda for the on_slashcommand
|
||||||
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
|
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
|
||||||
|
// Filter for the /pm slashcommand
|
||||||
if (event.command.get_command_name() == "pm") {
|
if (event.command.get_command_name() == "pm") {
|
||||||
|
// Discord stores all objects through a reference called a
|
||||||
|
// "snowflake"
|
||||||
dpp::snowflake user;
|
dpp::snowflake user;
|
||||||
|
|
||||||
if (event.get_parameter("user").index() == 0) {
|
if (event.get_parameter("user").index() == 0) {
|
||||||
|
// If the user parameter wasn't passed, just get the user that
|
||||||
|
// issued the command
|
||||||
user = event.command.get_issuing_user().id;
|
user = event.command.get_issuing_user().id;
|
||||||
} else {
|
} else {
|
||||||
|
// Get the value of the user parameter
|
||||||
user = std::get<dpp::snowflake>(event.get_parameter("user"));
|
user = std::get<dpp::snowflake>(event.get_parameter("user"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a direct message
|
||||||
bot.direct_message_create(
|
bot.direct_message_create(
|
||||||
|
// Define the user you're sending the message to, aswell as the
|
||||||
|
// message
|
||||||
user, dpp::message("Here's a private message!"),
|
user, dpp::message("Here's a private message!"),
|
||||||
|
// Once the request has been sent to discord, a callback can be
|
||||||
|
// specified to handle any errors or anything related to the
|
||||||
|
// message
|
||||||
[event, user](const dpp::confirmation_callback_t& callback) {
|
[event, user](const dpp::confirmation_callback_t& callback) {
|
||||||
|
// Check to make sure there wasnt an error sending the
|
||||||
|
// message
|
||||||
if (callback.is_error()) {
|
if (callback.is_error()) {
|
||||||
|
// Reply back with a message saying the direct message
|
||||||
|
// failed to send
|
||||||
if (user == event.command.get_issuing_user().id) {
|
if (user == event.command.get_issuing_user().id) {
|
||||||
event.reply(
|
event.reply(
|
||||||
dpp::message("I couldnt send you a message.")
|
dpp::message("I couldnt send you a message.")
|
||||||
@@ -52,6 +74,8 @@ int main() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In the case the direct message was able to be sent, send
|
||||||
|
// a confirmation message
|
||||||
if (user == event.command.get_issuing_user().id) {
|
if (user == event.command.get_issuing_user().id) {
|
||||||
event.reply(
|
event.reply(
|
||||||
dpp::message("I've sent you a private message")
|
dpp::message("I've sent you a private message")
|
||||||
@@ -66,16 +90,21 @@ int main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Define a callback for the on_ready
|
||||||
bot.on_ready([&bot](const dpp::ready_t& event) {
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
||||||
if (dpp::run_once<struct register_bot_commands>()) {
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
|
// Create the /pm slashcommand
|
||||||
dpp::slashcommand command("pm", "Send a private message.",
|
dpp::slashcommand command("pm", "Send a private message.",
|
||||||
bot.me.id);
|
bot.me.id);
|
||||||
|
// Add the "user" option to the command
|
||||||
command.add_option(dpp::command_option(
|
command.add_option(dpp::command_option(
|
||||||
dpp::co_mentionable, "user", "The user to message", false));
|
dpp::co_mentionable, "user", "The user to message", false));
|
||||||
|
// Register the command with discord
|
||||||
bot.global_command_create(command);
|
bot.global_command_create(command);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Start the bot
|
||||||
bot.start(dpp::st_wait);
|
bot.start(dpp::st_wait);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
15
src/01_basics/04_adding_files/CMakeLists.txt
Normal file
15
src/01_basics/04_adding_files/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(dppbots)
|
||||||
|
set(example_name "01_04_adding_files")
|
||||||
|
message("Setting up ${example_name}")
|
||||||
|
add_executable(${example_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
|
||||||
|
target_include_directories(${example_name} PUBLIC
|
||||||
|
"${dpp_SOURCE_DIR}/include"
|
||||||
|
"${dotenv-cpp_SOURCE_DIR}/include/laserpants"
|
||||||
|
|
||||||
|
)
|
||||||
|
target_link_libraries(${example_name}
|
||||||
|
dpp
|
||||||
|
dotenv
|
||||||
|
)
|
||||||
|
SET_OUTPUT_NAMES(${example_name})
|
||||||
17
src/01_basics/04_adding_files/src/main.cpp
Normal file
17
src/01_basics/04_adding_files/src/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <dotenv/dotenv.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
dotenv::init();
|
||||||
|
if (!std::getenv("DISCORD_TOKEN")) {
|
||||||
|
std::cout
|
||||||
|
<< "Failed to load the discord token from .env or the environment"
|
||||||
|
<< std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user