diff options
| -rw-r--r-- | bot/src/commands/command.cpp | 2 | ||||
| -rw-r--r-- | bot/src/config.cpp | 13 | ||||
| -rw-r--r-- | bot/src/config.hpp | 4 | ||||
| -rw-r--r-- | bot/src/modules/chatters.hpp | 78 | ||||
| -rw-r--r-- | luamods/chatters.lua | 67 |
5 files changed, 84 insertions, 80 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index 19cd31c..73ea90d 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -13,7 +13,6 @@ #include <string> #include "../bundle.hpp" -#include "../modules/chatters.hpp" #include "../modules/custom_command.hpp" #include "../modules/event.hpp" #include "../modules/help.hpp" @@ -39,7 +38,6 @@ namespace bot { this->add_command(std::make_unique<mod::CustomCommand>()); this->add_command(std::make_unique<mod::Timer>()); this->add_command(std::make_unique<mod::Help>()); - this->add_command(std::make_unique<mod::Chatters>()); this->add_command(std::make_unique<mod::Spam>()); this->add_command(std::make_unique<mod::Settings>()); this->add_command(std::make_unique<mod::User>()); diff --git a/bot/src/config.cpp b/bot/src/config.cpp index 714ff56..cfd900f 100644 --- a/bot/src/config.cpp +++ b/bot/src/config.cpp @@ -25,6 +25,13 @@ namespace bot { } else { cmds["rpost_path"] = sol::nil; } + if (this->commands.paste_path.has_value()) { + cmds["paste_path"] = this->commands.paste_path.value(); + } else { + cmds["paste_path"] = sol::nil; + } + cmds["paste_body_name"] = this->commands.paste_body_name; + cmds["paste_title_name"] = this->commands.paste_title_name; o["commands"] = cmds; // --- OWNER @@ -115,6 +122,12 @@ namespace bot { cmd_cfg.join_allow_from_other_chats = std::stoi(value); } else if (key == "commands.randompost.path") { cmd_cfg.rpost_path = value; + } else if (key == "commands.paste_path") { + cmd_cfg.paste_path = value; + } else if (key == "paste_body_name") { + cmd_cfg.paste_body_name = value; + } else if (key == "paste_title_name") { + cmd_cfg.paste_title_name = value; } else if (key == "owner.name") { diff --git a/bot/src/config.hpp b/bot/src/config.hpp index 05ce0f0..1e918b6 100644 --- a/bot/src/config.hpp +++ b/bot/src/config.hpp @@ -33,6 +33,10 @@ namespace bot { bool join_allow_from_other_chats = false; std::optional<std::string> rpost_path = std::nullopt; + + std::optional<std::string> paste_path = std::nullopt; + std::string paste_body_name = "paste"; + std::string paste_title_name = "title"; }; struct OwnerConfiguration { diff --git a/bot/src/modules/chatters.hpp b/bot/src/modules/chatters.hpp deleted file mode 100644 index 6e4c61c..0000000 --- a/bot/src/modules/chatters.hpp +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once - -#include <ctime> -#include <iomanip> -#include <sstream> -#include <string> -#include <vector> - -#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; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - if (!bundle.configuration.url.paste_service.has_value()) { - throw ResponseException<ResponseError::ILLEGAL_COMMAND>( - 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 command::Response( - bundle.localization - .get_formatted_line(request, loc::LineId::ChattersResponse, - {url}) - .value()); - } else { - throw ResponseException<ResponseError::EXTERNAL_API_ERROR>( - request, bundle.localization, response.status_code, - response.status_line); - } - } - }; -} diff --git a/luamods/chatters.lua b/luamods/chatters.lua new file mode 100644 index 0000000..a3c3820 --- /dev/null +++ b/luamods/chatters.lua @@ -0,0 +1,67 @@ +local lines = { + english = { + ["command_unavailable"] = "{sender.alias_name}: This command is not available.", + ["external_api_error"] = "{sender.alias_name}: Failed to post a chatter list. Try again later. (%s)", + ["channel_not_found"] = "{sender.alias_name}: Channel %s not found", + ["success"] = "{sender.alias_name}: %s", + }, + russian = { + ["command_unavailable"] = "{sender.alias_name}: Эта команда недоступна.", + ["external_api_error"] = "{sender.alias_name}: Не удалось отправить список чаттеров. Попробуйте позже. (%s)", + ["channel_not_found"] = "{sender.alias_name}: Канал %s не найден", + ["success"] = "{sender.alias_name}: %s", + }, +} + +return { + name = "chatters", + delay_sec = 30, + options = {}, + subcommands = {}, + aliases = { "chatterlist", "clist", "ulist", "userlist" }, + minimal_rights = "user", + handle = function(request) + cfg = bot_config() + if cfg == nil then + return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {}) + end + + if + cfg.url.paste_service == nil + or cfg.commands.paste_body_name == nil + or cfg.commands.paste_title_name == nil + then + return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {}) + end + + chatters = twitch_get_chatters() + body = #chatters .. " chatters\r\n---------------------\r\n\r\n" + + for i = 1, #chatters, 1 do + chatter = chatters[i] + body = body .. chatter.login .. "\r\n" + end + + time = time_format(time_current(), "%d.%m.%Y %H:%M:%S %z") + + response = net_post_multipart_with_headers(cfg.url.paste_service, { + [cfg.commands.paste_body_name] = body, + [cfg.commands.paste_title_name] = request.channel.alias_name .. "'s chatter list on " .. time, + }, { + Accept = "application/json", + }) + + if response.code ~= 201 and response.code ~= 200 then + return l10n_custom_formatted_line_request(request, lines, "external_api_error", { response.code }) + end + + body = json_parse(response.text) + + link = json_get_value(body, cfg.commands.paste_path) + if link == nil then + return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {}) + end + + return l10n_custom_formatted_line_request(request, lines, "success", { link }) + end, +} |
