コード例 #1
0
ファイル: Search.cs プロジェクト: rnordstrom/LobitaBot
 public Search(PageService ps)
 {
     _pageService    = ps;
     _characterIndex = new DbCharacterIndex(
         ConfigUtils.GetCurrentDatabase(Constants.ProductionConfig));
     _seriesIndex = new DbSeriesIndex(
         ConfigUtils.GetCurrentDatabase(Constants.ProductionConfig));
 }
コード例 #2
0
ファイル: Search.cs プロジェクト: rnordstrom/LobitaBot
        private async Task <EmbedBuilder> RandomPostAsync()
        {
            if (!_pageService.HandlerAdded)
            {
                Context.Client.ReactionAdded += ReactionAdded_Event;
                _pageService.HandlerAdded     = true;
            }

            DbCharacterIndex characterIndex = _characterIndex;

            using (MySqlConnection conn = _characterIndex.GetConnection())
            {
                EmbedBuilder embed;
                PostData     postData;
                string       tag = characterIndex.LookupRandomTag(conn);
                string       title;

                if (!string.IsNullOrEmpty(tag))
                {
                    title    = TagParser.BuildTitle(tag);
                    postData = characterIndex.LookupRandomPost(tag, conn);

                    while (postData == null)
                    {
                        tag      = characterIndex.LookupRandomTag(conn);
                        title    = TagParser.BuildTitle(tag);
                        postData = characterIndex.LookupRandomPost(tag, conn);
                    }

                    string embedDescription = rerollRandomDescription + "." +
                                              Environment.NewLine +
                                              listCharactersDescription + ".";

                    embed = BuildImageEmbed(postData, embedDescription);
                }
                else
                {
                    await ReplyAsync("An error occurred during random tag lookup.");

                    return(null);
                }

                return(embed);
            }
        }
コード例 #3
0
ファイル: Search.cs プロジェクト: rnordstrom/LobitaBot
        public async Task ReactionAdded_Event(Cacheable <IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
        {
            var msg = message.GetOrDownloadAsync().Result;

            if (reaction.UserId == msg.Author.Id)
            {
                return;
            }

            IEmbed       msgEmbed    = msg.Embeds.First();
            var          embedFields = msgEmbed.Fields;
            string       embedTitle  = msgEmbed.Title;
            string       footerText  = msgEmbed.Footer.ToString();
            int          imageIndex  = 0;
            int          numImages   = 0;
            string       characterId;
            int          postId;
            string       seriesName;
            EmbedBuilder embedBuilder;
            Embed        embed;
            PageData     pageData;
            bool         success;

            if (reaction.Emote.Name == Constants.RerollCharacter.Name)
            {
                characterId  = embedFields[0].Value;
                embedBuilder = SearchAsync(characterId, CATEGORY.CHARACTER, _characterIndex).Result;

                if (embedBuilder != null)
                {
                    embed = embedBuilder.Build();
                    var toSend = await channel.SendMessageAsync(embed : embed);

                    if (embed.Image != null)
                    {
                        await toSend.AddReactionAsync(Constants.RerollCharacter);

                        await toSend.AddReactionAsync(Constants.RerollSeries);

                        await toSend.AddReactionAsync(Constants.Characters);

                        await toSend.AddReactionAsync(Constants.PreviousImage);

                        await toSend.AddReactionAsync(Constants.NextImage);
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.RerollSeries.Name)
            {
                seriesName   = TagParser.Format(embedFields[2].Value);
                embedBuilder = SearchAsync(seriesName, CATEGORY.SERIES, _seriesIndex).Result;

                if (embedBuilder != null)
                {
                    embed = embedBuilder.Build();
                    var toSend = await channel.SendMessageAsync(embed : embed);

                    if (embed.Image != null)
                    {
                        await toSend.AddReactionAsync(Constants.RerollCharacter);

                        await toSend.AddReactionAsync(Constants.RerollSeries);

                        await toSend.AddReactionAsync(Constants.Characters);
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.RerollCollab.Name)
            {
                characterId  = embedFields[0].Value;
                embedBuilder = SearchCollabAsync(characterId.Split(", ")).Result;

                if (embedBuilder != null)
                {
                    embed = embedBuilder.Build();
                    var toSend = await channel.SendMessageAsync(embed : embed);

                    if (embed.Image != null)
                    {
                        await toSend.AddReactionAsync(Constants.RerollCollab);

                        await toSend.AddReactionAsync(Constants.Characters);

                        await toSend.AddReactionAsync(Constants.PreviousImage);

                        await toSend.AddReactionAsync(Constants.NextImage);
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.PreviousImage.Name)
            {
                characterId = embedFields[0].Value;
                postId      = int.Parse(embedFields[1].Value);
                footerText.Split(" ").First(i => int.TryParse(i, out imageIndex));
                footerText.Split(" ").Last(i => int.TryParse(i, out numImages));

                if (imageIndex > 1)
                {
                    if (characterId.Contains(", "))
                    {
                        embedBuilder = SearchCollabAsync(characterId.Split(", "), postId, ROLL_SEQUENCE.PREVIOUS).Result;

                        if (embedBuilder != null)
                        {
                            embed = embedBuilder.Build();
                            var toSend = await channel.SendMessageAsync(embed : embed);

                            if (embed.Image != null)
                            {
                                await toSend.AddReactionAsync(Constants.RerollCollab);

                                await toSend.AddReactionAsync(Constants.Characters);

                                await toSend.AddReactionAsync(Constants.PreviousImage);

                                await toSend.AddReactionAsync(Constants.NextImage);
                            }
                        }
                    }
                    else
                    {
                        embedBuilder = SearchAsync(
                            characterId,
                            CATEGORY.CHARACTER,
                            _characterIndex,
                            postId,
                            ROLL_SEQUENCE.PREVIOUS).Result;

                        if (embedBuilder != null)
                        {
                            embed = embedBuilder.Build();
                            var toSend = await channel.SendMessageAsync(embed : embed);

                            if (embed.Image != null)
                            {
                                await toSend.AddReactionAsync(Constants.RerollCharacter);

                                await toSend.AddReactionAsync(Constants.RerollSeries);

                                await toSend.AddReactionAsync(Constants.Characters);

                                await toSend.AddReactionAsync(Constants.PreviousImage);

                                await toSend.AddReactionAsync(Constants.NextImage);
                            }
                        }
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.NextImage.Name)
            {
                characterId = embedFields[0].Value;
                postId      = int.Parse(embedFields[1].Value);
                footerText.Split(" ").First(i => int.TryParse(i, out imageIndex));
                footerText.Split(" ").Last(i => int.TryParse(i, out numImages));

                if (imageIndex < numImages)
                {
                    if (characterId.Contains(", "))
                    {
                        embedBuilder = SearchCollabAsync(characterId.Split(", "), postId, ROLL_SEQUENCE.NEXT).Result;

                        if (embedBuilder != null)
                        {
                            embed = embedBuilder.Build();
                            var toSend = await channel.SendMessageAsync(embed : embed);

                            if (embed.Image != null)
                            {
                                await toSend.AddReactionAsync(Constants.RerollCollab);

                                await toSend.AddReactionAsync(Constants.Characters);

                                await toSend.AddReactionAsync(Constants.PreviousImage);

                                await toSend.AddReactionAsync(Constants.NextImage);
                            }
                        }
                    }
                    else
                    {
                        embedBuilder = SearchAsync(
                            characterId,
                            CATEGORY.CHARACTER,
                            _characterIndex,
                            postId,
                            ROLL_SEQUENCE.NEXT).Result;

                        if (embedBuilder != null)
                        {
                            embed = embedBuilder.Build();
                            var toSend = await channel.SendMessageAsync(embed : embed);

                            if (embed.Image != null)
                            {
                                await toSend.AddReactionAsync(Constants.RerollCharacter);

                                await toSend.AddReactionAsync(Constants.RerollSeries);

                                await toSend.AddReactionAsync(Constants.Characters);

                                await toSend.AddReactionAsync(Constants.PreviousImage);

                                await toSend.AddReactionAsync(Constants.NextImage);
                            }
                        }
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.Characters.Name)
            {
                DbCharacterIndex charIndex = _characterIndex;

                using (MySqlConnection conn = _characterIndex.GetConnection())
                {
                    List <string>  charsInPost = charIndex.CharactersInPost(int.Parse(embedFields[1].Value), conn);
                    List <TagData> charData    = charIndex.LookupTagData(charsInPost, conn);

                    pages    = TagParser.CompileSuggestions(charData, EmbedBuilder.MaxFieldCount);
                    pageData = new PageData(pages);

                    embedBuilder = BuildSuggestionsEmbed(pages);
                    var toSend = await channel.SendMessageAsync(embed : embedBuilder.Build());

                    ulong msgId = toSend.Id;

                    _pageService.AddLimited(msgId, pageData);

                    await toSend.AddReactionAsync(Constants.PageBack);

                    await toSend.AddReactionAsync(Constants.PageForward);

                    await toSend.AddReactionAsync(Constants.SortAlphabetical);

                    await toSend.AddReactionAsync(Constants.SortNumerical);

                    await toSend.AddReactionAsync(Constants.ChangeOrder);
                }
            }
            else if (reaction.Emote.Name == Constants.RerollRandom.Name)
            {
                embedBuilder = RandomPostAsync().Result;

                if (embedBuilder != null)
                {
                    embed = embedBuilder.Build();
                    var toSend = await channel.SendMessageAsync(embed : embed);

                    if (embed.Image != null)
                    {
                        await toSend.AddReactionAsync(Constants.RerollRandom);

                        await toSend.AddReactionAsync(Constants.Characters);
                    }
                }
            }
            else if (reaction.Emote.Name == Constants.PageBack.Name)
            {
                success = _pageService.PageIndex.TryGetValue(msg.Id, out pageData);

                if (success && pageData.PageNum > 0)
                {
                    _pageService.PageIndex[msg.Id].PageNum--;

                    EmbedBuilder builder = UpdatePage(pageData, embedTitle);

                    await msg.ModifyAsync(msg => msg.Embed = builder.Build());
                }
            }
            else if (reaction.Emote.Name == Constants.PageForward.Name)
            {
                success = _pageService.PageIndex.TryGetValue(msg.Id, out pageData);

                if (success && pageData.PageNum < pageData.Pages.Count - 1)
                {
                    _pageService.PageIndex[msg.Id].PageNum++;

                    EmbedBuilder builder = UpdatePage(pageData, embedTitle);

                    await msg.ModifyAsync(msg => msg.Embed = builder.Build());
                }
            }
            else if (reaction.Emote.Name == Constants.SortAlphabetical.Name)
            {
                success = _pageService.PageIndex.TryGetValue(msg.Id, out pageData);

                if (success)
                {
                    _pageService.SortAlphabeticalAsc(msg.Id);

                    EmbedBuilder builder = UpdatePage(pageData, embedTitle);

                    await msg.ModifyAsync(msg => msg.Embed = builder.Build());
                }
            }
            else if (reaction.Emote.Name == Constants.SortNumerical.Name)
            {
                success = _pageService.PageIndex.TryGetValue(msg.Id, out pageData);

                if (success)
                {
                    _pageService.SortPostNumAsc(msg.Id);

                    EmbedBuilder builder = UpdatePage(pageData, embedTitle);

                    await msg.ModifyAsync(msg => msg.Embed = builder.Build());
                }
            }
            else if (reaction.Emote.Name == Constants.ChangeOrder.Name)
            {
                success = _pageService.PageIndex.TryGetValue(msg.Id, out pageData);

                if (success)
                {
                    if (pageData.AlphabeticallySorted)
                    {
                        if (pageData.SortedAscending)
                        {
                            _pageService.SortAlphabeticalDesc(msg.Id);
                        }
                        else
                        {
                            _pageService.SortAlphabeticalAsc(msg.Id);
                        }
                    }
                    else if (pageData.NumericallySorted)
                    {
                        if (pageData.SortedAscending)
                        {
                            _pageService.SortPostNumDesc(msg.Id);
                        }
                        else
                        {
                            _pageService.SortPostNumAsc(msg.Id);
                        }
                    }

                    EmbedBuilder builder = UpdatePage(pageData, embedTitle);

                    await msg.ModifyAsync(msg => msg.Embed = builder.Build());
                }
            }
        }
コード例 #4
0
ファイル: Search.cs プロジェクト: rnordstrom/LobitaBot
        private async Task <EmbedBuilder> SearchCollabAsync(string[] searchTerms, int postId = 0, ROLL_SEQUENCE rollSequence = ROLL_SEQUENCE.RANDOM)
        {
            if (!_pageService.HandlerAdded)
            {
                Context.Client.ReactionAdded += ReactionAdded_Event;
                _pageService.HandlerAdded     = true;
            }

            if (searchTerms.Length < 2 || searchTerms.Length > 5)
            {
                await ReplyAsync("Please provide between 2 and 5 character names.");

                return(null);
            }

            bool wildcardLastArg = false;

            if (searchTerms[searchTerms.Length - 1] == "*")
            {
                wildcardLastArg = true;
                Array.Resize(ref searchTerms, searchTerms.Length - 1);
            }

            List <string> searchTermsList = new List <string>(searchTerms);

            foreach (string s in searchTermsList)
            {
                if (searchTermsList.FindAll(t => t == s).Count > 1)
                {
                    await ReplyAsync("Names may not be specified more than once.");

                    return(null);
                }
            }

            DbCharacterIndex charIndex = _characterIndex;

            using (MySqlConnection charConn = _characterIndex.GetConnection())
            {
                int    id;
                string tag;
                string matchedName;

                for (int i = 0; i < searchTerms.Length; i++)
                {
                    if (int.TryParse(searchTerms[i], out id))
                    {
                        tag = charIndex.LookupTagById(id, charConn);

                        if (!string.IsNullOrEmpty(tag))
                        {
                            searchTerms[i] = tag;
                        }
                    }

                    searchTerms[i] = TagParser.Format(searchTerms[i]);

                    if (!charIndex.HasExactMatch(searchTerms[i], charConn, out matchedName))
                    {
                        await ReplyAsync($"Character name '{TagParser.EscapeUnderscore(searchTerms[i])}' could not be found.");

                        return(null);
                    }
                    else
                    {
                        searchTerms[i] = matchedName;
                    }
                }

                EmbedBuilder embed;

                if (wildcardLastArg)
                {
                    List <string> furtherCollabs = charIndex.CollabsWithCharacters(searchTerms, charConn);

                    if (furtherCollabs.Count == 0)
                    {
                        await ReplyAsync($"No further collabs with the specified character(s) exist.");

                        return(null);
                    }

                    List <TagData> tagData = charIndex.LookupTagData(furtherCollabs, charConn);

                    pages = TagParser.CompileSuggestions(tagData, EmbedBuilder.MaxFieldCount);
                    embed = BuildSuggestionsEmbed(pages);
                }
                else
                {
                    PostData postData = null;

                    switch (rollSequence)
                    {
                    case ROLL_SEQUENCE.RANDOM:
                        postData = charIndex.LookupRandomCollab(searchTerms, charConn);
                        break;

                    case ROLL_SEQUENCE.PREVIOUS:
                        postData = charIndex.LookupPreviousCollab(searchTerms, postId, charConn);
                        break;

                    case ROLL_SEQUENCE.NEXT:
                        postData = charIndex.LookupNextCollab(searchTerms, postId, charConn);
                        break;
                    }

                    string embedDescription = rerollCollabDescription + "." +
                                              Environment.NewLine +
                                              listCharactersDescription + ".";

                    embedDescription += Environment.NewLine + cycleCharacterPageDescription + ".";

                    if (postData != null && !string.IsNullOrEmpty(postData.Link))
                    {
                        embed = BuildImageEmbed(postData, embedDescription);
                    }
                    else
                    {
                        await ReplyAsync($"No images found for this collab.");

                        return(null);
                    }
                }

                return(embed);
            }
        }
コード例 #5
0
ファイル: Search.cs プロジェクト: rnordstrom/LobitaBot
        public async Task InSeriesAsync(string seriesName = null)
        {
            if (string.IsNullOrEmpty(seriesName))
            {
                await ReplyAsync("Usage: oka.in_series series_name");

                return;
            }

            if (!_pageService.HandlerAdded)
            {
                Context.Client.ReactionAdded += ReactionAdded_Event;
                _pageService.HandlerAdded     = true;
            }

            DbSeriesIndex    seriesIndex = _seriesIndex;
            DbCharacterIndex charIndex   = _characterIndex;

            using (MySqlConnection charConn = _characterIndex.GetConnection())
                using (MySqlConnection seriesConn = _seriesIndex.GetConnection())
                {
                    int    id;
                    string tag;
                    string seriesNameEscaped;

                    if (int.TryParse(seriesName, out id))
                    {
                        tag = seriesIndex.LookupTagById(id, seriesConn);

                        if (!string.IsNullOrEmpty(tag))
                        {
                            seriesName = tag;
                        }
                    }

                    seriesName        = TagParser.Format(seriesName);
                    seriesNameEscaped = TagParser.EscapeUnderscore(seriesName);

                    List <string> characters = seriesIndex.CharactersInSeries(seriesName, seriesConn);

                    if (characters.Count > 0)
                    {
                        if (characters.Count < MaxSearchResults)
                        {
                            List <TagData> characterData = charIndex.LookupTagData(characters, charConn);

                            pages = TagParser.CompileSuggestions(characterData, EmbedBuilder.MaxFieldCount);

                            EmbedBuilder embed = BuildSuggestionsEmbed(pages);

                            if (embed != null)
                            {
                                var toSend = await Context.Channel.SendMessageAsync(embed : embed.Build());

                                ulong    msgId    = toSend.Id;
                                PageData pageData = new PageData(pages);

                                _pageService.AddLimited(msgId, pageData);

                                await toSend.AddReactionAsync(Constants.PageBack);

                                await toSend.AddReactionAsync(Constants.PageForward);

                                await toSend.AddReactionAsync(Constants.SortAlphabetical);

                                await toSend.AddReactionAsync(Constants.SortNumerical);

                                await toSend.AddReactionAsync(Constants.ChangeOrder);
                            }
                        }
                        else
                        {
                            await ReplyAsync(ExcessiveResults.Replace("%", seriesNameEscaped));
                        }
                    }
                    else
                    {
                        await ReplyAsync(NoResults.Replace("%", seriesNameEscaped));
                    }
                }
        }