From 8edf3bf0ca81b942a165cde3bdea11ca755da955 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Mon, 10 Apr 2023 11:15:35 -0400 Subject: modldr: Support modules in a different path from exe Change-Id: If56998535b8067b29ea34c760ac2da4339174983 --- src/bot.cpp | 40 +++++++++++++++++++++++++++++++++------- src/main.cpp | 2 +- src/modules.cpp | 10 +++++++++- 3 files changed, 43 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/bot.cpp b/src/bot.cpp index ed1531d..d5f21de 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -12,15 +12,16 @@ #include #include <142bot/db.hpp> #include +#include namespace fs = std::filesystem; -Bot::Bot(bool devel, dpp::cluster* cluster, char prefix) { +Bot::Bot(bool devel, dpp::cluster* cluster, char prefix, json &cfg) { dev = devel; this->core = cluster; - - std::ifstream f("config.json"); - json cfg = json::parse(f); + this->prefix = prefix; + this->cfg = cfg; + std::string token = cfg.value("token", "bad-token"); this->conn = db::connect(cfg["postgres"]["host"], cfg["postgres"]["user"], cfg["postgres"]["pass"], cfg["postgres"]["database"], cfg["postgres"]["port"]); @@ -107,6 +108,7 @@ void Bot::onMessage(const dpp::message_create_t &message) { } /* Ignore self, and bots */ if (message.msg.author.id != user.id && message.msg.author.is_bot() == false) { + core->log(dpp::ll_debug, "Got message event"); /* Replace all mentions with raw nicknames */ bool mentioned = false; @@ -129,9 +131,33 @@ void Bot::onMessage(const dpp::message_create_t &message) { } /* Remove linefeeds, they mess with botnix */ mentions_removed = trim(mentions_removed); - - /* Call modules */ - FOREACH_MOD(I_OnMessage,OnMessage(message, mentions_removed, mentioned, stringmentions)); + if (message.msg.content.starts_with(this->prefix)) { + // Command + core->log(dpp::ll_debug, fmt::format("Got command: {}", mentions_removed)); + + // Tokenize + std::vector result; + + const char* str = message.msg.content.c_str(); + + do + { + const char *begin = str; + while(*str != ' ' && *str) + str++; + core->log(dpp::ll_debug, std::string(begin, str)); + + result.push_back(std::string(begin, str)); + } while (0 != *str++); + core->log(dpp::ll_debug, "Attempting to call FOREACH_MOD"); + FOREACH_MOD(I_OnCommand,OnCommand(message, result[0], result)); + + } else { + /* Call modules */ + core->log(dpp::ll_debug, fmt::format("Got regular message: {}", mentions_removed)); + FOREACH_MOD(I_OnMessage,OnMessage(message, mentions_removed, + mentioned, stringmentions)); + } } } diff --git a/src/main.cpp b/src/main.cpp index 08574d1..e6d7c13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,7 +74,7 @@ int main(int argc, char const *argv[]) { }); /* code */ - Bot client(0, &bot, cfg.value("prefix", ".").at(0)); + Bot client(0, &bot, cfg.value("prefix", ".").at(0), cfg); client.set_owner_id(dpp::snowflake(cfg.value("owner", "00000000000"))); diff --git a/src/modules.cpp b/src/modules.cpp index 0c9fb18..4b636f9 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -7,6 +7,8 @@ #include #include +#include + const char* StringNames[I_END + 1] = { "I_BEGIN", "I_OnMessage", @@ -45,6 +47,7 @@ const char* StringNames[I_END + 1] = { "I_OnVoiceStateUpdate", "I_OnVoiceServerUpdate", "I_OnWebhooksUpdate", + "I_OnCommand", "I_END" }; @@ -95,7 +98,7 @@ bool ModuleLoader::load(const std::string &fname) { if (Modules.find(fname) == Modules.end()) { char buffer[PATH_MAX + 1]; - getcwd(buffer, PATH_MAX); + realpath(bot->cfg.value("module_path", ".").c_str(), buffer); std::string full_path = std::string(buffer) + "/" + fname; m.dlopen_handle = dlopen(full_path.c_str(), RTLD_NOW | RTLD_LOCAL); @@ -429,6 +432,11 @@ bool Module::OnWebhooksUpdate(const dpp::webhooks_update_t &obj) return true; } +bool Module::OnCommand(const dpp::message_create_t &message, const std::string &command, const std::vector& params) { + bot->core->log(dpp::ll_debug, "Called default OnCommand..."); + return true; +} + bool Module::OnAllShardsReady() { return true; -- cgit v1.2.3