Пример #1
0
        public async Task ShowExplanation(CommandContext ctx, [RemainingText, Description("Term to explain")] string term)
        {
            var sourceTerm = term;

            if (string.IsNullOrEmpty(term))
            {
                var lastBotMessages = await ctx.Channel.GetMessagesBeforeCachedAsync(ctx.Message.Id, 10).ConfigureAwait(false);

                var showList = true;
                foreach (var pastMsg in lastBotMessages)
                {
                    if (pastMsg.Embeds.FirstOrDefault() is DiscordEmbed pastEmbed &&
                        pastEmbed.Title == TermListTitle ||
                        BotReactionsHandler.NeedToSilence(pastMsg).needToChill)
                    {
                        showList = false;
                        break;
                    }
                }
                if (showList)
                {
                    await List(ctx).ConfigureAwait(false);
                }
                var botMsg = await ctx.RespondAsync("Please tell what term to explain:").ConfigureAwait(false);

                var interact   = ctx.Client.GetInteractivity();
                var newMessage = await interact.WaitForMessageAsync(m => m.Author == ctx.User && m.Channel == ctx.Channel && !string.IsNullOrEmpty(m.Content)).ConfigureAwait(false);

                await botMsg.DeleteAsync().ConfigureAwait(false);

                if (string.IsNullOrEmpty(newMessage.Result?.Content) || newMessage.Result.Content.StartsWith(Config.CommandPrefix))
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);

                    return;
                }

                sourceTerm = term = newMessage.Result.Content;
            }

            if (!await DiscordInviteFilter.CheckMessageForInvitesAsync(ctx.Client, ctx.Message).ConfigureAwait(false))
            {
                return;
            }

            if (!await ContentFilter.IsClean(ctx.Client, ctx.Message).ConfigureAwait(false))
            {
                return;
            }

            term = term.ToLowerInvariant();
            var result = await LookupTerm(term).ConfigureAwait(false);

            if (result.explanation == null || !string.IsNullOrEmpty(result.fuzzyMatch))
            {
                term = term.StripQuotes();
                var idx = term.LastIndexOf(" to ");
                if (idx > 0)
                {
                    var  potentialUserId = term[(idx + 4)..].Trim();
Пример #2
0
        public async Task ShowExplanation(CommandContext ctx, [RemainingText, Description("Term to explain")] string term)
        {
            var sourceTerm = term;

            if (string.IsNullOrEmpty(term))
            {
                var lastBotMessages = await ctx.Channel.GetMessagesBeforeAsync(ctx.Message.Id, 10).ConfigureAwait(false);

                var showList = true;
                foreach (var pastMsg in lastBotMessages)
                {
                    if (pastMsg.Embeds.FirstOrDefault() is DiscordEmbed pastEmbed &&
                        pastEmbed.Title == TermListTitle ||
                        BotReactionsHandler.NeedToSilence(pastMsg).needToChill)
                    {
                        showList = false;
                        break;
                    }
                }
                if (showList)
                {
                    await List(ctx).ConfigureAwait(false);
                }
                var botMsg = await ctx.RespondAsync("Please tell what term to explain:").ConfigureAwait(false);

                var interact   = ctx.Client.GetInteractivity();
                var newMessage = await interact.WaitForMessageAsync(m => m.Author == ctx.User && m.Channel == ctx.Channel && !string.IsNullOrEmpty(m.Content)).ConfigureAwait(false);

                await botMsg.DeleteAsync().ConfigureAwait(false);

                if (string.IsNullOrEmpty(newMessage.Result?.Content) || newMessage.Result.Content.StartsWith(Config.CommandPrefix))
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);

                    return;
                }

                sourceTerm = term = newMessage.Result.Content;
            }

            if (!await DiscordInviteFilter.CheckMessageForInvitesAsync(ctx.Client, ctx.Message).ConfigureAwait(false))
            {
                return;
            }

            if (!await ContentFilter.IsClean(ctx.Client, ctx.Message).ConfigureAwait(false))
            {
                return;
            }

            term = term.ToLowerInvariant();
            var result = await LookupTerm(term).ConfigureAwait(false);

            if (result.explanation == null || !string.IsNullOrEmpty(result.fuzzyMatch))
            {
                term = term.StripQuotes();
                var idx = term.LastIndexOf(" to ");
                if (idx > 0)
                {
                    var  potentialUserId = term.Substring(idx + 4).Trim();
                    bool hasMention      = false;
                    try
                    {
                        var lookup = await((IArgumentConverter <DiscordUser>) new DiscordUserConverter()).ConvertAsync(potentialUserId, ctx).ConfigureAwait(false);
                        hasMention = lookup.HasValue;
                    }
                    catch {}

                    if (hasMention)
                    {
                        term = term.Substring(0, idx).TrimEnd();
                        var mentionResult = await LookupTerm(term).ConfigureAwait(false);

                        if (mentionResult.score > result.score)
                        {
                            result = mentionResult;
                        }
                    }
                }
            }

            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);
                return;
            }

            string inSpecificLocation = null;
            if (!LimitedToSpamChannel.IsSpamChannel(ctx.Channel))
            {
                var spamChannel = await ctx.Client.GetChannelAsync(Config.BotSpamId).ConfigureAwait(false);

                inSpecificLocation = $" in {spamChannel.Mention} or bot DMs";
            }
            var msg = $"Unknown term `{term.Sanitize(replaceBackTicks: true)}`. Use `{ctx.Prefix}explain list` to look at defined terms{inSpecificLocation}";
            await ctx.RespondAsync(msg).ConfigureAwait(false);
        }