Skip to content

Commit

Permalink
feat: made commands only register if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Oct 27, 2023
1 parent 08cc22c commit a8ad847
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
28 changes: 7 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand Down
27 changes: 16 additions & 11 deletions src/BUDe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ using json = nlohmann::json;
int main(int argc, char *argv[])
{
// "./BUDe" counts as an argument so 2 here means "./BUDe <token>"
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];
Expand All @@ -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<struct register_bot_commands>()) {
if (BUDe::register_commands && dpp::run_once<struct register_bot_commands>()) {

BUDe::botRef->log(dpp::ll_info, "Bot is registering commands.");

std::vector<dpp::slashcommand> tempCommands;
std::vector<dpp::slashcommand> 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())
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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. "))
Expand Down
27 changes: 16 additions & 11 deletions src/BUDe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::unique_ptr<Command>> commands;
// All of BUD-e's statuses!
inline const std::vector<std::string> 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<std::unique_ptr<Command>> commands;

#pragma endregion
};

0 comments on commit a8ad847

Please sign in to comment.