From aad647cdb74c5773dd2ce45d063843e387d6b67c Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 15 Apr 2025 04:30:55 +0500 Subject: feat: !userid in lua --- bot/src/commands/command.cpp | 2 - bot/src/modules/user.hpp | 95 -------------------------------- luamods/userid.lua | 128 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 97 deletions(-) delete mode 100644 bot/src/modules/user.hpp create mode 100644 luamods/userid.lua diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index ad18cd2..18273e4 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -19,7 +19,6 @@ #include "../modules/notify.hpp" #include "../modules/settings.hpp" #include "../modules/timer.hpp" -#include "../modules/user.hpp" #include "../utils/chrono.hpp" #include "commands/lua.hpp" #include "modules/lua.hpp" @@ -35,7 +34,6 @@ 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()); this->add_command(std::make_unique()); this->add_command(std::make_unique()); diff --git a/bot/src/modules/user.hpp b/bot/src/modules/user.hpp deleted file mode 100644 index 71f2747..0000000 --- a/bot/src/modules/user.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "../bundle.hpp" -#include "../commands/command.hpp" -#include "../commands/response_error.hpp" -#include "cpr/api.h" -#include "cpr/cprtypes.h" -#include "cpr/response.h" -#include "nlohmann/json.hpp" - -namespace bot { - namespace mod { - class User : public command::Command { - std::string get_name() const override { return "userid"; } - - int get_delay_seconds() const override { return 10; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - if (!request.message.has_value()) { - throw ResponseException( - request, bundle.localization, command::CommandArgument::VALUE); - } - - std::vector parts = - utils::string::split_text(request.message.value(), ','); - - std::vector ids; - std::vector logins; - - for (const std::string &part : parts) { - if (ids.size() + logins.size() >= 3) break; - try { - int id = std::stoi(part); - ids.push_back(part); - } catch (std::exception e) { - logins.push_back(part); - } - } - - std::string query; - - if (!ids.empty()) { - query += "id="; - query += utils::string::join_vector(ids, ','); - - if (!logins.empty()) query += "&"; - } - - if (!logins.empty()) { - query += "login="; - query += utils::string::join_vector(logins, ','); - } - - if (query.empty()) { - throw ResponseException( - request, bundle.localization, request.message.value()); - } - - cpr::Response response = - cpr::Get(cpr::Url{"https://api.ivr.fi/v2/twitch/user?" + query}); - - if (response.status_code != 200) { - throw ResponseException( - request, bundle.localization, response.status_code, - response.status_line); - } - - nlohmann::json j = nlohmann::json::parse(response.text); - - std::vector msgs; - - for (const auto &x : j) { - std::string name = x["login"]; - std::string id = x["id"]; - - std::string is_banned = x["banned"] ? "⛔" : "✅"; - std::string ban_reason; - if (x.contains("banReason")) ban_reason = x["banReason"]; - - std::string msg = is_banned + " " + name + " (" + id + ")" + - (ban_reason.empty() ? "" : ": ") + ban_reason; - - msgs.push_back(request.user.get_alias_name() + ": " + msg); - } - - return command::Response(msgs); - } - }; - } -} diff --git a/luamods/userid.lua b/luamods/userid.lua new file mode 100644 index 0000000..db7b519 --- /dev/null +++ b/luamods/userid.lua @@ -0,0 +1,128 @@ +local lines = { + english = { + ["no_message"] = "{sender.alias_name}: No username provided.", + ["not_found"] = "{sender.alias_name}: %s not found.", + ["external_api_error"] = "{sender.alias_name}: External API error. Try again later. (%s)", + ["success"] = "{sender.alias_name}: %s %s (%s)%s" + }, + russian = { + ["no_message"] = "{sender.alias_name}: Имя пользователя должен быть предоставлен.", + ["not_found"] = "{sender.alias_name}: %s не найден.", + ["external_api_error"] = "{sender.alias_name}: Ошибка стороннего API. Попробуйте позже. (%s)", + ["success"] = "{sender.alias_name}: %s %s (%s)%s" + }, +} + +return { + name = "userid", + description = [[ +The `!userid` command allows you to check if the specified users +exist, or if they are banned, or if they are OK. + +## Syntax + +`!userid [users...]` + ++ `[users...]` - User ID or user names. Separated by **,** *(colon)*. + +## Usage + ++ `!userid drdisrespect` ++ `!userid 22484632` ++ `!userid drdisrespect,22484632,okayeg` + +## Responses + ++ `⛔ drdisrespect (17337557): TOS_INDEFINITE` ++ `✅ forsen (22484632)` ++ `✅ okayeg (489147225)` + +## Important notes + ++ User information is taken from the third-party API service ["ivr.fi"](https://api.ivr.fi/v2/docs) +]], + delay_sec = 2, + options = {}, + subcommands = {}, + aliases = { "uid", "banned", "isbanned", "isban", "bancheck" }, + minimal_rights = "user", + handle = function(request) + if request.message == nil then + return l10n_custom_formatted_line_request(request, lines, "no_message", {}) + end + + local parts = str_split(request.message, ' ') + + local ids = {} + local logins = {} + + for i = 1, #parts, 1 do + if #ids + #logins >= 3 then + break + end + + local part = parts[i] + local id = tonumber(part) + + if id == nil then + table.insert(logins, part) + else + table.insert(ids, id) + end + end + + local query = "" + + if #ids > 0 then + query = "id=" .. table.concat(ids, ',') + end + + if #logins > 0 then + if #ids > 0 then + query = query .. "&" + end + query = query .. "login=" .. table.concat(logins, ',') + end + + if #query == 0 then + return l10n_custom_formatted_line_request(request, lines, "no_message", {}) + end + + local response = net_get("https://api.ivr.fi/v2/twitch/user?" .. query) + + if response.code == 404 then + return l10n_custom_formatted_line_request(request, lines, "not_found", { request.message }) + end + + if response.code ~= 200 then + return l10n_custom_formatted_line_request(request, lines, "external_api_error", { response.code }) + end + + local j = json_parse(response.text) + + local msgs = {} + + for i = 1, #j, 1 do + local u = j[i] + + local name = u.login + local id = u.id + + local is_banned = "✅" + if u.banned then + is_banned = "⛔" + end + + local ban_reason = "" + if u.banReason ~= nil then + ban_reason = ": " .. u.banReason + end + + table.insert(msgs, l10n_custom_formatted_line_request(request, lines, "success", { + is_banned, name, id, ban_reason + })) + end + + return msgs + end +} -- cgit v1.2.3