private async Task <EmbedBuilder> SearchAsync(string searchTerm, CATEGORY category, ITagIndex tagIndex, int postIndex = 0, ROLL_SEQUENCE rollSequence = ROLL_SEQUENCE.RANDOM) { if (string.IsNullOrEmpty(searchTerm)) { string usageString = "Usage: "; switch (category) { case CATEGORY.CHARACTER: await ReplyAsync(usageString + "oka.character character_name"); break; case CATEGORY.SERIES: await ReplyAsync(usageString + "oka.series series_name"); break; } return(null); } if (!_pageService.HandlerAdded) { Context.Client.ReactionAdded += ReactionAdded_Event; _pageService.HandlerAdded = true; } searchTerm = TagParser.Format(searchTerm); string tag; string searchTermEscaped; string searchTermMatched; int id; PostData postData = null; List <string> tags; List <TagData> tagData; EmbedBuilder embed; List <EmbedFieldBuilder> fields = new List <EmbedFieldBuilder>(); string embedDescription = rerollCharacterDescription + ", " + rerollSeriesDescription + ". " + Environment.NewLine + listCharactersDescription + "."; using (MySqlConnection conn = tagIndex.GetConnection()) { if (int.TryParse(searchTerm, out id)) { tag = tagIndex.LookupTagById(id, conn); if (!string.IsNullOrEmpty(tag)) { searchTerm = tag; } } searchTermEscaped = TagParser.EscapeUnderscore(searchTerm); if (tagIndex.HasExactMatch(searchTerm, conn, out searchTermMatched)) { switch (rollSequence) { case ROLL_SEQUENCE.RANDOM: postData = tagIndex.LookupRandomPost(searchTermMatched, conn); break; case ROLL_SEQUENCE.PREVIOUS: postData = tagIndex.LookupPreviousPost(searchTermMatched, postIndex, conn); break; case ROLL_SEQUENCE.NEXT: postData = tagIndex.LookupNextPost(searchTermMatched, postIndex, conn); break; } switch (category) { case CATEGORY.CHARACTER: embedDescription += Environment.NewLine + cycleCharacterPageDescription + "."; break; } if (postData != null && !string.IsNullOrEmpty(postData.Link)) { embed = BuildImageEmbed(postData, embedDescription); } else { await ReplyAsync(NoImages.Replace("%", searchTermEscaped)); return(null); } } else { embed = new EmbedBuilder(); tags = tagIndex.LookupTags(searchTerm, conn); if (tags.Count > 0) { if (tags.Count > MaxSearchResults) { await ReplyAsync(ExcessiveResults.Replace("%", searchTerm)); return(null); } tagData = tagIndex.LookupTagData(tags, conn); pages = TagParser.CompileSuggestions(tagData, EmbedBuilder.MaxFieldCount); embed = BuildSuggestionsEmbed(pages); } else { await ReplyAsync(NoResults.Replace("%", searchTermEscaped)); return(null); } } return(embed); } }
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); } }