prototype of an image moderation handler

This commit is contained in:
13xforever 2020-03-08 20:36:31 +05:00
parent 055fca02ae
commit 86fce35c35
3 changed files with 71 additions and 4 deletions

View File

@ -15,12 +15,12 @@ namespace CompatBot.Commands
{
internal sealed class Vision: BaseCommandModuleCustom
{
private static IEnumerable<DiscordAttachment> GetImageAttachment(DiscordMessage message)
internal static IEnumerable<DiscordAttachment> 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
{

View File

@ -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<Task<BatchReadFileHeaders>>(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);
}
}
}
}

View File

@ -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;