make it more flexible

This commit is contained in:
13xforever 2020-03-08 18:16:45 +05:00
parent 5ef02db745
commit 055fca02ae

View File

@ -3,8 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Utils;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
@ -12,12 +15,66 @@ namespace CompatBot.Commands
{
internal sealed class Vision: BaseCommandModuleCustom
{
private 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)
);
[Command("describe"), RequiresBotModRole]
[Description("Generates an image description from the attached image, or from the url")]
public async Task Describe(CommandContext ctx)
{
if (GetImageAttachment(ctx.Message).FirstOrDefault() is DiscordAttachment attachment)
await Describe(ctx, attachment.Url).ConfigureAwait(false);
else
await ctx.ReactWithAsync(Config.Reactions.Failure, "No images detected").ConfigureAwait(false);
}
[Command("describe"), RequiresBotModRole]
[Description("Generates an image description")]
public async Task Describe(CommandContext ctx, string imageUrl)
{
try
{
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
var str = imageUrl.ToLowerInvariant();
if ((str.StartsWith("this") || str.StartsWith("that"))
&& ctx.Channel.PermissionsFor(ctx.Client.GetMember(ctx.Guild, ctx.Client.CurrentUser)).HasPermission(Permissions.ReadMessageHistory))
try
{
var previousMessages = await ctx.Channel.GetMessagesBeforeAsync(ctx.Message.Id, 5).ConfigureAwait(false);
imageUrl = (
from m in previousMessages
where m.Attachments?.Count > 0
from a in GetImageAttachment(m)
select a
).FirstOrDefault()?.Url;
if (string.IsNullOrEmpty(imageUrl))
{
imageUrl = (
from m in previousMessages
where m.Embeds?.Count > 0
from e in m.Embeds
let url = e.Image?.Url ?? e.Image?.ProxyUrl ?? e.Thumbnail?.Url ?? e.Thumbnail?.ProxyUrl
select url
).FirstOrDefault()?.ToString();
}
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to ");
}
}
if (string.IsNullOrEmpty(imageUrl) || !Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "No proper image url was found").ConfigureAwait(false);
return;
}
var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Config.AzureComputerVisionKey)) {Endpoint = Config.AzureComputerVisionEndpoint};
var result = await client.AnalyzeImageAsync(imageUrl, new List<VisualFeatureTypes> {VisualFeatureTypes.Description}, cancellationToken: Config.Cts.Token).ConfigureAwait(false);
var captions = result.Description.Captions.OrderByDescending(c => c.Confidence).ToList();
@ -35,10 +92,11 @@ namespace CompatBot.Commands
};
msg = $"{confidence} {captions[0].Text}";
#if DEBUG
msg += $" [{captions[0].Confidence * 100:0.00}%]";
if (captions.Count > 1)
{
msg += "\nHowever, here are more guesses:\n";
msg += string.Join('\n', captions.Skip(1).Select(c => c.Text));
msg += string.Join('\n', captions.Skip(1).Select(c => $"{c.Text} [{c.Confidence*100:0.00}%]"));
}
#endif
}