Fix pinging system in DiscordWebhook.cs (#1610)

* Check if ping already exists to avoid crashes.
* Remove bug where multiple words are not pinged
This commit is contained in:
Daenges 2021-05-31 18:54:04 +02:00 committed by GitHub
parent f16e9e87dd
commit 5281bc140c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -406,11 +406,11 @@ class DiscordWebhook : ChatBot
string pings = ""; string pings = "";
if (settings.GetMessageContains().Count > 0) 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<string, string> 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())) if (settings.GetMessageFrom().ContainsKey(username.ToLower()))
@ -603,15 +603,21 @@ class DiscordWebhook : ChatBot
{ {
List<string> tempList = GetStringsInQuotes(string.Join(" ", args)); List<string> tempList = GetStringsInQuotes(string.Join(" ", args));
if (tempList.Count >= 2) if (tempList.Count >= 2)
{
if (!settings.GetMessageContains().ContainsKey(tempList[0].ToLower()))
{ {
settings.GetMessageContains().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); settings.GetMessageContains().Add(tempList[0].ToLower(), string.Join(" ", tempList[1]));
return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]);
} }
else else
{
return "This ping already exists";
}
}
else
{ {
return "Too many arguments"; return "Too many arguments";
} }
} }
else else
{ {
@ -633,15 +639,21 @@ class DiscordWebhook : ChatBot
{ {
List<string> tempList = GetStringsInQuotes(string.Join(" ", args)); List<string> tempList = GetStringsInQuotes(string.Join(" ", args));
if (tempList.Count >= 2) if (tempList.Count >= 2)
{
if (!settings.GetMessageFrom().ContainsKey(tempList[0].ToLower()))
{ {
settings.GetMessageFrom().Add(tempList[0].ToLower(), string.Join(" ", tempList[1])); settings.GetMessageFrom().Add(tempList[0].ToLower(), string.Join(" ", tempList[1]));
return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]); return "Added " + tempList[0].ToLower() + " " + string.Join(" ", tempList[1]);
} }
else else
{
return "This ping already exists";
}
}
else
{ {
return "Too many arguments"; return "Too many arguments";
} }
} }
else else
{ {