public async Task Random(string taxonName) { // Get the taxon. Taxon taxon = await BotUtils.GetTaxonFromDb(taxonName); if (taxon is null) { await BotUtils.ReplyAsync_Error(Context, "No such taxon exists."); return; } // Get all species under that taxon. List <Species> species = new List <Species>(); species.AddRange(await BotUtils.GetSpeciesInTaxonFromDb(taxon)); species.RemoveAll(x => x.IsExtinct); if (species.Count() <= 0) { await BotUtils.ReplyAsync_Info(Context, string.Format("{0} **{1}** does not contain any extant species.", StringUtils.ToTitleCase(taxon.GetTypeName()), taxon.GetName())); } else { await SpeciesCommands.ShowSpeciesInfoAsync(Context, species[BotUtils.RandomInteger(species.Count())]); } }
public async Task GetInfo(string name) { // Prioritize species first. Species[] species = await BotUtils.GetSpeciesFromDb("", name); if (species.Count() > 0) { if (await BotUtils.ReplyValidateSpeciesAsync(Context, species)) { await SpeciesCommands.ShowSpeciesInfoAsync(Context, species[0]); } } else { // Otherwise, show other taxon. Taxon[] taxa = await BotUtils.GetTaxaFromDb(name); if (taxa.Count() <= 0) { // This command was traditionally used with species, so show the user species suggestions in the event of no matches. await BotUtils.ReplyAsync_SpeciesSuggestions(Context, "", name, async (BotUtils.ConfirmSuggestionArgs args) => await GetInfo(args.Suggestion)); } else if (await BotUtils.ReplyAsync_ValidateTaxa(Context, taxa)) { await BotUtils.Command_ShowTaxon(Context, taxa[0].type, name); } } }
public async Task Random() { // Get a random species from the database. using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id NOT IN (SELECT species_id FROM Extinctions) ORDER BY RANDOM() LIMIT 1;")) { DataRow row = await Database.GetRowAsync(cmd); if (row is null) { await BotUtils.ReplyAsync_Info(Context, "There are currently no extant species."); } else { await SpeciesCommands.ShowSpeciesInfoAsync(Context, await SpeciesUtils.SpeciesFromDataRow(row)); } } }
public async Task Search([Remainder] string queryString) { // Create and execute the search query. Taxa.SearchQuery query = new Taxa.SearchQuery(Context, queryString); Taxa.SearchQueryResult result = await query.GetResultAsync(); // Build the embed. if (result.Count() <= 0) { await BotUtils.ReplyAsync_Info(Context, "No species matching this query could be found."); } else { if (result.DisplayFormat == Taxa.DisplayFormat.Gallery) { List <Picture> pictures = new List <Picture>(); foreach (Species species in result.ToArray()) { pictures.AddRange(await SpeciesUtils.GetPicturesAsync(species)); } await GalleryCommands.ShowGalleryAsync(Context, string.Format("search results ({0})", result.Count()), pictures.ToArray()); } else if (result.DisplayFormat == Taxa.DisplayFormat.Leaderboard) { // Match each group to a rank depending on how many results it contains. Dictionary <Taxa.SearchQueryResult.Group, long> groupRanks = new Dictionary <Taxa.SearchQueryResult.Group, long>(); long rank = 0; int lastCount = -1; foreach (Taxa.SearchQueryResult.Group group in result.Groups.OrderByDescending(x => x.Count())) { groupRanks[group] = (lastCount >= 0 && group.Count() == lastCount) ? rank : ++rank; lastCount = group.Count(); } // Create a list of groups that will be displayed to the user. List <string> lines = new List <string>(); foreach (Taxa.SearchQueryResult.Group group in result.Groups) { lines.Add(string.Format("**`{0}.`**{1}`{2}` {3}", groupRanks[group].ToString("000"), UserRank.GetRankIcon(groupRanks[group]), group.Count().ToString("000"), string.Format(groupRanks[group] <= 3 ? "**{0}**" : "{0}", string.IsNullOrEmpty(group.Name) ? "Results" : StringUtils.ToTitleCase(group.Name)) )); } Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder { Title = string.Format("Search results ({0})", result.Groups.Count()) }; embed.AddPages(EmbedUtils.LinesToEmbedPages(lines)); embed.AddPageNumbers(); await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build()); } else { if (result.Count() == 1) { // If there's only one result, just show that species. await SpeciesCommands.ShowSpeciesInfoAsync(Context, result.ToArray()[0]); } else { Bot.PaginatedMessageBuilder embed; if (result.HasGroup(Taxa.SearchQuery.DefaultGroupName)) { // If there's only one group, just list the species without creating separate fields. embed = new Bot.PaginatedMessageBuilder(EmbedUtils.ListToEmbedPages(result.DefaultGroup.ToStringArray().ToList(), fieldName: string.Format("Search results ({0})", result.Count()))); } else { embed = new Bot.PaginatedMessageBuilder(); embed.AddPages(EmbedUtils.SearchQueryResultToEmbedPages(result)); } embed.SetFooter(""); embed.AddPageNumbers(); await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build()); } } } }
public async Task GetInfo(string genusName, string speciesName) { await SpeciesCommands.ShowSpeciesInfoAsync(Context, genusName, speciesName); }