From 5281bc140cbd31b29ee7544dafa6675c36b89d07 Mon Sep 17 00:00:00 2001 From: Daenges <57369924+Daenges@users.noreply.github.com> Date: Mon, 31 May 2021 18:54:04 +0200 Subject: [PATCH] Fix pinging system in DiscordWebhook.cs (#1610) * Check if ping already exists to avoid crashes. * Remove bug where multiple words are not pinged --- .../config/ChatBots/DiscordWebhook.cs | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/MinecraftClient/config/ChatBots/DiscordWebhook.cs b/MinecraftClient/config/ChatBots/DiscordWebhook.cs index a9c88da8..5b2aac58 100644 --- a/MinecraftClient/config/ChatBots/DiscordWebhook.cs +++ b/MinecraftClient/config/ChatBots/DiscordWebhook.cs @@ -406,11 +406,11 @@ class DiscordWebhook : ChatBot string pings = ""; if (settings.GetMessageContains().Count > 0) { - msg = new string(msg.Where(c => !char.IsPunctuation(c)).ToArray()); + msg = new string(msg.Where(c => !char.IsPunctuation(c)).ToArray()).ToLower(); - foreach (string word in msg.Split(' ')) + foreach (KeyValuePair keyPhrase in settings.GetMessageContains()) { - if (settings.GetMessageContains().ContainsKey(word.ToLower())) { pings += string.Join(" ", settings.GetMessageContains()[word.ToLower()]); } + if (msg.Contains(keyPhrase.Key)) { pings += string.Join(" ", keyPhrase.Value); } } } if (settings.GetMessageFrom().ContainsKey(username.ToLower())) @@ -604,14 +604,20 @@ class DiscordWebhook : ChatBot List tempList = GetStringsInQuotes(string.Join(" ", args)); if (tempList.Count >= 2) { - settings.GetMessageContains().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); - return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); + if (!settings.GetMessageContains().ContainsKey(tempList[0].ToLower())) + { + settings.GetMessageContains().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); + return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); + } + else + { + return "This ping already exists"; + } } else { return "Too many arguments"; } - } else { @@ -634,14 +640,20 @@ class DiscordWebhook : ChatBot List tempList = GetStringsInQuotes(string.Join(" ", args)); if (tempList.Count >= 2) { - settings.GetMessageFrom().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); - return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); + if (!settings.GetMessageFrom().ContainsKey(tempList[0].ToLower())) + { + settings.GetMessageFrom().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); + return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); + } + else + { + return "This ping already exists"; + } } else { return "Too many arguments"; } - } else {