diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-21 18:00:16 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-21 18:00:16 +0500 |
| commit | 20911c0cfbe608cc6188cfa27e4f953632a3ef29 (patch) | |
| tree | 29410091d1c03e1977220fd43d9b7757df73f2c0 /src | |
| parent | e72ff42203f59b0dc39b9f65f79716a4f61bacac (diff) | |
feat: handler for chat messages
Diffstat (limited to 'src')
| -rw-r--r-- | src/handlers.cpp | 43 | ||||
| -rw-r--r-- | src/handlers.hpp | 12 | ||||
| -rw-r--r-- | src/main.cpp | 11 |
3 files changed, 66 insertions, 0 deletions
diff --git a/src/handlers.cpp b/src/handlers.cpp new file mode 100644 index 0000000..b483abd --- /dev/null +++ b/src/handlers.cpp @@ -0,0 +1,43 @@ +#include "handlers.hpp" + +#include <optional> +#include <variant> +#include <vector> + +#include "bundle.hpp" +#include "commands/command.hpp" +#include "commands/request.hpp" +#include "commands/request_util.hpp" +#include "irc/message.hpp" + +namespace bot::handlers { + void handle_private_message( + const InstanceBundle &bundle, + const command::CommandLoader &command_loader, + const irc::Message<irc::MessageType::Privmsg> &message) { + std::optional<command::Request> request = + command::generate_request(command_loader, message); + + if (request.has_value()) { + auto o_response = command_loader.run(bundle, request.value()); + + if (o_response.has_value()) { + auto response = o_response.value(); + + try { + auto str = std::get<std::string>(response); + bundle.irc_client.say(message.source.login, str); + } catch (const std::exception &e) { + } + + try { + auto strs = std::get<std::vector<std::string>>(response); + for (const std::string &str : strs) { + bundle.irc_client.say(message.source.login, str); + } + } catch (const std::exception &e) { + } + } + } + } +} diff --git a/src/handlers.hpp b/src/handlers.hpp new file mode 100644 index 0000000..ed74551 --- /dev/null +++ b/src/handlers.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "bundle.hpp" +#include "commands/command.hpp" +#include "irc/message.hpp" + +namespace bot::handlers { + void handle_private_message( + const InstanceBundle &bundle, + const command::CommandLoader &command_loader, + const irc::Message<irc::MessageType::Privmsg> &message); +} diff --git a/src/main.cpp b/src/main.cpp index dffe4d0..7ba4c2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,12 @@ #include <iostream> #include <optional> +#include "bundle.hpp" #include "commands/command.hpp" #include "config.hpp" +#include "handlers.hpp" #include "irc/client.hpp" +#include "irc/message.hpp" #include "localization/localization.hpp" int main(int argc, char *argv[]) { @@ -28,6 +31,14 @@ int main(int argc, char *argv[]) { bot::loc::Localization localization("localization"); client.join(cfg.bot_username); + + client.on<bot::irc::MessageType::Privmsg>( + [&client, &command_loader, &localization]( + const bot::irc::Message<bot::irc::MessageType::Privmsg> &message) { + bot::InstanceBundle bundle{client, localization}; + bot::handlers::handle_private_message(bundle, command_loader, message); + }); + client.run(); return 0; |
