Tidy up /location code

This commit is contained in:
UnknownShadow200 2021-06-12 08:09:16 +10:00
parent 7cf4748fb3
commit 2bf9593bbf
2 changed files with 14 additions and 10 deletions

View File

@ -43,19 +43,23 @@ namespace MCGalaxy.Commands.Moderation {
p.Message("&WPlayer has an internal IP, cannot trace"); return; p.Message("&WPlayer has an internal IP, cannot trace"); return;
} }
string json, region = null, country = null; string json;
using (WebClient client = HttpUtil.CreateWebClient()) { try {
json = client.DownloadString("http://ipinfo.io/" + ip + "/geo"); WebRequest req = HttpUtil.CreateRequest("http://ipinfo.io/" + ip + "/geo");
WebResponse res = req.GetResponse();
json = HttpUtil.GetResponseText(res);
} catch (Exception ex) {
HttpUtil.DisposeErrorResponse(ex);
throw;
} }
JsonReader reader = new JsonReader(json); JsonReader reader = new JsonReader(json);
reader.OnMember = (obj, key, value) => { JsonObject obj = (JsonObject)reader.Parse();
if (key == "region") region = (string)value; if (obj == null) { p.Message("&WError parsing GeoIP info"); return; }
if (key == "country") country = (string)value;
};
reader.Parse(); object region = null, country = null;
if (reader.Failed) { p.Message("&WError parsing GeoIP info"); return; } obj.TryGetValue("region", out region);
obj.TryGetValue("country", out country);
string suffix = HasExtraPerm(p, data.Rank, 1) ? "&b{1}&S/&b{2}" : "&b{2}"; string suffix = HasExtraPerm(p, data.Rank, 1) ? "&b{1}&S/&b{2}" : "&b{2}";
string nick = name == null ? ip : "of " + p.FormatNick(name); string nick = name == null ? ip : "of " + p.FormatNick(name);