From 52fd4ea8bad5cd7d3940a41df4f8f54b4e72beae Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 7 Dec 2024 17:24:25 +0500 Subject: feat: a special class for command responses --- bot/src/commands/command.cpp | 6 +++--- bot/src/commands/command.hpp | 10 +++++----- bot/src/commands/response.cpp | 40 ++++++++++++++++++++++++++++++++++++++ bot/src/commands/response.hpp | 25 ++++++++++++++++++++++++ bot/src/handlers.cpp | 16 +++++---------- bot/src/modules/chatters.hpp | 14 ++++++------- bot/src/modules/custom_command.hpp | 24 +++++++++++------------ bot/src/modules/event.hpp | 20 +++++++++---------- bot/src/modules/help.hpp | 15 +++++++------- bot/src/modules/join.hpp | 37 +++++++++++++++++++---------------- bot/src/modules/massping.hpp | 8 +++----- bot/src/modules/notify.hpp | 20 +++++++++---------- bot/src/modules/ping.hpp | 17 ++++++++-------- bot/src/modules/timer.hpp | 21 ++++++++++---------- 14 files changed, 166 insertions(+), 107 deletions(-) create mode 100644 bot/src/commands/response.cpp create mode 100644 bot/src/commands/response.hpp (limited to 'bot/src') diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index b656254..8d75999 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -20,6 +20,7 @@ #include "../modules/timer.hpp" #include "../utils/chrono.hpp" #include "request.hpp" +#include "response.hpp" namespace bot { namespace command { @@ -39,9 +40,8 @@ namespace bot { this->commands.push_back(std::move(command)); } - std::optional, std::string>> - CommandLoader::run(const InstanceBundle &bundle, - const Request &request) const { + std::optional 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; }); diff --git a/bot/src/commands/command.hpp b/bot/src/commands/command.hpp index 40ec114..793446e 100644 --- a/bot/src/commands/command.hpp +++ b/bot/src/commands/command.hpp @@ -3,11 +3,11 @@ #include #include #include -#include #include #include "../bundle.hpp" #include "request.hpp" +#include "response.hpp" namespace bot { namespace command { @@ -24,8 +24,8 @@ namespace bot { class Command { public: virtual std::string get_name() const = 0; - virtual std::variant, std::string> run( - const InstanceBundle &bundle, const Request &request) const = 0; + virtual Response run(const InstanceBundle &bundle, + const Request &request) const = 0; virtual schemas::PermissionLevel get_permission_level() const { return schemas::PermissionLevel::USER; } @@ -41,8 +41,8 @@ namespace bot { ~CommandLoader() = default; void add_command(std::unique_ptr cmd); - std::optional, std::string>> run( - const InstanceBundle &bundle, const Request &msg) const; + std::optional run(const InstanceBundle &bundle, + const Request &msg) const; const std::vector> &get_commands() const { return this->commands; diff --git a/bot/src/commands/response.cpp b/bot/src/commands/response.cpp new file mode 100644 index 0000000..cf13009 --- /dev/null +++ b/bot/src/commands/response.cpp @@ -0,0 +1,40 @@ +#include "response.hpp" + +#include +#include +#include + +namespace bot::command { + Response::Response() { + this->single = std::nullopt; + this->multiple = std::nullopt; + } + + Response::Response(std::string single) { + this->single = single; + this->multiple = std::nullopt; + } + + Response::Response(std::vector multiple) { + this->single = std::nullopt; + this->multiple = multiple; + } + + const std::string Response::get_single() const { + return this->single.value(); + } + + const std::vector Response::get_multiple() const { + return this->multiple.value(); + } + + const bool Response::is_single() const { return this->single.has_value(); } + + const bool Response::is_multiple() const { + return this->multiple.has_value(); + } + + const bool Response::is_empty() const { + return !this->single.has_value() && !this->multiple.has_value(); + } +} \ No newline at end of file diff --git a/bot/src/commands/response.hpp b/bot/src/commands/response.hpp new file mode 100644 index 0000000..2f05fd8 --- /dev/null +++ b/bot/src/commands/response.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include + +namespace bot::command { + class Response { + public: + Response(); + Response(std::string single); + Response(std::vector multiple); + + const std::string get_single() const; + const std::vector get_multiple() const; + + const bool is_single() const; + const bool is_multiple() const; + const bool is_empty() const; + + private: + std::optional single; + std::optional> multiple; + }; +} \ No newline at end of file diff --git a/bot/src/handlers.cpp b/bot/src/handlers.cpp index c7820b4..e764652 100644 --- a/bot/src/handlers.cpp +++ b/bot/src/handlers.cpp @@ -36,18 +36,12 @@ namespace bot::handlers { auto response = command_loader.run(bundle, request.value()); if (response.has_value()) { - try { - auto str = std::get(*response); - bundle.irc_client.say(message.source.login, str); - } catch (const std::exception &e) { - } - - try { - auto strs = std::get>(*response); - for (const std::string &str : strs) { - bundle.irc_client.say(message.source.login, str); + if (response->is_single()) { + bundle.irc_client.say(message.source.login, response->get_single()); + } else if (response->is_multiple()) { + for (const std::string &msg : response->get_multiple()) { + bundle.irc_client.say(message.source.login, msg); } - } catch (const std::exception &e) { } } } catch (const std::exception &e) { diff --git a/bot/src/modules/chatters.hpp b/bot/src/modules/chatters.hpp index fe921ab..6e4c61c 100644 --- a/bot/src/modules/chatters.hpp +++ b/bot/src/modules/chatters.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "../bundle.hpp" @@ -25,9 +24,8 @@ namespace bot::mod { int get_delay_seconds() const override { return 10; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!bundle.configuration.url.paste_service.has_value()) { throw ResponseException( request, bundle.localization); @@ -65,9 +63,11 @@ namespace bot::mod { std::string url = *bundle.configuration.url.paste_service + "/" + id; - return bundle.localization - .get_formatted_line(request, loc::LineId::ChattersResponse, {url}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::ChattersResponse, + {url}) + .value()); } else { throw ResponseException( request, bundle.localization, response.status_code, diff --git a/bot/src/modules/custom_command.hpp b/bot/src/modules/custom_command.hpp index 50b3692..9fd1429 100644 --- a/bot/src/modules/custom_command.hpp +++ b/bot/src/modules/custom_command.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -21,9 +20,8 @@ namespace bot { return {"new", "remove"}; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!request.subcommand_id.has_value()) { throw ResponseException( request, bundle.localization, command::SUBCOMMAND); @@ -68,10 +66,11 @@ namespace bot { "', '" + m + "')"); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::CustomcommandNew, - {name}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::CustomcommandNew, + {name}) + .value()); } else if (subcommand_id == "remove") { if (cmds.empty()) { throw ResponseException( @@ -82,10 +81,11 @@ namespace bot { std::to_string(cmds[0][0].as())); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::CustomcommandDelete, - {name}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line( + request, loc::LineId::CustomcommandDelete, {name}) + .value()); } throw ResponseException( diff --git a/bot/src/modules/event.hpp b/bot/src/modules/event.hpp index 4242f07..58695d9 100644 --- a/bot/src/modules/event.hpp +++ b/bot/src/modules/event.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -22,9 +21,8 @@ namespace bot { return {"on", "off"}; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!request.subcommand_id.has_value()) { throw ResponseException( request, bundle.localization, command::SUBCOMMAND); @@ -119,9 +117,10 @@ namespace bot { work.exec(query); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::EventOn, {t}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::EventOn, {t}) + .value()); } else if (subcommand_id == "off") { if (event.empty()) { throw ResponseException( @@ -132,9 +131,10 @@ namespace bot { std::to_string(event[0][0].as())); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::EventOff, {t}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::EventOff, {t}) + .value()); } throw ResponseException( diff --git a/bot/src/modules/help.hpp b/bot/src/modules/help.hpp index 13af228..1341b86 100644 --- a/bot/src/modules/help.hpp +++ b/bot/src/modules/help.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -13,18 +12,18 @@ namespace bot { class Help : public command::Command { std::string get_name() const override { return "help"; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!bundle.configuration.url.help.has_value()) { throw ResponseException( request, bundle.localization); } - return bundle.localization - .get_formatted_line(request, loc::LineId::HelpResponse, - {*bundle.configuration.url.help}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::HelpResponse, + {*bundle.configuration.url.help}) + .value()); } }; } diff --git a/bot/src/modules/join.hpp b/bot/src/modules/join.hpp index 16e8b4a..6818c01 100644 --- a/bot/src/modules/join.hpp +++ b/bot/src/modules/join.hpp @@ -2,7 +2,6 @@ #include #include -#include #include #include "../bundle.hpp" @@ -14,9 +13,8 @@ namespace bot { class Join : public command::Command { std::string get_name() const override { return "join"; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!bundle.configuration.commands.join_allowed) { std::string owner = ""; @@ -28,10 +26,11 @@ namespace bot { .value(); } - return bundle.localization - .get_formatted_line(request, loc::LineId::JoinNotAllowed, - {owner}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::JoinNotAllowed, + {owner}) + .value()); } if (!bundle.configuration.commands.join_allow_from_other_chats && @@ -59,14 +58,17 @@ namespace bot { bundle.irc_client.join(channel.get_alias_name()); - return bundle.localization - .get_formatted_line(request, loc::LineId::JoinRejoined, {}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::JoinRejoined, + {}) + .value()); } - return bundle.localization - .get_formatted_line(request, loc::LineId::JoinAlreadyIn, {}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::JoinAlreadyIn, {}) + .value()); } work.exec("INSERT INTO channels(alias_id, alias_name) VALUES (" + @@ -82,9 +84,10 @@ namespace bot { {}) .value()); - return bundle.localization - .get_formatted_line(request, loc::LineId::JoinResponse, {}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::JoinResponse, {}) + .value()); } }; } diff --git a/bot/src/modules/massping.hpp b/bot/src/modules/massping.hpp index 2957e34..23d67d3 100644 --- a/bot/src/modules/massping.hpp +++ b/bot/src/modules/massping.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -18,9 +17,8 @@ namespace bot { int get_delay_seconds() const override { return 1; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { auto chatters = bundle.helix_client.get_chatters( request.channel.get_alias_id(), bundle.irc_client.get_bot_id()); @@ -55,7 +53,7 @@ namespace bot { msgs2.push_back(base + m); } - return msgs2; + return command::Response(msgs2); } }; } diff --git a/bot/src/modules/notify.hpp b/bot/src/modules/notify.hpp index 3587e73..a25b34f 100644 --- a/bot/src/modules/notify.hpp +++ b/bot/src/modules/notify.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -18,9 +17,8 @@ namespace bot { return {"sub", "unsub"}; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!request.subcommand_id.has_value()) { throw ResponseException( request, bundle.localization, command::SUBCOMMAND); @@ -105,9 +103,10 @@ namespace bot { std::to_string(request.user.get_id())); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::NotifySub, {t}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::NotifySub, {t}) + .value()); } else if (subcommand_id == "unsub") { if (subs.empty()) { throw ResponseException( @@ -118,9 +117,10 @@ namespace bot { std::to_string(subs[0][0].as())); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::NotifyUnsub, {t}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::NotifyUnsub, {t}) + .value()); } throw ResponseException( diff --git a/bot/src/modules/ping.hpp b/bot/src/modules/ping.hpp index 836917d..cc982f6 100644 --- a/bot/src/modules/ping.hpp +++ b/bot/src/modules/ping.hpp @@ -6,7 +6,6 @@ #include #include -#include #include #include "../bundle.hpp" @@ -18,9 +17,8 @@ namespace bot { class Ping : public command::Command { std::string get_name() const override { return "ping"; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { auto now = std::chrono::steady_clock::now(); auto duration = now - START_TIME; auto seconds = @@ -48,11 +46,12 @@ namespace bot { cpp_info.append(" ยท "); } - return bundle.localization - .get_formatted_line( - request, loc::LineId::PingResponse, - {cpp_info, uptime, std::to_string(used_memory)}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line( + request, loc::LineId::PingResponse, + {cpp_info, uptime, std::to_string(used_memory)}) + .value()); } }; } diff --git a/bot/src/modules/timer.hpp b/bot/src/modules/timer.hpp index 36c3982..5fd3549 100644 --- a/bot/src/modules/timer.hpp +++ b/bot/src/modules/timer.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../bundle.hpp" @@ -21,9 +20,8 @@ namespace bot { return {"new", "remove"}; } - std::variant, std::string> run( - const InstanceBundle &bundle, - const command::Request &request) const override { + command::Response run(const InstanceBundle &bundle, + const command::Request &request) const override { if (!request.subcommand_id.has_value()) { throw ResponseException( request, bundle.localization, command::SUBCOMMAND); @@ -86,9 +84,10 @@ namespace bot { "', '" + m + "', " + std::to_string(interval_s) + ")"); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::TimerNew, {name}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::TimerNew, {name}) + .value()); } else if (subcommand_id == "remove") { if (timers.empty()) { throw ResponseException( @@ -99,9 +98,11 @@ namespace bot { std::to_string(timers[0][0].as())); work.commit(); - return bundle.localization - .get_formatted_line(request, loc::LineId::TimerDelete, {name}) - .value(); + return command::Response( + bundle.localization + .get_formatted_line(request, loc::LineId::TimerDelete, + {name}) + .value()); } throw ResponseException( -- cgit v1.2.3