Ejemplo n.º 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();
Ejemplo n.º 2
0
        public async Task Compat(CommandContext ctx, [RemainingText, Description("Game title to look up")] string title)
        {
            title = title?.TrimEager().Truncate(40);
            if (string.IsNullOrEmpty(title))
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "You should specify what you're looking for").ConfigureAwait(false);

                return;
            }

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

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

            try
            {
                var requestBuilder = RequestBuilder.Start().SetSearch(title);
                await DoRequestAndRespond(ctx, requestBuilder).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e, "Failed to get compat list info");
            }
        }
Ejemplo n.º 3
0
        public async Task Compat(CommandContext ctx, [RemainingText, Description("Game title to look up")] string title)
        {
            title = title?.TrimEager().Truncate(40);
            if (string.IsNullOrEmpty(title))
            {
                var prompt = await ctx.RespondAsync($"{ctx.Message.Author.Mention} what game do you want to check?").ConfigureAwait(false);

                var interact = ctx.Client.GetInteractivity();
                var response = await interact.WaitForMessageAsync(m => m.Author == ctx.Message.Author && m.Channel == ctx.Channel).ConfigureAwait(false);

                if (string.IsNullOrEmpty(response.Result?.Content) || response.Result.Content.StartsWith(Config.CommandPrefix))
                {
                    await prompt.ModifyAsync("You should specify what you're looking for").ConfigureAwait(false);

                    return;
                }

                DeletedMessagesMonitor.RemovedByBotCache.Set(prompt.Id, true, DeletedMessagesMonitor.CacheRetainTime);
                await prompt.DeleteAsync().ConfigureAwait(false);

                title = response.Result.Content.TrimEager().Truncate(40);
            }

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

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

            var productCodes = ProductCodeLookup.GetProductIds(ctx.Message.Content);

            if (productCodes.Any())
            {
                await ProductCodeLookup.LookupAndPostProductCodeEmbedAsync(ctx.Client, ctx.Message, productCodes).ConfigureAwait(false);

                return;
            }

            try
            {
                var requestBuilder = RequestBuilder.Start().SetSearch(title);
                await DoRequestAndRespond(ctx, requestBuilder).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Config.Log.Error(e, "Failed to get compat list info");
            }
        }
Ejemplo n.º 4
0
        public async Task Dump(CommandContext ctx, [RemainingText, Description("Term to dump **or** a link to a message containing the explanation")] string termOrLink = null)
        {
            if (string.IsNullOrEmpty(termOrLink))
            {
                var term = ctx.Message.Content.Split(' ', 2).Last();
                await ShowExplanation(ctx, term).ConfigureAwait(false);

                return;
            }

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

            termOrLink = termOrLink.ToLowerInvariant().StripQuotes();
            var isLink = CommandContextExtensions.MessageLinkRegex.IsMatch(termOrLink);

            if (isLink)
            {
                await DumpLink(ctx, termOrLink).ConfigureAwait(false);

                return;
            }

            using (var db = new BotDb())
            {
                var item = await db.Explanation.FirstOrDefaultAsync(e => e.Keyword == termOrLink).ConfigureAwait(false);

                if (item == null)
                {
                    var term = ctx.Message.Content.Split(' ', 2).Last();
                    await ShowExplanation(ctx, term).ConfigureAwait(false);
                }
                else
                {
                    if (!string.IsNullOrEmpty(item.Text))
                    {
                        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(item.Text)))
                            await ctx.Channel.SendFileAsync($"{termOrLink}.txt", stream).ConfigureAwait(false);
                    }
                    if (!string.IsNullOrEmpty(item.AttachmentFilename))
                    {
                        using (var stream = new MemoryStream(item.Attachment))
                            await ctx.Channel.SendFileAsync(item.AttachmentFilename, stream).ConfigureAwait(false);
                    }
                }
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public async Task ShowExplanation(CommandContext ctx, [RemainingText, Description("Term to explain")] string term)
        {
            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";
            }


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

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

            if (string.IsNullOrEmpty(term))
            {
                await ctx.RespondAsync($"You may want to look at available terms by using `{Config.CommandPrefix}explain list`{inSpecificLocation}").ConfigureAwait(false);

                return;
            }

            term = term.ToLowerInvariant();
            using (var db = new BotDb())
            {
                var explanation = await db.Explanation.FirstOrDefaultAsync(e => e.Keyword == term).ConfigureAwait(false);

                if (explanation != null)
                {
                    await ctx.RespondAsync(explanation.Text).ConfigureAwait(false);

                    return;
                }
            }

            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 new DiscordUserConverter().ConvertAsync(potentialUserId, ctx).ConfigureAwait(false);
                    hasMention = lookup.HasValue;
                }
                catch { }
                if (hasMention)
                {
                    term = term.Substring(0, idx).TrimEnd();
                    using (var db = new BotDb())
                    {
                        var explanation = await db.Explanation.FirstOrDefaultAsync(e => e.Keyword == term).ConfigureAwait(false);

                        if (explanation != null)
                        {
                            await ctx.RespondAsync(explanation.Text).ConfigureAwait(false);

                            return;
                        }
                    }
                }
            }

            var msg = $"Unknown term `{term.Sanitize()}`. Use `{Config.CommandPrefix}explain list` to look at defined terms{inSpecificLocation}";
            await ctx.RespondAsync(msg).ConfigureAwait(false);
        }