From d0c56b41397ddc8364d4a3834cc7f59fe9102f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A8=D0=B8?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD=D1=81=D0=BA=D0=B8=D0=B9?= Date: Thu, 16 Nov 2023 23:47:28 +0300 Subject: [PATCH] fix player count game status --- JusticeHandler.cs | 8 ++-- Startup.cs | 2 +- Types/ServerResponse.cs | 93 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 Types/ServerResponse.cs diff --git a/JusticeHandler.cs b/JusticeHandler.cs index 82d6f21..4abcc60 100644 --- a/JusticeHandler.cs +++ b/JusticeHandler.cs @@ -3,6 +3,7 @@ using Discord.Interactions; using Discord.WebSocket; using DiscordApp.Database; using DiscordApp.Justice.Commands; +using DiscordApp.Types; using Microsoft.VisualBasic; using Newtonsoft.Json; using System.Reflection; @@ -50,10 +51,9 @@ namespace DiscordApp try { var request = await http.GetAsync("https://api.mcsrvstat.us/3/pl.spworlds.ru"); - JsonNode responseAboutPL = JsonNode.Parse(request.Content.ReadAsStringAsync().Result); - bool result; - if (bool.TryParse(responseAboutPL["online"].ToString(), out result) || result) await client.SetGameAsync($"выключенный PL", "https://yaflay.ru/", ActivityType.Watching); - else await client.SetGameAsync($"онлайн на PL: {responseAboutPL["players"]["online"]}", "https://yaflay.ru/", ActivityType.Watching); + var responseAboutPL = JsonConvert.DeserializeObject(request.Content.ReadAsStringAsync().Result); + if (!responseAboutPL.online) await client.SetGameAsync($"выключенный PL", "https://yawaflua.ru/", ActivityType.Watching); + else await client.SetGameAsync($"онлайн на PL: {responseAboutPL.players.online}", "https://yawaflua.ru/", ActivityType.Watching); } finally { diff --git a/Startup.cs b/Startup.cs index 2689dee..eae4e29 100644 --- a/Startup.cs +++ b/Startup.cs @@ -138,7 +138,7 @@ namespace DiscordApp } else { - throw new Exception("Unknown error from site!"); + throw new Exception($"Unknown error from site! {request.Content.ReadAsStringAsync().Result}") ; } } catch (Exception ex) diff --git a/Types/ServerResponse.cs b/Types/ServerResponse.cs new file mode 100644 index 0000000..290f1bb --- /dev/null +++ b/Types/ServerResponse.cs @@ -0,0 +1,93 @@ +namespace DiscordApp.Types +{ + // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); + public class A + { + public string name { get; set; } + public string type { get; set; } + public string @class { get; set; } + public int ttl { get; set; } + public int rdlength { get; set; } + public string rdata { get; set; } + public string cname { get; set; } + public string address { get; set; } + } + + public class Debug + { + public bool ping { get; set; } + public bool query { get; set; } + public bool srv { get; set; } + public bool querymismatch { get; set; } + public bool ipinsrv { get; set; } + public bool cnameinsrv { get; set; } + public bool animatedmotd { get; set; } + public bool cachehit { get; set; } + public int cachetime { get; set; } + public int cacheexpire { get; set; } + public int apiversion { get; set; } + public Dns dns { get; set; } + public Error error { get; set; } + } + + public class Dns + { + public Error error { get; set; } + public List a { get; set; } + } + + public class Error + { + public Srv srv { get; set; } + public string query { get; set; } + } + + public class List + { + public string name { get; set; } + public string uuid { get; set; } + } + + public class Motd + { + public List raw { get; set; } + public List clean { get; set; } + public List html { get; set; } + } + + public class Players + { + public int online { get; set; } + public int max { get; set; } + public List list { get; set; } + } + + public class Protocol + { + public int version { get; set; } + public string name { get; set; } + } + + public class ServerResponse + { + public string ip { get; set; } + public int port { get; set; } + public Debug debug { get; set; } + public Motd motd { get; set; } + public Players players { get; set; } + public string version { get; set; } + public bool online { get; set; } + public Protocol protocol { get; set; } + public string hostname { get; set; } + public string icon { get; set; } + public string software { get; set; } + public bool eula_blocked { get; set; } + } + + public class Srv + { + public string hostname { get; set; } + public string message { get; set; } + } + +}