public PostData LookupPreviousPost(string searchTerm, int postId, MySqlConnection conn) { searchTerm = TagParser.EscapeApostrophe(searchTerm); int previousLinkId = GetPreviousLinkId(searchTerm, postId, conn); PostData pd = GetPostForQuery(BuildPostQuery(_postQuery, searchTerm, previousLinkId), conn); pd.PostIndex = GetIndexForPostId(searchTerm, previousLinkId, conn); return(pd); }
public PostData LookupRandomPost(string searchTerm, MySqlConnection conn) { searchTerm = TagParser.EscapeApostrophe(searchTerm); string linkIdQuery = $"SELECT tl.link_id " + $"FROM tags AS t, tag_links AS tl " + $"WHERE t.id = tl.tag_id AND t.name = '{searchTerm}'"; int randomId = GetRandomLinkIdForQuery(linkIdQuery, conn); return(GetPostForQuery(BuildPostQuery(_postQuery, searchTerm, randomId), conn)); }
public PostData LookupRandomPost(string searchTerm, MySqlConnection conn) { searchTerm = TagParser.EscapeApostrophe(searchTerm); string postQuery = $"SELECT t.id, t.name, l.url, s.name, l.id, s.post_count " + $"FROM links AS l, tags AS t, tag_links AS tl, series_tags AS st, series AS s " + $"WHERE l.id = tl.link_id AND t.id = tl.tag_id AND t.id = st.tag_id AND s.id = st.series_id AND s.name = '&' AND l.id = %"; string linkIdQuery = $"SELECT tl.link_id " + $"FROM tags AS t, series AS s, series_tags AS st, tag_links AS tl " + $"WHERE s.id = st.series_id AND st.tag_id = t.id AND t.id = tl.tag_id AND s.name = '{searchTerm}'"; return(GetPostForQuery(BuildPostQuery(postQuery, searchTerm, GetRandomLinkIdForQuery(linkIdQuery, conn)), conn)); }
private PostData RandomCollabPost(string[] searchTerms, MySqlConnection conn) { Random rand = new Random(); List <int> commonLinkIds = GetCommonLinkIds(searchTerms, conn).ToList(); commonLinkIds.Sort(); string searchTermEscaped = TagParser.EscapeApostrophe(searchTerms[0]); int randomId = commonLinkIds[rand.Next(0, commonLinkIds.Count())]; string postQuery = BuildPostQuery(_postQuery, searchTermEscaped, randomId); PostData pd = GetPostForQuery(postQuery, conn); pd.PostIndex = commonLinkIds.IndexOf(randomId); pd.PostCount = commonLinkIds.Count(); return(pd); }
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); } }
private EmbedBuilder BuildImageEmbed(PostData postData, string embedDescription) { StringBuilder seriesNames = new StringBuilder(TagParser.EscapeUnderscore(postData.SeriesName)); StringBuilder title = new StringBuilder(TagParser.BuildTitle(postData.TagName)); StringBuilder tagIds = new StringBuilder(postData.TagId.ToString()); StringBuilder tagIdTitle = new StringBuilder("Character ID"); StringBuilder seriesNameTitle = new StringBuilder("Series Name"); if (postData.AdditionalData != null) { foreach (string s in postData.AdditionalData.AdditionalTagNames) { title.Append($", {TagParser.BuildTitle(s)}"); } foreach (int i in postData.AdditionalData.AdditionalTagIds) { tagIds.Append($", {i}"); } tagIdTitle.Append("s"); foreach (string s in postData.AdditionalData.AdditionalSeriesNames) { if (s != postData.SeriesName) { seriesNames.Append($", {s}"); } } seriesNameTitle.Append("s"); } return(new EmbedBuilder().WithTitle(title.ToString()) .AddField(tagIdTitle.ToString(), tagIds.ToString(), true) .AddField("Post ID", postData.LinkId, true) .AddField(seriesNameTitle.ToString(), seriesNames.ToString(), true) .WithDescription(embedDescription) .WithImageUrl(postData.Link) .WithUrl(postData.Link) .WithAuthor(Context.Client.CurrentUser) .WithFooter($"Image {postData.PostIndex + 1} of {postData.PostCount}") .WithColor(Color.DarkGrey) .WithCurrentTimestamp()); }
private PostData PreviousCollabPost(string[] searchTerms, int currentId, MySqlConnection conn) { List <int> commonLinkIds = GetCommonLinkIds(searchTerms, conn).ToList(); commonLinkIds.Sort(); int idIndex = commonLinkIds.IndexOf(currentId); int previousIndex = idIndex == 0 ? idIndex : idIndex - 1; string postQuery = BuildPostQuery(_postQuery, TagParser.EscapeApostrophe(searchTerms[0]), commonLinkIds[previousIndex]); PostData pd = GetPostForQuery(postQuery, conn); AdditionalPostData apd = BuildAdditionalPostData(searchTerms, conn); pd.PostIndex = previousIndex; pd.PostCount = commonLinkIds.Count(); pd.AdditionalData = apd; return(pd); }
public void SortPostNumDesc(ulong msgId) { PageData pageData = PageIndex[msgId]; List <TagData> tagData = new List <TagData>(); foreach (List <TagData> page in pageData.Pages) { foreach (TagData t in page) { tagData.Add(t); } } tagData.Sort((t1, t2) => t2.NumLinks.CompareTo(t1.NumLinks)); PageIndex[msgId].Pages = TagParser.CompileSuggestions(tagData, EmbedBuilder.MaxFieldCount); PageIndex[msgId].AlphabeticallySorted = false; PageIndex[msgId].NumericallySorted = true; PageIndex[msgId].SortedAscending = false; }
private EmbedBuilder UpdatePage(PageData pageData, string embedTitle) { List <EmbedFieldBuilder> fields = new List <EmbedFieldBuilder>(); List <string> pageContent = TagParser.ToTagInfoList(pageData.Pages[pageData.PageNum]); int i = pageData.PageNum * EmbedBuilder.MaxFieldCount + 1; foreach (string s in pageContent) { fields.Add(new EmbedFieldBuilder() .WithName($"{i}.") .WithValue(s) .WithIsInline(IsInline)); i++; } return(new EmbedBuilder() .WithTitle(embedTitle) .WithFields(fields) .WithDescription(suggestionDescription) .WithFooter($"Page {pageData.PageNum + 1} of {pageData.Pages.Count}")); }
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()); } } }
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); } }
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)); } } }