summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-12-15 00:47:20 +0500
committerilotterytea <iltsu@alright.party>2024-12-15 00:47:20 +0500
commit0581549cc5f5f6b80d681c55bed4b17783f1fd48 (patch)
tree9e1ab8204939476f603ee5cb28a817e4fb7fff4a
parent2b2611d5a2bb9f2bec1ef71285e3a4e2d848392c (diff)
feat: mcsrv command
-rw-r--r--bot/src/commands/command.cpp2
-rw-r--r--bot/src/modules/mcsrv.hpp82
-rw-r--r--docs/summary.md2
3 files changed, 85 insertions, 1 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index 169ca71..bd33c68 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -15,6 +15,7 @@
#include "../modules/help.hpp"
#include "../modules/join.hpp"
#include "../modules/massping.hpp"
+#include "../modules/mcsrv.hpp"
#include "../modules/notify.hpp"
#include "../modules/ping.hpp"
#include "../modules/settings.hpp"
@@ -40,6 +41,7 @@ namespace bot {
this->add_command(std::make_unique<mod::Spam>());
this->add_command(std::make_unique<mod::Settings>());
this->add_command(std::make_unique<mod::User>());
+ this->add_command(std::make_unique<mod::MinecraftServerCheck>());
}
void CommandLoader::add_command(std::unique_ptr<Command> command) {
diff --git a/bot/src/modules/mcsrv.hpp b/bot/src/modules/mcsrv.hpp
new file mode 100644
index 0000000..8027fc2
--- /dev/null
+++ b/bot/src/modules/mcsrv.hpp
@@ -0,0 +1,82 @@
+#pragma once
+
+#include <string>
+
+#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<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ 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<ResponseError::EXTERNAL_API_ERROR>(
+ 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<int>());
+ player_count += "/";
+ player_count += std::to_string(players["max"].get<int>());
+ }
+
+ 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/docs/summary.md b/docs/summary.md
index a56d0a0..102f0d5 100644
--- a/docs/summary.md
+++ b/docs/summary.md
@@ -26,6 +26,7 @@
## Miscellaneous
++ [Minecraft server status check](/wiki/misc/mcsrv)
+ [Ping](/wiki/misc/ping)
# [Errors](/wiki/errors)
@@ -40,5 +41,4 @@
## Miscellaneous
-+ [Minecraft server status check](/wiki/misc/mcsrv)
+ [Get today's holidays](/wiki/misc/holiday)