Skip to content

Commit

Permalink
[discord] flood protection against repeated messages
Browse files Browse the repository at this point in the history
allow one repetition within 20 seconds.

b/c of rate limits imposed by the new API
  • Loading branch information
Istador committed Oct 23, 2024
1 parent ad0fc12 commit 60d65ed
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Server/DiscordBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 60d65ed

Please sign in to comment.