diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-21 17:20:38 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-21 17:20:38 +0500 |
| commit | 8c391504d160909753e6c6ee3186e0166b44c475 (patch) | |
| tree | 43a95cf37ef9075d32a41ae8b7d924df68d845e9 /src/commands/request_util.cpp | |
| parent | 6572077c73406cf6c353395aad13d493929a1596 (diff) | |
feat: use Request instead of Message + util for creating requests
Diffstat (limited to 'src/commands/request_util.cpp')
| -rw-r--r-- | src/commands/request_util.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
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 <optional> + +#include "../constants.hpp" +#include "../irc/message.hpp" +#include "command.hpp" +#include "request.hpp" + +namespace bot::command { + std::optional<Request> generate_request( + const command::CommandLoader &command_loader, + const irc::Message<irc::MessageType::Privmsg> &irc_message) { + std::vector<std::string> 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<std::string> subcommand_id = parts[0]; + if (subcommand_id->empty()) { + subcommand_id = std::nullopt; + } + parts.erase(parts.begin()); + + std::optional<std::string> message = utils::string::join_vector(parts, ' '); + + if (message->empty()) { + message = std::nullopt; + } + + Request req{command_id, subcommand_id, message, irc_message}; + return req; + } +} |
