Skip to content

Instantly share code, notes, and snippets.

@123DMWM
Last active March 6, 2023 15:33
Show Gist options
  • Save 123DMWM/57103f407be3ea7bfc23a25b85e85792 to your computer and use it in GitHub Desktop.
Save 123DMWM/57103f407be3ea7bfc23a25b85e85792 to your computer and use it in GitHub Desktop.
MCGalaxy custom command for accessing the ClassiCube Player API lookup.
//reference System.dll
//reference ServiceStack.Text.dll
using System;
using System.Text.RegularExpressions;
using System.Net;
using MCGalaxy.Network;
namespace MCGalaxy {
public class CmdCcapi : Command {
public override string name { get { return "ccapi"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return "other"; } }
public override bool museumUsable { get { return true; } }
public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
public override void Use(Player p, string message) {
string userSearch = message != "" ? message : p.truename.TrimEnd('+');
if (!new Regex(@"^([a-zA-Z0-9._]{2,16}|[a-zA-Z0-9._]{1,15}@\d*)$", RegexOptions.Compiled).IsMatch(userSearch)) {
p.Message("{0} is not a valid player name!", userSearch);
return;
}
string data = "";
using (WebClient client = HttpUtil.CreateWebClient()) {
try {
data = client.DownloadString("https://www.classicube.net/api/player/" + userSearch);
} catch (WebException e) {
if (e.Message.CaselessContains("too many requests")) {
p.Message("&4Slow Down!");
return;
}
} catch {
p.Message("&4Error!");
return;
}
}
if (string.IsNullOrEmpty(data) || !data.Contains("forum_title")) {
p.Message(userSearch == ".." ? "&WSorry, but that is one of the only names that can't be looked up." : "Error retrieving data!");
return;
}
ServiceStack.Text.JsonObject result = ServiceStack.Text.JsonObject.Parse(data);
if (result == null) {
p.Message("&WError parsing CCAPI info");
return;
}
string error = result["error"];
if (!string.IsNullOrWhiteSpace(error)) {
if (error.ToLower().Equals("user not found")) {
p.Message("&WUser not found!");
} else {
p.Message("&WError occured: " + error);
}
return;
}
string username = result["username"];
string flags = result["flags"], flagSTR = "&F";
string[] flagchar = Array.Empty<string>();
if (flags != "[]") {
flagchar = flags.Trim('[', ']').Split(',');
foreach (string c in flagchar) {
switch (c) {
case "a": flagSTR += "Website Admin, "; break;
case "b": flagSTR += "&4Banned from old forums&F, "; break;
case "d": flagSTR += "ClassiCube Developer, "; break;
case "e": flagSTR += "ClassiCube Blog Editor, "; break;
case "m": flagSTR += "Forum Moderator, "; break;
case "p": flagSTR += "&6ClassiCube Patron&F, "; break;
case "r": flagSTR += "Recovering Account, "; break;
case "u": flagSTR += "Unverified, "; break;
default: break;
}
}
}
bool premium = bool.Parse(result["premium"]);
DateTime registered = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(double.Parse(result["registered"]));
p.Message("&EAPI info about user {1}&F{0}&E with ID:&F{2}", username, premium ? "&2☼" : "", result["id"]);
p.Message(" &ERegistered[&F{0}&E]", result["registered"]);
p.Message(" &F{0}&E at &F{1}&E UTC", registered.ToLongDateString(), registered.ToLongTimeString());
p.Message(" &F{0} ago.", TimeSpanToString(DateTime.UtcNow - registered));
if (flags != "[]") {
p.Message(" &EFlags[&F{0}&E]", flagchar.Join(","));
p.Message(" " + flagSTR.TrimEnd(',', ' '));
}
if (result["forum_title"] != "") {
p.Message(" &EForum Title");
p.Message(" &F" + result["forum_title"]);
}
}
public static string TimeSpanToString(TimeSpan value, bool longer = true) {
string time = "";
bool negate = value.TotalSeconds < 0;
if (negate) value = -value;
//Add(ref time, value.Days / 7, " week");
Add(ref time, value.Days, "d" + (longer ? "ay" : ""), longer);// % 7, " day");
Add(ref time, value.Hours, "h" + (longer ? "our" : ""), longer);
Add(ref time, value.Minutes, "m" + (longer ? "inute" : ""), longer);
Add(ref time, value.Seconds, "s" + (longer ? "econd" : ""), longer);
if (string.IsNullOrWhiteSpace(time)) time = "right now";
time = time.TrimEnd(',');
if (longer && time.Contains(',')) {
string beforeand = time.Substring(0, time.LastIndexOf(','));
string afterand = time.Substring(time.LastIndexOf(',')).TrimStart(',');
time = beforeand + ", and" + afterand;
}
return negate ? "-" + time : time;
}
public static void Add(ref string time, int amount, string suffix, bool longer) {
if (amount == 0) return;
time = (time.Length == 0 ? "" : time + " ") + amount.ToString("N0") + (longer ? " " : "") + suffix + (amount != 1 && longer ? "s," : longer ? "," : "");
}
public override void Help(Player p) {
p.Message("/ccapi <username> - Shows ClassiCube Account information.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment