diff --git a/CompatBot/Commands/Explain.cs b/CompatBot/Commands/Explain.cs index ff6f0bf6..aa9d6fc8 100644 --- a/CompatBot/Commands/Explain.cs +++ b/CompatBot/Commands/Explain.cs @@ -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 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; diff --git a/CompatBot/Database/Providers/ContentFilter.cs b/CompatBot/Database/Providers/ContentFilter.cs index a7be7c3a..66db6f89 100644 --- a/CompatBot/Database/Providers/ContentFilter.cs +++ b/CompatBot/Database/Providers/ContentFilter.cs @@ -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))) { diff --git a/CompatBot/EventHandlers/LogParsingHandler.cs b/CompatBot/EventHandlers/LogParsingHandler.cs index 63824c94..16c8cd18 100644 --- a/CompatBot/EventHandlers/LogParsingHandler.cs +++ b/CompatBot/EventHandlers/LogParsingHandler.cs @@ -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)