Improvements for Discord Webhook (#1544)

* Improve caching and remove unused functions
* Save Player UUID before leaving
Save the UUID when they leave and save it until they join again.
* Clear the cache after disconnect
* Update MinecraftClient/config/ChatBots/DiscordWebhook.cs
This commit is contained in:
Daenges 2021-04-18 16:14:09 +02:00 committed by GitHub
parent 92eb4e1457
commit 239900451d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,6 +22,7 @@ class WebhoookSettings
private Dictionary<string, List<string>> messageCache = new Dictionary<string, List<string>>(); private Dictionary<string, List<string>> messageCache = new Dictionary<string, List<string>>();
private Dictionary<string, string> messageContains = new Dictionary<string, string>(); private Dictionary<string, string> messageContains = new Dictionary<string, string>();
private Dictionary<string, string> messageFrom = new Dictionary<string, string>(); private Dictionary<string, string> messageFrom = new Dictionary<string, string>();
private Dictionary<string, string> namesToUuidMojangCache = new Dictionary<string, string>();
private List<string> ignoredPlayers = new List<string>(); private List<string> ignoredPlayers = new List<string>();
#endregion #endregion
@ -96,16 +97,11 @@ class WebhoookSettings
} }
public Dictionary<string, string> GetMessageContains() { return this.messageContains; } public Dictionary<string, string> GetMessageContains() { return this.messageContains; }
public void SetMessageContains(Dictionary<string, string> value) { this.messageContains = value; }
public Dictionary<string, string> GetMessageFrom() { return this.messageFrom; } public Dictionary<string, string> GetMessageFrom() { return this.messageFrom; }
public void SetMessageFrom(Dictionary<string, string> value) { this.messageFrom = value; }
public Dictionary<string, List<string>> GetCachedMessages() { return this.messageCache; } public Dictionary<string, List<string>> GetCachedMessages() { return this.messageCache; }
public void SetCachedMessages(Dictionary<string, List<string>> value) { this.messageCache = value; }
public Dictionary<string, string> GetSkinModes() { return this.skinModes; } public Dictionary<string, string> GetSkinModes() { return this.skinModes; }
public Dictionary<string, string> GetNamesToUuidMojangCache() { return this.namesToUuidMojangCache; }
public void resetUUIDCache() { namesToUuidMojangCache.Clear(); }
public List<string> GetIgnoredPlayers() { return ignoredPlayers; } public List<string> GetIgnoredPlayers() { return ignoredPlayers; }
} }
@ -126,12 +122,19 @@ class SkinAPI
/// <returns></returns> /// <returns></returns>
public string GetUUIDFromMojang(string name) public string GetUUIDFromMojang(string name)
{ {
WebClient wc = new WebClient(); if (settings.GetNamesToUuidMojangCache().ContainsKey(name))
try return settings.GetNamesToUuidMojangCache()[name];
using (WebClient wc = new WebClient())
{ {
return Json.ParseJson(wc.DownloadString("https://api.mojang.com/users/profiles/minecraft/" + name)).Properties["id"].StringValue; try
{
string uuid = Json.ParseJson(wc.DownloadString("https://api.mojang.com/users/profiles/minecraft/" + name)).Properties["id"].StringValue;
settings.GetNamesToUuidMojangCache().Add(name, uuid);
return uuid;
}
catch (Exception) { return "00000000000000000000000000000000"; }
} }
catch (Exception) { return "00000000000000000000000000000000"; }
} }
/// <summary> /// <summary>
@ -311,6 +314,28 @@ class DiscordWebhook : ChatBot
} }
} }
public override void OnPlayerJoin(Guid uuid, string name)
{
if (settings.GetNamesToUuidMojangCache().ContainsKey(name))
{
settings.GetNamesToUuidMojangCache().Remove(name);
}
}
public override void OnPlayerLeave(Guid uuid, string name)
{
if (!settings.GetNamesToUuidMojangCache().ContainsKey(name))
{
settings.GetNamesToUuidMojangCache().Add(name, uuid.ToString());
}
}
public override bool OnDisconnect(DisconnectReason reason, string message)
{
settings.resetUUIDCache();
return false;
}
public override void GetText(string text) public override void GetText(string text)
{ {
if (settings.Togglesending) if (settings.Togglesending)
@ -730,3 +755,4 @@ class DiscordWebhook : ChatBot
else { return GetHelp(); } else { return GetHelp(); }
} }
} }
}