add anti-spam handler that removes the same message in multiple channels

This commit is contained in:
13xforever
2025-07-17 18:47:04 +05:00
parent 9fbd917829
commit 00853aa3a3
2 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
using CompatBot.Utils.Extensions;
using Microsoft.Extensions.Caching.Memory;
namespace CompatBot.EventHandlers;
public static class AntiSpamMessageHandler
{
private static readonly MemoryCache MessageCache = new(new MemoryCacheOptions{ ExpirationScanFrequency = TimeSpan.FromMinutes(1) });
private static readonly TimeSpan DefaultExpiration = TimeSpan.FromSeconds(10);
public static async Task<bool> OnMessageCreated(DiscordClient client, MessageCreatedEventArgs args)
{
var author = args.Author;
if (author.IsBotSafeCheck())
return true;
#if !DEBUG
if (await author.IsSmartlistedAsync(client, args.Guild).ConfigureAwait(false)
|| await author.IsWhitelistedAsync(client, args.Guild).ConfigureAwait(false))
return true;
#endif
var msg = args.Message;
if (msg.Content is not { Length: > 0 })
return true;
if (MessageCache.TryGetValue(author.Id, out var item)
&& item is (DiscordMessage { Content.Length: >0 } lastMessage, bool isWarned)
&& lastMessage.Content == msg.Content
&& lastMessage.ChannelId != msg.ChannelId)
{
var removedSpam = false;
try
{
await msg.DeleteAsync("spam").ConfigureAwait(false);
removedSpam = true;
}
catch (Exception e)
{
Config.Log.Warn(e, $"Faled to delete spam message from user {author.Username} ({author.Id}) in #{msg.Channel?.Name} {msg.JumpLink}");
}
try
{
if (!isWarned)
{
await author.SendMessageAsync("Please do not spam the same message in multiple channels. Thank you.").ConfigureAwait(false);
isWarned = true;
}
}
catch (Exception e)
{
Config.Log.Warn(e, $"Faled to send DM to user {author.Username} ({author.Id})");
}
MessageCache.Set(author.Id, (lastMessage, isWarned), DefaultExpiration);
return !removedSpam; // couldn't remove, need to check with filters etc
}
MessageCache.Set(author.Id, (msg, false), DefaultExpiration);
return true;
}
}

View File

@@ -236,6 +236,7 @@ internal static class Program
]));
config.HandleMessageCreated(new MultiEventHandlerWrapper<MessageCreatedEventArgs>(
[
AntiSpamMessageHandler.OnMessageCreated,
ContentFilterMonitor.OnMessageCreated, // should be first
DiscordInviteFilter.OnMessageCreated,
],