From 60d65eda30665ec5913e4130092462086711291b Mon Sep 17 00:00:00 2001 From: "Robin C. Ladiges" Date: Wed, 23 Oct 2024 03:28:20 +0200 Subject: [PATCH] [discord] flood protection against repeated messages allow one repetition within 20 seconds. b/c of rate limits imposed by the new API --- Server/DiscordBot.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Server/DiscordBot.cs b/Server/DiscordBot.cs index b8b71d4..5daa183 100644 --- a/Server/DiscordBot.cs +++ b/Server/DiscordBot.cs @@ -15,6 +15,10 @@ public class DiscordBot private SocketTextChannel? logChannel = null; private bool firstInitTriggered = false; + private DateTime lastMessageStamp = DateTime.Now; + private string lastMessage = ""; + private bool repeatedMessage = false; + //check how this works with neither, one or the other, or both channels set. public DiscordBot() @@ -158,6 +162,23 @@ private async void LogToDiscordLogChannel(string source, string level, string te logChannel = (ulong.TryParse(localSettings.AdminChannel, out ulong lcid) ? (client != null ? await client.GetChannelAsync(lcid) : null) : null) as SocketTextChannel; if (logChannel != null) { + // prevent logging the same text too often in a short timeframe + DateTime newTime = DateTime.Now; + if (text == lastMessage) + { + if (repeatedMessage && newTime <= lastMessageStamp) + { + return; + } + repeatedMessage = true; + } + else + { + repeatedMessage = false; + } + lastMessage = text; + lastMessageStamp = newTime.Add(TimeSpan.FromSeconds(20)); + try { switch (color)