mirror of
https://github.com/RPCS3/discord-bot.git
synced 2026-01-31 01:25:22 +01:00
try to match spam messages between different users
to further reduce ocr lag
This commit is contained in:
@@ -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<string>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user