From 2a90152bc7b3a9009b3a90cbc021fde8383f4b9b Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 25 May 2024 00:30:01 +0500 Subject: feat: new command - chatters --- bot/src/commands/command.cpp | 2 ++ bot/src/config.cpp | 2 ++ bot/src/config.hpp | 1 + bot/src/localization/line_id.cpp | 4 +++ bot/src/localization/line_id.hpp | 4 ++- bot/src/modules/chatters.hpp | 78 ++++++++++++++++++++++++++++++++++++++++ localization/english.json | 4 ++- localization/russian.json | 4 ++- 8 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 bot/src/modules/chatters.hpp diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index e3b45b1..b656254 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -9,6 +9,7 @@ #include #include "../bundle.hpp" +#include "../modules/chatters.hpp" #include "../modules/custom_command.hpp" #include "../modules/event.hpp" #include "../modules/help.hpp" @@ -31,6 +32,7 @@ namespace bot { this->add_command(std::make_unique()); this->add_command(std::make_unique()); this->add_command(std::make_unique()); + this->add_command(std::make_unique()); } void CommandLoader::add_command(std::unique_ptr command) { diff --git a/bot/src/config.cpp b/bot/src/config.cpp index ec55913..6c8e6d6 100644 --- a/bot/src/config.cpp +++ b/bot/src/config.cpp @@ -68,6 +68,8 @@ namespace bot { else if (key == "url.help") { url_cfg.help = value; + } else if (key == "url.chatters.paste_service") { + url_cfg.paste_service = value; } } diff --git a/bot/src/config.hpp b/bot/src/config.hpp index 5c437d6..8707d02 100644 --- a/bot/src/config.hpp +++ b/bot/src/config.hpp @@ -39,6 +39,7 @@ namespace bot { struct UrlConfiguration { std::optional help = std::nullopt; + std::optional paste_service = std::nullopt; }; struct Configuration { diff --git a/bot/src/localization/line_id.cpp b/bot/src/localization/line_id.cpp index 567a3ba..dabd85a 100644 --- a/bot/src/localization/line_id.cpp +++ b/bot/src/localization/line_id.cpp @@ -92,6 +92,10 @@ namespace bot { return LineId::HelpResponse; } + else if (str == "chatters.response") { + return LineId::ChattersResponse; + } + else { return std::nullopt; } diff --git a/bot/src/localization/line_id.hpp b/bot/src/localization/line_id.hpp index 41ceec6..6d7729e 100644 --- a/bot/src/localization/line_id.hpp +++ b/bot/src/localization/line_id.hpp @@ -48,7 +48,9 @@ namespace bot { TimerNew, TimerDelete, - HelpResponse + HelpResponse, + + ChattersResponse }; std::optional string_to_line_id(const std::string &str); diff --git a/bot/src/modules/chatters.hpp b/bot/src/modules/chatters.hpp new file mode 100644 index 0000000..12d641f --- /dev/null +++ b/bot/src/modules/chatters.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "../bundle.hpp" +#include "../commands/command.hpp" +#include "../commands/response_error.hpp" +#include "cpr/api.h" +#include "cpr/multipart.h" +#include "cpr/response.h" +#include "nlohmann/json.hpp" + +namespace bot::mod { + class Chatters : public command::Command { + std::string get_name() const override { return "chatters"; } + + schemas::PermissionLevel get_permission_level() const override { + return schemas::PermissionLevel::USER; + } + + int get_delay_seconds() const override { return 10; } + + std::variant, std::string> run( + const InstanceBundle &bundle, + const command::Request &request) const override { + if (!bundle.configuration.url.paste_service.has_value()) { + throw ResponseException( + request, bundle.localization); + } + + auto chatters = bundle.helix_client.get_chatters( + request.channel.get_alias_id(), bundle.irc_client.get_bot_id()); + + std::string body; + + for (const auto &chatter : chatters) { + body += chatter.login + '\n'; + } + + std::time_t t = std::time(nullptr); + std::tm *now = std::localtime(&t); + + std::ostringstream oss; + + oss << std::put_time(now, "%d.%m.%Y %H:%M:%s"); + + cpr::Multipart multipart = { + {"paste", body}, + {"title", request.channel.get_alias_name() + "'s chatter list on " + + oss.str()}}; + + cpr::Response response = cpr::Post( + cpr::Url{*bundle.configuration.url.paste_service + "/paste"}, + multipart); + + if (response.status_code == 201) { + nlohmann::json j = nlohmann::json::parse(response.text); + + std::string id = j["data"]["id"]; + + std::string url = *bundle.configuration.url.paste_service + "/" + id; + + return bundle.localization + .get_formatted_line(request, loc::LineId::ChattersResponse, {url}) + .value(); + } else { + throw ResponseException( + request, bundle.localization, response.status_code, + response.status_line); + } + } + }; +} diff --git a/localization/english.json b/localization/english.json index f6679ef..a198783 100644 --- a/localization/english.json +++ b/localization/english.json @@ -41,5 +41,7 @@ "timer.new": "{sender.alias_name}: New timer \"%s\" has been successfully created!", "timer.delete": "{sender.alias_name}: The timer \"%s\" has been deleted!", - "help.response": "{sender.alias_name}: Bot command reference: %s" + "help.response": "{sender.alias_name}: Bot command reference: %s", + + "chatters.response": "{sender.alias_name}: Chatter list: %s" } diff --git a/localization/russian.json b/localization/russian.json index e68d770..f170bb3 100644 --- a/localization/russian.json +++ b/localization/russian.json @@ -41,5 +41,7 @@ "timer.new": "{sender.alias_name}: Новый таймер \"%s\" был успешно создан!", "timer.delete": "{sender.alias_name}: Таймер \"%s\" был удален!", - "help.response": "{sender.alias_name}: Справочник по командам бота: %s" + "help.response": "{sender.alias_name}: Справочник по командам бота: %s", + + "chatters.response": "{sender.alias_name}: Список чаттеров: %s" } -- cgit v1.2.3