diff options
| -rw-r--r-- | bot/src/config.cpp | 14 | ||||
| -rw-r--r-- | bot/src/config.hpp | 4 | ||||
| -rw-r--r-- | luamods/randompost.lua | 46 |
3 files changed, 64 insertions, 0 deletions
diff --git a/bot/src/config.cpp b/bot/src/config.cpp index 010a0ec..714ff56 100644 --- a/bot/src/config.cpp +++ b/bot/src/config.cpp @@ -20,6 +20,11 @@ namespace bot { cmds["join_allowed"] = this->commands.join_allowed; cmds["join_allow_from_other_chats"] = this->commands.join_allow_from_other_chats; + if (this->commands.rpost_path.has_value()) { + cmds["rpost_path"] = this->commands.rpost_path.value(); + } else { + cmds["rpost_path"] = sol::nil; + } o["commands"] = cmds; // --- OWNER @@ -48,6 +53,11 @@ namespace bot { } else { url["paste_service"] = sol::nil; } + if (this->url.randompost.has_value()) { + url["randompost"] = this->url.randompost.value(); + } else { + url["randompost"] = sol::nil; + } o["url"] = url; return o; @@ -103,6 +113,8 @@ namespace bot { cmd_cfg.join_allowed = std::stoi(value); } else if (key == "commands.join_allow_from_other_chats") { cmd_cfg.join_allow_from_other_chats = std::stoi(value); + } else if (key == "commands.randompost.path") { + cmd_cfg.rpost_path = value; } else if (key == "owner.name") { @@ -115,6 +127,8 @@ namespace bot { url_cfg.help = value; } else if (key == "url.chatters.paste_service") { url_cfg.paste_service = value; + } else if (key == "url.randompost") { + url_cfg.randompost = value; } else if (key == "token.github") { diff --git a/bot/src/config.hpp b/bot/src/config.hpp index 11493d7..05ce0f0 100644 --- a/bot/src/config.hpp +++ b/bot/src/config.hpp @@ -1,6 +1,7 @@ #pragma once #include <optional> +#include <sol/sol.hpp> #include <string> #define GET_DATABASE_CONNECTION_URL(c) \ @@ -30,6 +31,8 @@ namespace bot { struct CommandConfiguration { bool join_allowed = true; bool join_allow_from_other_chats = false; + + std::optional<std::string> rpost_path = std::nullopt; }; struct OwnerConfiguration { @@ -40,6 +43,7 @@ namespace bot { struct UrlConfiguration { std::optional<std::string> help = std::nullopt; std::optional<std::string> paste_service = std::nullopt; + std::optional<std::string> randompost = std::nullopt; }; struct TokenConfiguration { diff --git a/luamods/randompost.lua b/luamods/randompost.lua new file mode 100644 index 0000000..24cbde9 --- /dev/null +++ b/luamods/randompost.lua @@ -0,0 +1,46 @@ +local lines = { + english = { + ["command_unavailable"] = "{sender.alias_name}: This command is not available.", + ["external_api_error"] = "{sender.alias_name}: Failed to get a random post. Try again later. (%s)", + ["success"] = "{sender.alias_name}: %s", + }, + russian = { + ["command_unavailable"] = "{sender.alias_name}: Эта команда недоступна.", + ["external_api_error"] = "{sender.alias_name}: Не удалось получить случайный пост. Попробуйте позже. (%s)", + ["success"] = "{sender.alias_name}: %s", + }, +} + +return { + name = "randompost", + delay_sec = 5, + options = {}, + subcommands = {}, + aliases = { "rpost", "rps", "rtnd" }, + 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.commands.rpost_path == nil or cfg.url.randompost == nil then + return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {}) + end + + response = net_get_with_headers(cfg.url.randompost, { Accept = "application/json" }) + + if 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.rpost_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, +} |
