Fixed and tested Discord Webhook chat bot

Fixed and tested Discord Webhook chat bot.
Now using ProxiedWebRequest and sanitizing input.
This commit is contained in:
Anon 2022-09-24 11:56:11 +00:00 committed by GitHub
commit ef79ca1fe8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
//MCCScript 1.0 //MCCScript 1.0
//using System.Collections.Specialized; //using System.Collections.Specialized;
//using MinecraftClient.Protocol;
MCC.LoadBot(new DiscordWebhook()); MCC.LoadBot(new DiscordWebhook());
@ -126,17 +127,17 @@ class SkinAPI
if (settings.GetNamesToUuidMojangCache().ContainsKey(name)) if (settings.GetNamesToUuidMojangCache().ContainsKey(name))
return settings.GetNamesToUuidMojangCache()[name]; return settings.GetNamesToUuidMojangCache()[name];
using (WebClient wc = new WebClient())
{
try try
{ {
string uuid = Json.ParseJson(wc.DownloadString("https://api.mojang.com/users/profiles/minecraft/" + name)).Properties["id"].StringValue; var request = new ProxiedWebRequest("https://api.mojang.com/users/profiles/minecraft/" + name);
request.Accept = "application/json";
var response = request.Get();
string uuid = Json.ParseJson(response.Body).Properties["id"].StringValue;
settings.GetNamesToUuidMojangCache().Add(name, uuid); settings.GetNamesToUuidMojangCache().Add(name, uuid);
return uuid; return uuid;
} }
catch (Exception) { return "00000000000000000000000000000000"; } catch (Exception) { return "00000000000000000000000000000000"; }
} }
}
/// <summary> /// <summary>
/// Gets the UUID from the internal bot command, which is faster and should be safer on servers with titles. /// Gets the UUID from the internal bot command, which is faster and should be safer on servers with titles.
@ -275,23 +276,6 @@ class MessageCache
} }
} }
class HTTP
{
public static byte[] Post(string url, NameValueCollection pairs)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
using (WebClient webClient = new WebClient())
{
return webClient.UploadValues(url, pairs);
}
}
}
class DiscordWebhook : ChatBot class DiscordWebhook : ChatBot
{ {
private WebhoookSettings settings = new WebhoookSettings(); private WebhoookSettings settings = new WebhoookSettings();
@ -442,6 +426,19 @@ class DiscordWebhook : ChatBot
return pings; return pings;
} }
public string Sanizize(string input)
{
return string.Join(" ",
input.Replace("\"", "'")
.Replace("\n", "\\n")
.Replace("{", "")
.Replace("}", "")
.Trim()
.Split(' ')
.Where(s => !string.IsNullOrWhiteSpace(s)))
.Replace("\\n ", "\\n");
}
/// <summary> /// <summary>
/// Sends the message to the discord webhook. /// Sends the message to the discord webhook.
/// </summary> /// </summary>
@ -455,22 +452,15 @@ class DiscordWebhook : ChatBot
LogDebugToConsole("Send webhook request to Discord."); LogDebugToConsole("Send webhook request to Discord.");
try try
{ {
HTTP.Post(settings.WebhookURL, new NameValueCollection() StringBuilder requestData = new StringBuilder();
{
{ requestData.Append("{");
"username", requestData.Append("\"username\": \"" + msg.SenderName + "\", ");
msg.SenderName requestData.Append("\"content\": \"" + Sanizize(msg.Content) + "\", ");
}, requestData.Append("\"avatar_url\": \"" + (msg.SenderName == "[Server]" ? sAPI.GetSkinURLCrafatar("f78a4d8dd51b4b3998a3230f2de0c670") : sAPI.GetSkinURLCrafatar(msg.SenderUUID)) + "\"");
{ requestData.Append("}");
"content",
msg.Content PostRequest(settings.WebhookURL, "application/json", requestData.ToString());
},
{
"avatar_url",
msg.SenderName == "[Server]" ? sAPI.GetSkinURLCrafatar("f78a4d8dd51b4b3998a3230f2de0c670") : sAPI.GetSkinURLCrafatar(msg.SenderUUID)
}
}
);
} }
catch (Exception e) catch (Exception e)
{ {
@ -529,6 +519,14 @@ class DiscordWebhook : ChatBot
return result; return result;
} }
public string PostRequest(string url, string contentType, string content)
{
var request = new ProxiedWebRequest(url);
request.Accept = contentType;
var response = request.Post(contentType, content);
return response.Body;
}
/// <summary> /// <summary>
/// Handles all commands. /// Handles all commands.
/// </summary> /// </summary>