diff options
author | Cara Salter <cara@devcara.com> | 2023-03-30 15:09:52 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2023-03-30 15:09:52 -0400 |
commit | 8233fc4bcffc82907e82b31072cb8129aa200ae5 (patch) | |
tree | bb9551e287fb632cbb3dfb070ffa228351ed4129 /modules | |
parent | 527b7ab2df126bb2f480999049ed05b057a6ef83 (diff) | |
download | 142bot-8233fc4bcffc82907e82b31072cb8129aa200ae5.tar.gz 142bot-8233fc4bcffc82907e82b31072cb8129aa200ae5.zip |
reactions module
Diffstat (limited to 'modules')
-rw-r--r-- | modules/mmanager/mmanager.cpp | 107 | ||||
-rw-r--r-- | modules/reactions/reactions.cpp | 43 |
2 files changed, 148 insertions, 2 deletions
diff --git a/modules/mmanager/mmanager.cpp b/modules/mmanager/mmanager.cpp index fda8b3c..36b6aae 100644 --- a/modules/mmanager/mmanager.cpp +++ b/modules/mmanager/mmanager.cpp @@ -19,14 +19,17 @@ #include <stdlib.h> #include <string> #include <142bot/modules.hpp> +#include <142bot/util.hpp> +#include <pcre.h> #include <dpp/dpp.h> +#include <fmt/format.h> class MManagerModule : public Module { double microseconds_ping; public: MManagerModule(Bot* creator, ModuleLoader* ml) : Module(creator, ml) { ml->attach({ I_OnMessage, I_OnReady }, this); - creator->core->log(dpp::ll_info, "ModuleManager online!"); + creator->core->log(dpp::ll_info, "ModuleManager online!"); } virtual std::string version() { @@ -43,7 +46,107 @@ public: } virtual bool OnMessage(const dpp::message_create_t &message, const std::string& clean_message, bool mentioned, const std::vector<std::string> &stringmentions) { - bot->core->log(dpp::ll_info, "Got message!"); + std::vector<std::string> param; + const char* pcre_error; + int pcre_error_ofs; + auto comp = pcre_compile(std::string("^sudo(\\s+(.+?))$").c_str(), PCRE_CASELESS | PCRE_MULTILINE, &pcre_error, &pcre_error_ofs, NULL); + if (!comp) { + bot->core->log(dpp::ll_error, pcre_error); + } + + int matcharr[90]; + int matchcount = pcre_exec(comp, NULL, clean_message.c_str(), clean_message.length(), 0, 0, matcharr, 90); + for (int i = 0; i < matchcount; ++i) { + param.push_back(std::string(clean_message.c_str() + matcharr[2 * i], (size_t)(matcharr[2 * i + 1] - matcharr[2 * i]))); + } + if (mentioned && matchcount > 0) { + if (message.msg.author.id == bot->get_owner_id()) { + // Tokenize + for (int i = 0; i < param.size(); i++) { + bot->core->log(dpp::ll_debug, fmt::format("{}", param[i])); + } + std::stringstream tokens(trim(param[2])); + std::string subcommand; + tokens >> subcommand; + + bot->core->log(dpp::ll_warning, fmt::format("SUDO: <{}> {}",message.msg.author.username, clean_message)); + + if (lowercase(subcommand) == "modules") { + std::stringstream s; + + // NOTE: GetModuleList's reference is safe from within a module event + const ModuleMap& modlist = bot->loader->get_loaded_modules(); + + s << "```diff" << std::endl; + s << fmt::format("╭─────────────────────────┬───────────┬────────────────────────────────────────────────╮") << std::endl; + s << fmt::format("│ Filename | Version | Description |") << std::endl; + s << fmt::format("├─────────────────────────┼───────────┼────────────────────────────────────────────────┤") << std::endl; + + for (auto mod = modlist.begin(); mod != modlist.end(); ++mod) { + s << fmt::format("│ {:23} | {:9} | {:46} |", mod->first, mod->second->version(), mod->second->description()) << std::endl; + } + s << fmt::format("╰─────────────────────────┴───────────┴────────────────────────────────────────────────╯") << std::endl; + s << "```"; + + dpp::channel* c = dpp::find_channel(message.msg.channel_id); + if (c) { + bot->core->message_create(dpp::message(c->id, s.str())); + + } + + } else if (lowercase(subcommand) == "load") { + std::string modfile; + tokens >> modfile; + if (bot->loader->load(modfile)) { + EmbedSimple("Loaded module: " + modfile, message.msg.channel_id); + } else { + EmbedSimple(std::string("Can't do that, check server logs"), message.msg.channel_id); + } + } else if (lowercase(subcommand) == "unload") { + std::string modfile; + tokens >> modfile; + if (modfile == "module_mmanager.so") { + EmbedSimple("That's the module manager, are you sure about that chief?", message.msg.channel_id); + } + + if (bot->loader->unload(modfile)) { + EmbedSimple("Unloaded module: " + modfile, message.msg.channel_id); + } else { + EmbedSimple("Can't do that, check server logs.", message.msg.channel_id); + } + } else if(lowercase(subcommand) == "reload") { + std::string modfile; + tokens >> modfile; + if (modfile == "module_mmanager.so") { + EmbedSimple("That's the module manager, are you sure about that chief?", message.msg.channel_id); + ::sleep(500); + } + + if (bot->loader->reload(modfile)) { + EmbedSimple("Reloaded module: " + modfile, message.msg.channel_id); + } else { + EmbedSimple("Can't do that, check server logs", message.msg.channel_id); + } + }else if (lowercase(subcommand) == "ping") { + dpp::channel* c = dpp::find_channel(message.msg.channel_id); + if (c) { + std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now(); + dpp::snowflake cid = message.msg.channel_id; + bot->core->message_create(dpp::message(message.msg.channel_id, "Pinging..."), [cid, this, start_time](const dpp::confirmation_callback_t & state) { + double microseconds_ping = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start_time).count(); + dpp::snowflake mid = (std::get<dpp::message>(state.value)).id; + this->bot->core->message_delete(mid, cid); + this->EmbedSimple(fmt::format("**Pong!** REST Response time: {:.3f} ms", microseconds_ping / 1000, 4), cid); + }); + } + } else { + EmbedSimple("Command not found.", message.msg.channel_id); + } + } else { + bot->core->log(dpp::ll_error, fmt::format("Called ModuleManager as a mortal ({})", bot->get_owner_id())); + message.reply(dpp::message("nope")); + } + } return true; } diff --git a/modules/reactions/reactions.cpp b/modules/reactions/reactions.cpp new file mode 100644 index 0000000..3818d0d --- /dev/null +++ b/modules/reactions/reactions.cpp @@ -0,0 +1,43 @@ +#include <dpp/message.h> +#include <dpp/json.h> +#include <stdlib.h> +#include <142bot/modules.hpp> +#include <fmt/format.h> + +using std::to_string; +class ReactionsModule : public Module { + std::map<std::string, std::string> reactionMap; +public: + ReactionsModule(Bot* creator, ModuleLoader* ml): Module(creator, ml) { + ml->attach({I_OnMessage}, this); + + std::ifstream f("resources/reactions.json"); + json reactions = json::parse(f); + + for(auto it = reactions.begin(); it != reactions.end(); it++) { + reactionMap.insert_or_assign(it.key(), reactions.value(it.key(), "")); + } + } + + virtual std::string version() { + return "0.1.0"; + } + + virtual std::string description() { + return "Auto-reactions based on keyword"; + } + virtual bool OnMessage(const dpp::message_create_t &message, const std::string& clean_message, bool mentioned, const std::vector<std::string> &stringmentions) { + for (auto i = reactionMap.begin(); i != reactionMap.end(); i++) { + if (clean_message.find(i->first) != std::string::npos) { + bot->core->message_add_reaction(message.msg, i->second); + bot->core->log(dpp::ll_debug, "Adding reaction based on keyword"); + } + } + + return true; + } + +}; + + +ENTRYPOINT(ReactionsModule)
\ No newline at end of file |