add support for FilterAction.ShowExplanation

This commit is contained in:
13xforever 2020-03-09 23:20:57 +05:00
parent 5072df14f5
commit 0a9774e89e
3 changed files with 43 additions and 26 deletions

View File

@ -92,31 +92,8 @@ namespace CompatBot.Commands
}
}
try
{
if (result.explanation != null && result.score > 0.5)
{
if (!string.IsNullOrEmpty(result.fuzzyMatch))
{
var fuzzyNotice = $"Showing explanation for `{result.fuzzyMatch}`:";
#if DEBUG
fuzzyNotice = $"Showing explanation for `{result.fuzzyMatch}` ({result.score:0.######}):";
#endif
await ctx.RespondAsync(fuzzyNotice).ConfigureAwait(false);
}
var explain = result.explanation;
StatsStorage.ExplainStatCache.TryGetValue(explain.Keyword, out int stat);
StatsStorage.ExplainStatCache.Set(explain.Keyword, ++stat, StatsStorage.CacheTime);
await ctx.Channel.SendMessageAsync(explain.Text, explain.Attachment, explain.AttachmentFilename).ConfigureAwait(false);
return;
}
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to explain " + sourceTerm);
if (await SendExplanation(result, term, ctx.Message).ConfigureAwait(false))
return;
}
string inSpecificLocation = null;
if (!LimitedToSpamChannel.IsSpamChannel(ctx.Channel))
@ -401,6 +378,36 @@ namespace CompatBot.Commands
return (explanation, fuzzyMatch, coefficient);
}
internal static async Task<bool> SendExplanation((Explanation explanation, string fuzzyMatch, double score) termLookupResult, string term, DiscordMessage sourceMessage)
{
try
{
if (termLookupResult.explanation != null && termLookupResult.score > 0.5)
{
if (!string.IsNullOrEmpty(termLookupResult.fuzzyMatch))
{
var fuzzyNotice = $"Showing explanation for `{termLookupResult.fuzzyMatch}`:";
#if DEBUG
fuzzyNotice = $"Showing explanation for `{termLookupResult.fuzzyMatch}` ({termLookupResult.score:0.######}):";
#endif
await sourceMessage.Channel.SendMessageAsync(fuzzyNotice).ConfigureAwait(false);
}
var explain = termLookupResult.explanation;
StatsStorage.ExplainStatCache.TryGetValue(explain.Keyword, out int stat);
StatsStorage.ExplainStatCache.Set(explain.Keyword, ++stat, StatsStorage.CacheTime);
await sourceMessage.Channel.SendMessageAsync(explain.Text, explain.Attachment, explain.AttachmentFilename).ConfigureAwait(false);
return true;
}
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to explain " + term);
return true;
}
return false;
}
private static async Task DumpLink(CommandContext ctx, string messageLink)
{
string explanation = null;

View File

@ -11,6 +11,7 @@ using DSharpPlus.Entities;
using HomoglyphConverter;
using Microsoft.EntityFrameworkCore;
using NReco.Text;
using Microsoft.Extensions.Caching.Memory;
namespace CompatBot.Database.Providers
{
@ -120,7 +121,7 @@ namespace CompatBot.Database.Providers
return true;
await PerformFilterActions(client, message, trigger).ConfigureAwait(false);
return false;
return (trigger.Actions & (FilterAction.IssueWarning | FilterAction.RemoveContent)) == 0;
}
public static async Task PerformFilterActions(DiscordClient client, DiscordMessage message, Piracystring trigger, FilterAction ignoreFlags = 0, string triggerContext = null, string infraction = null, string warningReason = null)
@ -185,6 +186,12 @@ namespace CompatBot.Database.Providers
}
}
if (trigger.Actions.HasFlag(FilterAction.ShowExplain) && !ignoreFlags.HasFlag(FilterAction.ShowExplain))
{
var result = await Explain.LookupTerm(trigger.ExplainTerm).ConfigureAwait(false);
await Explain.SendExplanation(result, trigger.ExplainTerm, message).ConfigureAwait(false);
}
var actionList = "";
foreach (FilterAction fa in Enum.GetValues(typeof(FilterAction)))
{

View File

@ -182,7 +182,10 @@ namespace CompatBot.EventHandlers
else
{
if (result.SelectedFilter != null)
await ContentFilter.PerformFilterActions(client, message, result.SelectedFilter, FilterAction.IssueWarning | FilterAction.SendMessage, result.SelectedFilterContext).ConfigureAwait(false);
{
var ignoreFlags = FilterAction.IssueWarning | FilterAction.SendMessage | FilterAction.ShowExplain;
await ContentFilter.PerformFilterActions(client, message, result.SelectedFilter, ignoreFlags, result.SelectedFilterContext).ConfigureAwait(false);
}
botMsg = await botMsg.UpdateOrCreateMessageAsync(channel,
requester == null ? null : $"Analyzed log from {client.GetMember(channel.Guild, message.Author)?.GetUsernameWithNickname()} by request from {requester.Mention}:",
embed: await result.AsEmbedAsync(client, message, source).ConfigureAwait(false)