diff --git a/CompatBot/Commands/Vision.cs b/CompatBot/Commands/Vision.cs index ee6cd00a..da6376f6 100644 --- a/CompatBot/Commands/Vision.cs +++ b/CompatBot/Commands/Vision.cs @@ -15,12 +15,12 @@ namespace CompatBot.Commands { internal sealed class Vision: BaseCommandModuleCustom { - private static IEnumerable GetImageAttachment(DiscordMessage message) + internal static IEnumerable GetImageAttachment(DiscordMessage message) => message.Attachments.Where(a => a.FileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) || a.FileName.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) || a.FileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) - || a.FileName.EndsWith(".webp", StringComparison.InvariantCultureIgnoreCase) + //|| a.FileName.EndsWith(".webp", StringComparison.InvariantCultureIgnoreCase) ); [Command("describe"), RequiresBotModRole] @@ -34,14 +34,17 @@ namespace CompatBot.Commands } [Command("describe"), RequiresBotModRole] - public async Task Describe(CommandContext ctx, string imageUrl) + public async Task Describe(CommandContext ctx, [RemainingText] string imageUrl) { try { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { var str = imageUrl.ToLowerInvariant(); - if ((str.StartsWith("this") || str.StartsWith("that")) + if ((str.StartsWith("this") + || str.StartsWith("that") + || str.StartsWith("last") + || str.StartsWith("previous")) && ctx.Channel.PermissionsFor(ctx.Client.GetMember(ctx.Guild, ctx.Client.CurrentUser)).HasPermission(Permissions.ReadMessageHistory)) try { diff --git a/CompatBot/EventHandlers/MediaScreenshotMonitor.cs b/CompatBot/EventHandlers/MediaScreenshotMonitor.cs new file mode 100644 index 00000000..d41182c7 --- /dev/null +++ b/CompatBot/EventHandlers/MediaScreenshotMonitor.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using CompatBot.Commands; +using CompatBot.Database; +using CompatBot.Database.Providers; +using CompatBot.Utils; +using CompatBot.Utils.Extensions; +using DSharpPlus.EventArgs; +using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; +using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; + +namespace CompatBot.EventHandlers +{ + internal sealed class MediaScreenshotMonitor + { + private static readonly ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Config.AzureComputerVisionKey)) {Endpoint = Config.AzureComputerVisionEndpoint}; + + public static async Task OnMessageCreated(MessageCreateEventArgs e) + { + var message = e.Message; + if (message == null) + return; + +#if !DEBUG + if (message.Author.IsBotSafeCheck()) + return; +#endif + + if (!message.Attachments.Any()) + return; + + var images = Vision.GetImageAttachment(message).ToList(); + var tasks = new List>(images.Count); + foreach (var img in images) + tasks.Add(client.BatchReadFileAsync(img.Url, Config.Cts.Token)); + foreach (var t in tasks) + { + var headers = await t.ConfigureAwait(false); + Config.Log.Trace($"Read result location url: {headers.OperationLocation}"); + ReadOperationResult result; + do + { + result = await client.GetReadOperationResultAsync(new Uri(headers.OperationLocation).Segments.Last(), Config.Cts.Token).ConfigureAwait(false); + if (result.Status == TextOperationStatusCodes.Succeeded) + { + foreach (var r in result.RecognitionResults) + foreach (var l in r.Lines) + { + Config.Log.Debug($"{message.Id} text: {l.Text}"); + if (await ContentFilter.FindTriggerAsync(FilterContext.Log, l.Text).ConfigureAwait(false) is Piracystring hit) + { + await e.Client.ReportAsync("🖼 Screenshot of a pirated game", message, hit.String, l.Text, ReportSeverity.Medium).ConfigureAwait(false); + } + } + } + } while (result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted); + } + } + } +} diff --git a/CompatBot/Program.cs b/CompatBot/Program.cs index 72433666..ac5410b4 100644 --- a/CompatBot/Program.cs +++ b/CompatBot/Program.cs @@ -212,6 +212,8 @@ namespace CompatBot client.MessageCreated += _ => { Watchdog.TimeSinceLastIncomingMessage.Restart(); return Task.CompletedTask;}; client.MessageCreated += ContentFilterMonitor.OnMessageCreated; // should be first + if (!string.IsNullOrEmpty(Config.AzureComputerVisionKey)) + client.MessageCreated += MediaScreenshotMonitor.OnMessageCreated; client.MessageCreated += ProductCodeLookup.OnMessageCreated; client.MessageCreated += LogParsingHandler.OnMessageCreated; client.MessageCreated += LogAsTextMonitor.OnMessageCreated;