From 5c9e62613b259a7f1d5027992d9a985fe053f8c0 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Wed, 28 Jan 2026 21:55:58 +0500 Subject: [PATCH] try to match spam messages between different users to further reduce ocr lag --- .../EventHandlers/MediaScreenshotMonitor.cs | 58 +++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/CompatBot/EventHandlers/MediaScreenshotMonitor.cs b/CompatBot/EventHandlers/MediaScreenshotMonitor.cs index 7a6d9e79..22a89702 100644 --- a/CompatBot/EventHandlers/MediaScreenshotMonitor.cs +++ b/CompatBot/EventHandlers/MediaScreenshotMonitor.cs @@ -1,11 +1,11 @@ -using System.Collections.Concurrent; -using CompatApiClient.Utils; +using CompatApiClient.Utils; using CompatBot.Commands; using CompatBot.Database; using CompatBot.Database.Providers; using CompatBot.Ocr; using CompatBot.Utils.Extensions; using Microsoft.Extensions.Caching.Memory; +using System.Collections.Concurrent; namespace CompatBot.EventHandlers; @@ -13,8 +13,8 @@ internal sealed class MediaScreenshotMonitor { private static readonly SemaphoreSlim WorkSemaphore = new(0); private static readonly ConcurrentQueue<(DiscordMessage msg, string imgUrl)> WorkQueue = new(); - private static readonly MemoryCache RemovedMessages = new(new MemoryCacheOptions() { ExpirationScanFrequency = TimeSpan.FromMinutes(5) }); - private static readonly TimeSpan CachedTime = TimeSpan.FromMinutes(1); + private static readonly MemoryCache RemovedMessages = new(new MemoryCacheOptions() { ExpirationScanFrequency = TimeSpan.FromMinutes(10) }); + private static readonly TimeSpan CachedTime = TimeSpan.FromMinutes(5); public DiscordClient Client { get; internal set; } = null!; public static int MaxQueueLength { get; private set; } @@ -82,16 +82,40 @@ internal sealed class MediaScreenshotMonitor try { - if (await OcrProvider.GetTextAsync(item.imgUrl, Config.Cts.Token).ConfigureAwait(false) is ({Length: >0} result, var confidence) - && !Config.Cts.Token.IsCancellationRequested) + var signature = GetSignature(item.msg); + var prefix = $"[{item.msg.Id % 100:00}]"; + if (RemovedMessages.TryGetValue(signature, out (Piracystring hit, DiscordMessage msg) previousItem)) + { + Config.Log.Debug($"{prefix} OCR result of message {item.msg.JumpLink} from user {item.msg.Author?.Username} ({item.msg.Author?.Id}):"); + var ocrTextBuf = new StringBuilder($"OCR result of message <{item.msg.JumpLink}>:").AppendLine(); + FilterAction suppressFlags = 0; + if ("media".Equals(item.msg.Channel?.Name)) + { + suppressFlags = FilterAction.SendMessage | FilterAction.ShowExplain; + } + await ContentFilter.PerformFilterActions( + Client, + item.msg, + previousItem.hit, + suppressFlags, + $"Matched to previously removed message from {previousItem.msg.Author?.Mention}: {previousItem.msg.JumpLink}", + "🖼 Screenshot of an undesirable content", + "Screenshot of an undesirable content" + ).ConfigureAwait(false); + if (previousItem.hit.Actions.HasFlag(FilterAction.RemoveContent)) + RemovedMessages.Set(item.msg.Id, true, CachedTime); + } + else if (await OcrProvider.GetTextAsync(item.imgUrl, Config.Cts.Token).ConfigureAwait(false) is ({ Length: > 0 } result, var confidence)) { var cnt = true; - var prefix = $"[{item.msg.Id % 100:00}]"; - var ocrTextBuf = new StringBuilder($"OCR result of message <{item.msg.JumpLink}> ({confidence*100:0.00}%):").AppendLine(); - Config.Log.Debug($"{prefix} OCR result of message {item.msg.JumpLink} from user {item.msg.Author?.Username} ({item.msg.Author?.Id}) ({confidence*100:0.00}%):"); var duplicates = new HashSet(); - ocrTextBuf.AppendLine(result.Sanitize()); - Config.Log.Debug($"{prefix} {result}"); + Config.Log.Debug($""" + {prefix} OCR result of message {item.msg.JumpLink} from user {item.msg.Author?.Username} ({item.msg.Author?.Id}) ({confidence * 100:0.00}%): + {result} + """ + ); + var ocrTextBuf = new StringBuilder($"OCR result of message <{item.msg.JumpLink}> ({confidence * 100:0.00}%):").AppendLine() + .AppendLine(result.Sanitize()); if (cnt && confidence > 0.65 && await ContentFilter.FindTriggerAsync(FilterContext.Chat, result).ConfigureAwait(false) is Piracystring hit @@ -112,7 +136,11 @@ internal sealed class MediaScreenshotMonitor "Screenshot of an undesirable content" ).ConfigureAwait(false); if (hit.Actions.HasFlag(FilterAction.RemoveContent)) + { RemovedMessages.Set(item.msg.Id, true, CachedTime); + if (signature is { Length: >0}) + RemovedMessages.Set(signature, (hit, item.msg), CachedTime); + } cnt &= !hit.Actions.HasFlag(FilterAction.RemoveContent) && !hit.Actions.HasFlag(FilterAction.IssueWarning); } var ocrText = ocrTextBuf.ToString(); @@ -138,4 +166,12 @@ internal sealed class MediaScreenshotMonitor } } while (!Config.Cts.IsCancellationRequested); } + + private static string GetSignature(DiscordMessage msg) + { + var result = msg.Content ?? ""; + foreach (var att in msg.Attachments) + result += $"📎 {att.FileName} ({att.FileSize})\n"; + return result.TrimEnd(); + } } \ No newline at end of file