From a8ad847f2faf9f9815bf147549d92716cf610faa Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Fri, 27 Oct 2023 21:50:42 +0100 Subject: [PATCH] feat: made commands only register if specified --- CMakeLists.txt | 28 +++++++--------------------- README.md | 5 +++-- src/BUDe.cpp | 27 ++++++++++++++++----------- src/BUDe.h | 27 ++++++++++++++++----------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fcca71..2f12b85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,30 +6,18 @@ file(GLOB bude_src CONFIGURE_DEPENDS "src/*.h" "src/*.cpp" "src/*/*.h" "src/*/*. # Add source to this project's executable. add_executable(${PROJECT_NAME} - ${bude_src} # Much love to GLOB <3 + ${bude_src} # Much love to GLOB <3 ) -# =============================================== -# Stuff for registering commands via Jenkins - -set(REGISTER_COMMANDS, on) - -if (REGISTER_COMMANDS) - target_compile_definitions(${PROJECT_NAME} REGISTER_COMMANDS) -endif () - -# Registering commands stuff over. -# =============================================== - # =============================================== # Required stuff for coro: target_compile_definitions(${PROJECT_NAME} PUBLIC - DPP_CORO + DPP_CORO ) target_compile_features(${PROJECT_NAME} PUBLIC - cxx_std_20 + cxx_std_20 ) set(DPP_CORO on) @@ -40,16 +28,14 @@ set(DPP_CORO on) add_subdirectory(libs/DPP) target_link_libraries(${PROJECT_NAME} - dpp + dpp ) target_include_directories(${PROJECT_NAME} PRIVATE - libs/DPP/include + libs/DPP/include ) set_target_properties(${PROJECT_NAME} PROPERTIES - CMAKE_STANDARD 20 - CMAKE_STANDARD_REQUIRED ON + CMAKE_STANDARD 20 + CMAKE_STANDARD_REQUIRED ON ) - -# TODO: Add tests and install targets if needed. diff --git a/README.md b/README.md index e71b8e8..d696a05 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # BUD-e -The BUD-e Discord Bot, rewrote in C++! BUD-e also serves as a testing ground for the D++ docs. +The BUD-e Discord Bot, rewrote in C++! Updates will be made frequently. BUD-e' is automated via Jenkins and will update locally every git push, however, this does not update the bot whilst running. @@ -8,7 +8,8 @@ BUD-e will update on discord when he's flagged to do so. BUD-e has two versions: - Experimental - Stable -These are different bots. Experimental is a bot that is ran locally. Stable is the official "BUD-e" bot. +These are different bots. The Experimental version is a bot that is only ever online when running locally. Stable is the official "BUD-e" bot. +Experimental also serves as a testing ground for D++ contributions. ## Libraries diff --git a/src/BUDe.cpp b/src/BUDe.cpp index cd677fa..97101d9 100644 --- a/src/BUDe.cpp +++ b/src/BUDe.cpp @@ -19,9 +19,14 @@ using json = nlohmann::json; int main(int argc, char *argv[]) { // "./BUDe" counts as an argument so 2 here means "./BUDe " - if(argc != 2) { - std::cout << "No bot token specified. Can't launch bot Aborting..."; + if(argc < 2) { + std::cout << "No bot token specified, Aborting..."; return 0; + } else if(argc > 3) { + std::cout << "Too many arguments specified, Aborting..."; + return 0; + } else if(argc == 3 && argv[2] == "REGISTER") { + BUDe::register_commands = true; } BUDe::token = argv[1]; @@ -46,18 +51,17 @@ int main(int argc, char *argv[]) bot.on_button_click(rps_listener::on_button_click); /* Register slash command here in on_ready */ - BUDe::botRef->on_ready([&](const dpp::ready_t& event) { + BUDe::botRef->on_ready([&bot](const dpp::ready_t& event) { /* Wrap command registration in run_once to make sure it doesn't run on every full reconnection */ - if (dpp::run_once()) { + if (BUDe::register_commands && dpp::run_once()) { BUDe::botRef->log(dpp::ll_info, "Bot is registering commands."); std::vector tempCommands; std::vector tempCommandsPrivate; - for(auto& cmd : BUDe::commands) - { + for(auto& cmd : BUDe::commands) { dpp::slashcommand tempCommand{cmd->commandName, cmd->commandDescription, BUDe::botRef->me.id}; for(dpp::command_option& option : cmd->CommandOptions()) @@ -77,10 +81,6 @@ int main(int argc, char *argv[]) BUDe::botRef->guild_bulk_command_create(tempCommandsPrivate, 667401873233543173); BUDe::botRef->log(dpp::ll_info, "Bot has completed registering commands."); - - bot.start_timer([](const dpp::timer& timer) { - BUDe::DoStatusChange(); - }, 120); } BUDe::botRef->message_create(dpp::message(667405048267014164, @@ -92,6 +92,11 @@ int main(int argc, char *argv[]) // Call status change now. BUDe::DoStatusChange(); + + /* Now, start a timer to do a status change every x seconds */ + bot.start_timer([](const dpp::timer& timer) { + BUDe::DoStatusChange(); + }, 120); }); signal(SIGINT, BUDe::callback_handler); @@ -105,7 +110,7 @@ int main(int argc, char *argv[]) void BUDe::callback_handler(int signum) { - BUDe::botRef->message_create(dpp::message(667405048267014164,EmbedBuilder::BasicEmbedWithTimestamp( + BUDe::botRef->message_create(dpp::message(667405048267014164, EmbedBuilder::BasicEmbedWithTimestamp( dpp::colours::red, "Shutting systems off, Captain!", "I have received a signal to shut down. ")) diff --git a/src/BUDe.h b/src/BUDe.h index 3fad351..fd21f6b 100644 --- a/src/BUDe.h +++ b/src/BUDe.h @@ -13,27 +13,32 @@ namespace BUDe #pragma region Functions - void callback_handler(int signum); +void callback_handler(int signum); - void ChangeStatusThread(); +void ChangeStatusThread(); - void DoStatusChange(); +void DoStatusChange(); #pragma endregion #pragma region Variables - inline std::string token; +inline std::string token; - // The bot reference. - inline dpp::cluster* botRef; +// The bot reference. +inline dpp::cluster* botRef; - // All of BUD-e's statuses! - inline const std::vector statuses = { "$playing with wires.", "$playing Golf on the moon!", - "$listening electro-beeps!", "$listening circuit symphonies!", - "$watching binary sunsets!", "$watching algorithms in action!"}; +inline bool register_commands; - inline std::vector> commands; +// All of BUD-e's statuses! +inline const std::vector statuses = { "$playing with wires.", + "$playing Golf on the moon!", + "$listening electro-beeps!", + "$listening circuit symphonies!", + "$watching binary sunsets!", + "$watching algorithms in action!" }; + +inline std::vector> commands; #pragma endregion };