From 1b34dc98d33c78cf2d09f0e46b83aceee0c43694 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 12 Apr 2025 17:33:47 +0500 Subject: feat: !mcsrv in lua --- bot/src/commands/command.cpp | 2 - bot/src/modules/join.hpp | 94 ------------------------------------- bot/src/modules/mcsrv.hpp | 82 -------------------------------- luamods/mcsrv.lua | 108 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 178 deletions(-) delete mode 100644 bot/src/modules/join.hpp delete mode 100644 bot/src/modules/mcsrv.hpp create mode 100644 luamods/mcsrv.lua diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index 8360e63..ad18cd2 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -16,7 +16,6 @@ #include "../modules/custom_command.hpp" #include "../modules/event.hpp" #include "../modules/help.hpp" -#include "../modules/mcsrv.hpp" #include "../modules/notify.hpp" #include "../modules/settings.hpp" #include "../modules/timer.hpp" @@ -37,7 +36,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/join.hpp b/bot/src/modules/join.hpp deleted file mode 100644 index 6818c01..0000000 --- a/bot/src/modules/join.hpp +++ /dev/null @@ -1,94 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "../bundle.hpp" -#include "../commands/command.hpp" -#include "../schemas/channel.hpp" - -namespace bot { - namespace mod { - class Join : public command::Command { - std::string get_name() const override { return "join"; } - - command::Response run(const InstanceBundle &bundle, - const command::Request &request) const override { - if (!bundle.configuration.commands.join_allowed) { - std::string owner = ""; - - if (bundle.configuration.owner.name.has_value()) { - owner = " " + bundle.localization - .get_formatted_line( - request, loc::LineId::MsgOwner, - {*bundle.configuration.owner.name}) - .value(); - } - - return command::Response( - bundle.localization - .get_formatted_line(request, loc::LineId::JoinNotAllowed, - {owner}) - .value()); - } - - if (!bundle.configuration.commands.join_allow_from_other_chats && - request.channel.get_alias_name() != - bundle.irc_client.get_bot_username()) { - return bundle.localization - .get_formatted_line(request, loc::LineId::JoinFromOtherChat, - {bundle.irc_client.get_bot_username()}) - .value(); - } - - pqxx::work work(request.conn); - - pqxx::result channels = - work.exec("SELECT * FROM channels WHERE alias_id = " + - std::to_string(request.user.get_alias_id())); - - if (!channels.empty()) { - schemas::Channel channel(channels[0]); - - if (channel.get_opted_out_at().has_value()) { - work.exec("UPDATE channels SET opted_out_at = null WHERE id = " + - std::to_string(channel.get_id())); - work.commit(); - - bundle.irc_client.join(channel.get_alias_name()); - - return command::Response( - bundle.localization - .get_formatted_line(request, loc::LineId::JoinRejoined, - {}) - .value()); - } - - return command::Response( - bundle.localization - .get_formatted_line(request, loc::LineId::JoinAlreadyIn, {}) - .value()); - } - - work.exec("INSERT INTO channels(alias_id, alias_name) VALUES (" + - std::to_string(request.user.get_alias_id()) + ", '" + - request.user.get_alias_name() + "')"); - work.commit(); - - bundle.irc_client.join(request.user.get_alias_name()); - bundle.irc_client.say( - request.user.get_alias_name(), - bundle.localization - .get_formatted_line(request, loc::LineId::JoinResponseInChat, - {}) - .value()); - - return command::Response( - bundle.localization - .get_formatted_line(request, loc::LineId::JoinResponse, {}) - .value()); - } - }; - } -} diff --git a/bot/src/modules/mcsrv.hpp b/bot/src/modules/mcsrv.hpp deleted file mode 100644 index 8027fc2..0000000 --- a/bot/src/modules/mcsrv.hpp +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#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 MinecraftServerCheck : public command::Command { - std::string get_name() const override { return "mcsrv"; } - - 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); - } - - cpr::Response response = cpr::Get(cpr::Url{ - "https://api.mcsrvstat.us/3/" + request.message.value()}); - - 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::string online = j["online"] ? "✅" : "⛔"; - std::string ip = "IP N/A"; - - if (j.contains("ip")) ip = j["ip"]; - - std::string player_count = "PLAYERS N/A"; - - if (j.contains("players")) { - auto players = j["players"]; - player_count = std::to_string(players["online"].get()); - player_count += "/"; - player_count += std::to_string(players["max"].get()); - } - - std::string version = "VERSION N/A"; - - if (j.contains("protocol")) { - auto protocol = j["protocol"]; - if (protocol.contains("name")) version = protocol["name"]; - } - - std::string motd = "MOTD N/A"; - - if (j.contains("motd")) { - auto motd_json = j["motd"]; - if (motd_json.contains("clean")) { - motd.clear(); - for (const auto &line : motd_json["clean"]) { - motd += line; - motd += " / "; - } - motd = motd.substr(0, motd.size() - 3); - motd = "\"" + motd + "\""; - } - } - - std::string msg = online + " " + request.message.value() + " (" + ip + - ") | " + player_count + " | " + motd + " | " + - version; - - return command::Response(msg); - } - }; - } -} diff --git a/luamods/mcsrv.lua b/luamods/mcsrv.lua new file mode 100644 index 0000000..ef94f3d --- /dev/null +++ b/luamods/mcsrv.lua @@ -0,0 +1,108 @@ +local lines = { + english = { + ["no_message"] = "{sender.alias_name}: No IP or hostname 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 | %s | %s" + }, + russian = { + ["no_message"] = "{sender.alias_name}: IP или хост должен быть предоставлен.", + ["not_found"] = "{sender.alias_name}: %s не найден.", + ["external_api_error"] = "{sender.alias_name}: Ошибка стороннего API. Попробуйте позже. (%s)", + ["success"] = "{sender.alias_name}: %s %s (%s) | %s | %s | %s" + }, +} + +return { + name = "mcsrv", + description = [[ +The `!mcsrv` command allows you to quickly find out the status of Minecraft server. +This is a handy command that solves the problem of +logging into Minecraft and waiting for 20 seconds to load to check the server. + +## Syntax +`!mcsrv [address]` + ++ `[address]` - IP address or name address of the server. + +## Usage + ++ `!mcsrv mc.hypixel.net` ++ `!mcsrv 12.255.56.21` + +## Responses + ++ `✅ hypixel.net (209.222.114.115) | 36911/200000 | Hypixel Network [1.8-1.20]; HOLIDAYS EVENT | TRIPLE COINS AND EXP | 1.8.9` ++ `⛔ 12.255.56.21 (127.0.0.1)` + +## The meanings of the parts of the message *(separated by |)* + ++ Alphabetic and numeric IP addresses. ++ The number of people playing at the moment and the maximum number of players. ++ The MOTD of the server. Separated by **;** *(semicolon)*. ++ Server version. + + +## Important notes + ++ The server status is taken from the third-party API ["mcsrvstat.us"](https://mcsrvstat.us). +]], + delay_sec = 10, + options = {}, + subcommands = {}, + aliases = {}, + minimal_rights = "user", + handle = function(request) + if request.message == nil then + return l10n_custom_formatted_line_request(request, lines, "no_message", {}) + end + + local response = net_get("https://api.mcsrvstat.us/3/" .. request.message) + + 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 online = "⛔" + if j.online ~= nil and j.online then + online = "✅" + end + + local ip = "IP N/A" + if j.ip ~= nil then + ip = j.ip + end + + local players = "PLAYERS N/A" + if j.players ~= nil and j.players.online ~= nil and j.players.max ~= nil then + players = j.players.online .. '/' .. j.players.max + end + + local version = "VERSION N/A" + if j.protocol ~= nil and j.protocol.name ~= nil then + version = j.protocol.name + end + + local motd = "MOTD N/A" + if j.motd ~= nil and j.motd.clean ~= nil then + motd = "" + for i = 1, #j.motd.clean, 1 do + motd = motd .. j.motd.clean[i] + if i + 1 < #j.motd.clean then + motd = motd .. " / " + end + end + motd = '"' .. motd .. '"' + end + + return l10n_custom_formatted_line_request(request, lines, "success", { + online, request.message, ip, players, version, motd + }) + end +} -- cgit v1.2.3