public async Task ReplyMatchingTaxaAsync(IEnumerable <ITaxon> matchingTaxa) { SortedDictionary <TaxonRankType, List <ITaxon> > taxaDict = new SortedDictionary <TaxonRankType, List <ITaxon> >(); foreach (ITaxon taxon in matchingTaxa) { if (!taxaDict.ContainsKey(taxon.Rank.Type)) { taxaDict[taxon.Rank.Type] = new List <ITaxon>(); } taxaDict[taxon.Rank.Type].Add(taxon); } Discord.Messaging.Embed embed = new Discord.Messaging.Embed(); if (taxaDict.Keys.Count() > 1) { embed.Title = string.Format("Matching taxa ({0})", matchingTaxa.Count()); } foreach (TaxonRankType type in taxaDict.Keys) { taxaDict[type].Sort((lhs, rhs) => lhs.Name.CompareTo(rhs.Name)); StringBuilder fieldContent = new StringBuilder(); foreach (ITaxon taxon in taxaDict[type]) { fieldContent.AppendLine(type == TaxonRankType.Species ? (await Db.GetSpeciesAsync(taxon.Id))?.GetShortName() : taxon.GetName()); } embed.AddField(string.Format("{0}{1} ({2})", taxaDict.Keys.Count() == 1 ? "Matching " : "", taxaDict.Keys.Count() == 1 ? type.GetName(true).ToLowerInvariant() : type.GetName(true).ToTitle(), taxaDict[type].Count()), fieldContent.ToString()); } await ReplyAsync(embed); }
public async Task <IPaginatedMessage> BuildSpeciesMessageAsync(ISpecies species) { if (!species.IsValid()) { return(null); } Discord.Messaging.Embed embed = new Discord.Messaging.Embed { Title = species.GetFullName() }; if (species.CommonNames.Count() > 0) { embed.Title += string.Format(" ({0})", string.Join(", ", species.CommonNames.Select(name => name.ToTitle()))); } if (Config.GenerationsEnabled) { // Add a field for the generation. IGeneration gen = await Db.GetGenerationByDateAsync(species.CreationDate); embed.AddField("Gen", gen is null ? "???" : gen.Number.ToString(), inline: true); } // Add a field for the species owner. embed.AddField("Owner", await GetCreatorAsync(species.Creator), inline: true); // Add a field for the species' zones. IEnumerable <ISpeciesZoneInfo> speciesZoneList = (await Db.GetZonesAsync(species)) .Where(info => !info.Zone.Flags.HasFlag(ZoneFlags.Retired)); string zonesFieldValue = speciesZoneList.ToString(ZoneListToStringOptions.Default, DiscordUtilities.MaxFieldLength); embed.AddField("Zone(s)", string.IsNullOrEmpty(zonesFieldValue) ? "None" : zonesFieldValue, inline: true); // Add the species' description. StringBuilder descriptionBuilder = new StringBuilder(); if (species.IsExtinct()) { embed.Title = "[EXTINCT] " + embed.Title; if (!string.IsNullOrEmpty(species.Status.Reason)) { descriptionBuilder.AppendLine(string.Format("**Extinct ({0}):** _{1}_\n", await BotUtils.TimestampToDateStringAsync(DateUtilities.GetTimestampFromDate((DateTimeOffset)species.Status.Date), BotContext), species.Status.Reason)); } } descriptionBuilder.Append(species.GetDescriptionOrDefault()); embed.Description = descriptionBuilder.ToString(); // Add the species' picture. embed.ThumbnailUrl = species.GetPictureUrl(); if (!string.IsNullOrEmpty(Config.WikiUrlFormat)) { // Discord automatically encodes certain characters in URIs, which doesn't allow us to update the config via Discord when we have "{0}" in the URL. // Replace this with the proper string before attempting to call string.Format. string format = Config.WikiUrlFormat.Replace("%7B0%7D", "{0}"); embed.Url = string.Format(format, Uri.EscapeUriString(WikiUtilities.GetWikiPageTitle(species))); } // Create embed pages. IEnumerable <Discord.Messaging.IEmbed> embedPages = EmbedUtilities.CreateEmbedPages(embed, EmbedPaginationOptions.AddPageNumbers | EmbedPaginationOptions.CopyFields); IPaginatedMessage paginatedMessage = new PaginatedMessage(embedPages); if (speciesZoneList.Count() > 0) { paginatedMessage.SetColor((await Db.GetZoneTypeAsync(speciesZoneList.GroupBy(x => x.Zone.TypeId).OrderBy(x => x.Count()).Last().Key)).Color); } if (species.IsExtinct()) { paginatedMessage.SetColor(Color.Red); } return(paginatedMessage); }