Example #1
0
        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);
            }
        }
Example #2
0
        public async Task WithCharacterAsync(string charName = null)
        {
            if (string.IsNullOrEmpty(charName))
            {
                await ReplyAsync("Usage: oka.with_character character_name");

                return;
            }

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

            DbCharacterIndex charIndex   = _characterIndex;
            DbSeriesIndex    seriesIndex = _seriesIndex;

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

                    if (int.TryParse(charName, out id))
                    {
                        tag = charIndex.LookupTagById(id, charConn);

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

                    charName        = TagParser.Format(charName);
                    charNameEscaped = TagParser.EscapeUnderscore(charName);

                    string        series     = charIndex.SeriesWithCharacter(charName, charConn);
                    List <string> seriesList = new List <string>();

                    seriesList.Add(series);

                    if (!string.IsNullOrEmpty(series))
                    {
                        if (seriesList.Count < MaxSearchResults)
                        {
                            List <TagData> seriesData = seriesIndex.LookupTagData(seriesList, seriesConn);

                            pages = TagParser.CompileSuggestions(seriesData, 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("%", charNameEscaped));
                        }
                    }
                    else
                    {
                        await ReplyAsync(NoResults.Replace("%", charNameEscaped));
                    }
                }
        }