diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-21 13:20:09 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-21 13:20:09 +0500 |
| commit | 9f3b2ea3b4391f4cbbe28463b917936c639491ec (patch) | |
| tree | 4aa41354f4bc62f499b0bd941e1807071c241f8f | |
| parent | a761cdf71f0ca410efcf0dc77b788db2a3b7fb5c (diff) | |
feat: request
| -rw-r--r-- | src/commands/request.cpp | 62 | ||||
| -rw-r--r-- | src/commands/request.hpp | 43 |
2 files changed, 105 insertions, 0 deletions
diff --git a/src/commands/request.cpp b/src/commands/request.cpp new file mode 100644 index 0000000..72b04c7 --- /dev/null +++ b/src/commands/request.cpp @@ -0,0 +1,62 @@ +#include "request.hpp" + +#include <algorithm> +#include <optional> +#include <string> +#include <vector> + +#include "../constants.hpp" + +namespace bot { + namespace command { + bool Request::fill_request() { + 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 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 new file mode 100644 index 0000000..71f2f04 --- /dev/null +++ b/src/commands/request.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include <optional> +#include <string> + +#include "../irc/message.hpp" +#include "command.hpp" + +namespace bot { + namespace command { + class Request { + public: + Request(const command::CommandLoader &command_loader, + const irc::Message<irc::MessageType::Privmsg> &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<std::string> &get_subcommand_id() const { + return this->subcommand_id; + }; + const std::optional<std::string> &get_message() const { + return this->message; + }; + + const irc::Message<irc::MessageType::Privmsg> &get_irc_message() const { + return this->irc_message; + }; + + private: + std::string command_id; + std::optional<std::string> subcommand_id; + std::optional<std::string> message; + + const irc::Message<irc::MessageType::Privmsg> &irc_message; + const command::CommandLoader &command_loader; + }; + + } + +} |
