From 8c391504d160909753e6c6ee3186e0166b44c475 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 21 Apr 2024 17:20:38 +0500 Subject: feat: use Request instead of Message + util for creating requests --- src/commands/command.cpp | 20 +++++++------- src/commands/command.hpp | 8 +++--- src/commands/request.cpp | 62 ------------------------------------------- src/commands/request.hpp | 42 +++++------------------------ src/commands/request_util.cpp | 58 ++++++++++++++++++++++++++++++++++++++++ src/commands/request_util.hpp | 11 ++++++++ 6 files changed, 90 insertions(+), 111 deletions(-) delete mode 100644 src/commands/request.cpp create mode 100644 src/commands/request_util.cpp create mode 100644 src/commands/request_util.hpp (limited to 'src/commands') diff --git a/src/commands/command.cpp b/src/commands/command.cpp index 9d6ec6a..bf482f5 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -1,11 +1,12 @@ #include "command.hpp" -#include +#include #include #include #include "../bundle.hpp" #include "../modules/ping.hpp" +#include "request.hpp" namespace bot { namespace command { @@ -18,16 +19,17 @@ namespace bot { } std::optional, std::string>> - CommandLoader::run( - const InstanceBundle &bundle, - const irc::Message &msg) const { - for (const std::unique_ptr &command : this->commands) { - if (command->get_name() == msg.message) { - return command->run(bundle, msg); - } + CommandLoader::run(const InstanceBundle &bundle, + const Request &request) const { + auto command = std::find_if( + this->commands.begin(), this->commands.end(), + [&](const auto &x) { return x->get_name() == request.command_id; }); + + if (command == this->commands.end()) { + return std::nullopt; } - return std::nullopt; + return (*command)->run(bundle, request); } } } diff --git a/src/commands/command.hpp b/src/commands/command.hpp index 5f69788..498a1f2 100644 --- a/src/commands/command.hpp +++ b/src/commands/command.hpp @@ -7,7 +7,7 @@ #include #include "../bundle.hpp" -#include "../irc/message.hpp" +#include "request.hpp" namespace bot { namespace command { @@ -15,8 +15,7 @@ namespace bot { public: virtual std::string get_name() const = 0; virtual std::variant, std::string> run( - const InstanceBundle &bundle, - const irc::Message &msg) const = 0; + const InstanceBundle &bundle, const Request &request) const = 0; }; class CommandLoader { @@ -26,8 +25,7 @@ namespace bot { void add_command(std::unique_ptr cmd); std::optional, std::string>> run( - const InstanceBundle &bundle, - const irc::Message &msg) const; + const InstanceBundle &bundle, const Request &msg) const; const std::vector> &get_commands() const { return this->commands; diff --git a/src/commands/request.cpp b/src/commands/request.cpp deleted file mode 100644 index 72b04c7..0000000 --- a/src/commands/request.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "request.hpp" - -#include -#include -#include -#include - -#include "../constants.hpp" - -namespace bot { - namespace command { - bool Request::fill_request() { - std::vector parts = - utils::string::split_text(irc_message.message, ' '); - - std::string command_id = parts[0]; - - if (command_id.substr(0, DEFAULT_PREFIX.length()) != DEFAULT_PREFIX) { - return false; - } - - command_id = - command_id.substr(DEFAULT_PREFIX.length(), command_id.length()); - - bool found = std::any_of(this->command_loader.get_commands().begin(), - this->command_loader.get_commands().end(), - [&](const auto &command) { - return command->get_name() == command_id; - }); - - if (!found) { - return false; - } - - this->command_id = command_id; - - parts.erase(parts.begin()); - - if (parts.empty()) { - return true; - } - - std::string subcommand_id = parts[0]; - if (subcommand_id.empty()) { - this->subcommand_id = std::nullopt; - } else { - this->subcommand_id = subcommand_id; - } - parts.erase(parts.begin()); - - std::string message = utils::string::join_vector(parts, ' '); - - if (message.empty()) { - this->message = std::nullopt; - } else { - this->message = message; - } - - return true; - } - } -} diff --git a/src/commands/request.hpp b/src/commands/request.hpp index 71f2f04..9742cbc 100644 --- a/src/commands/request.hpp +++ b/src/commands/request.hpp @@ -4,40 +4,12 @@ #include #include "../irc/message.hpp" -#include "command.hpp" - -namespace bot { - namespace command { - class Request { - public: - Request(const command::CommandLoader &command_loader, - const irc::Message &irc_message) - : irc_message(irc_message), command_loader(command_loader){}; - ~Request() = default; - - bool fill_request(); - - const std::string &get_command_id() const { return this->command_id; }; - const std::optional &get_subcommand_id() const { - return this->subcommand_id; - }; - const std::optional &get_message() const { - return this->message; - }; - - const irc::Message &get_irc_message() const { - return this->irc_message; - }; - - private: - std::string command_id; - std::optional subcommand_id; - std::optional message; - - const irc::Message &irc_message; - const command::CommandLoader &command_loader; - }; - - } +namespace bot::command { + struct Request { + std::string command_id; + std::optional subcommand_id; + std::optional message; + const irc::Message &irc_message; + }; } diff --git a/src/commands/request_util.cpp b/src/commands/request_util.cpp new file mode 100644 index 0000000..af17623 --- /dev/null +++ b/src/commands/request_util.cpp @@ -0,0 +1,58 @@ +#include "request_util.hpp" + +#include + +#include "../constants.hpp" +#include "../irc/message.hpp" +#include "command.hpp" +#include "request.hpp" + +namespace bot::command { + std::optional generate_request( + const command::CommandLoader &command_loader, + const irc::Message &irc_message) { + std::vector parts = + utils::string::split_text(irc_message.message, ' '); + + std::string command_id = parts[0]; + + if (command_id.substr(0, DEFAULT_PREFIX.length()) != DEFAULT_PREFIX) { + return std::nullopt; + } + + command_id = + command_id.substr(DEFAULT_PREFIX.length(), command_id.length()); + + bool found = std::any_of( + command_loader.get_commands().begin(), + command_loader.get_commands().end(), + [&](const auto &command) { return command->get_name() == command_id; }); + + if (!found) { + return std::nullopt; + } + + parts.erase(parts.begin()); + + if (parts.empty()) { + Request req{command_id, std::nullopt, std::nullopt, irc_message}; + + return req; + } + + std::optional subcommand_id = parts[0]; + if (subcommand_id->empty()) { + subcommand_id = std::nullopt; + } + parts.erase(parts.begin()); + + std::optional message = utils::string::join_vector(parts, ' '); + + if (message->empty()) { + message = std::nullopt; + } + + Request req{command_id, subcommand_id, message, irc_message}; + return req; + } +} diff --git a/src/commands/request_util.hpp b/src/commands/request_util.hpp new file mode 100644 index 0000000..df61ba5 --- /dev/null +++ b/src/commands/request_util.hpp @@ -0,0 +1,11 @@ +#include + +#include "../irc/message.hpp" +#include "command.hpp" +#include "request.hpp" + +namespace bot::command { + std::optional generate_request( + const command::CommandLoader &command_loader, + const irc::Message &irc_message); +} -- cgit v1.2.3