public async Task GetZoneTypes() { IEnumerable <IZone> zones = await Db.GetZonesAsync(); IEnumerable <IZoneType> zoneTypes = (await Db.GetZoneTypesAsync()) .OrderBy(type => type.Name) .Where(type => zones.Any(zone => zone.TypeId == type.Id)); if (zoneTypes.Any()) { List <string> lines = new List <string>(); foreach (IZoneType zoneType in zoneTypes) { StringBuilder lineBuilder = new StringBuilder(); int zoneCount = zones.Where(zone => zone.TypeId == zoneType.Id).Count(); lineBuilder.Append($"{zoneType.Icon} {zoneType.Name.ToTitle().ToBold()} ({zoneCount})"); if (!string.IsNullOrWhiteSpace(zoneType.Description)) { lineBuilder.Append("\t—\t"); lineBuilder.Append(zoneType.Description.GetFirstSentence()); } lines.Add(lineBuilder.ToString().Truncate(DiscordUtilities.MaxEmbedLineLength - 2, true)); } string description = $"For detailed type information, use `{Config.Prefix}zonetype <type>` (e.g. `{Config.Prefix}zonetype {zoneTypes.First().Name.ToLowerInvariant()}`).\n\n"; IPaginatedMessage message = new PaginatedMessage(); message.AddLines($"All Zone Types ({zoneTypes.Count()})", lines, columnsPerPage: 1, options: EmbedPaginationOptions.AddPageNumbers); foreach (IEmbed embed in message.Select(page => page.Embed)) { embed.Description = description + embed.Description; embed.Footer += " — Empty types are not listed."; } await ReplyAsync(message); } else { await ReplyInfoAsync("No zone types have been added."); } }
private async Task <SQLiteDatabase> GetDatabaseFromDMAsync() { if (Config.SingleDatabase) { // If we're running in single-database mode, there is only one database to choose from. return(await DatabaseService.GetDatabaseAsync(Context.Guild)); } else { // Find guilds that we share with the user. List <IGuild> sharedGuilds = new List <IGuild>(); foreach (IGuild guild in await Context.Client.GetGuildsAsync()) { if (await guild.GetUserAsync(Context.User.Id) != null) { sharedGuilds.Add(guild); } } // If the user has a DM guild set and is still in that guild, use it. IGuild userDMGuild = await GetUserDMGuildAsync(Context.User); if (userDMGuild != null && sharedGuilds.Any(guild => guild.Id == userDMGuild.Id)) { return(await DatabaseService.GetDatabaseAsync(userDMGuild)); } else { // The user does not have a DM guild set. List <SQLiteDatabase> databases = new List <SQLiteDatabase>(); foreach (IGuild guild in sharedGuilds) { databases.Add(await DatabaseService.GetDatabaseAsync(guild)); } if (databases.Count() == 1) { return(databases.First()); } else if (databases.Count() > 0) { IPaginatedMessage message = new PaginatedMessage(); message.AddLines(string.Empty, sharedGuilds.Select( (guild, index) => $"`{(index + 1).ToString().PadLeft(sharedGuilds.Count().ToString().Length)}.` {guild.Name}"), options: EmbedPaginationOptions.Default | EmbedPaginationOptions.AddPageNumbers); foreach (Discord.Messaging.IEmbed embed in message.Select(page => page.Embed)) { embed.Description = $"You share more than one guild with {Bot.Name}. You can pick which guild you would like to interact with using the `{Config.Prefix}pickserver` command.\n\n" + embed.Description; } message.SetTitle($"Shared Guilds ({sharedGuilds.Count()})"); await ReplyAsync(message); throw new Exception($"You must specify which guild you would like to interact with using the `{Config.Prefix}pickserver` command."); } else { throw new Exception($"You must share at least one guild with {Bot.Name}."); } } } }
public async Task <IPaginatedMessage> BuildTaxonMessageAsync(ITaxon taxon) { if (!taxon.IsValid()) { return(null); } List <string> subItems = new List <string>(); if (taxon.Rank.Type == TaxonRankType.Species) { ISpecies species = await Db.GetSpeciesAsync(taxon.Id); return(await BuildSpeciesMessageAsync(species)); } else if (taxon.Rank.Type == TaxonRankType.Genus) { // For genera, get all species underneath it. // This will let us check if the species is extinct, and cross it out if that's the case. List <ISpecies> speciesList = new List <ISpecies>(); foreach (ITaxon subtaxon in await Db.GetSubtaxaAsync(taxon)) { speciesList.Add(await Db.GetSpeciesAsync(subtaxon.Id)); } speciesList.Sort((lhs, rhs) => TaxonFormatter.GetString(lhs, false).CompareTo(TaxonFormatter.GetString(rhs, false))); foreach (ISpecies species in speciesList.Where(s => s.IsValid())) { subItems.Add(TaxonFormatter.GetString(species)); } } else { // Get all subtaxa under this taxon. IEnumerable <ITaxon> subtaxa = await Db.GetSubtaxaAsync(taxon); // Add all subtaxa to the list. foreach (ITaxon subtaxon in subtaxa) { if (subtaxon.Rank.Type == TaxonRankType.Species) { // Do not attempt to count sub-taxa for species. subItems.Add(subtaxon.GetName()); } else { // Count the number of species under this taxon. // Taxa with no species under them will not be displayed. long speciesCount = await Db.GetSpeciesCountAsync(subtaxon); if (speciesCount > 0) { // Count the sub-taxa under this taxon. long subtaxaCount = (await Db.GetSubtaxaAsync(subtaxon)).Count(); // Add the taxon to the list. if (subtaxaCount > 0) { subItems.Add(string.Format("{0} ({1})", subtaxon.GetName(), subtaxaCount)); } } } } } // Generate embed pages. string title = taxon.CommonNames.Count() <= 0 ? taxon.GetName() : string.Format("{0} ({1})", taxon.GetName(), taxon.GetCommonName()); string fieldTitle = string.Format("{0} in this {1} ({2}):", taxon.GetChildRank().GetName(true).ToTitle(), taxon.GetRank().GetName().ToLowerInvariant(), subItems.Count()); string thumbnailUrl = taxon.GetPictureUrl(); StringBuilder description = new StringBuilder(); description.AppendLine(taxon.GetDescriptionOrDefault()); if (subItems.Count() <= 0) { description.AppendLine(); description.AppendLine(string.Format("This {0} contains no {1}.", taxon.GetRank().GetName(), taxon.GetChildRank().GetName(true))); } List <Discord.Messaging.IEmbed> pages = new List <Discord.Messaging.IEmbed>(EmbedUtilities.CreateEmbedPages(fieldTitle, subItems, options: EmbedPaginationOptions.AddPageNumbers)); if (!pages.Any()) { pages.Add(new Discord.Messaging.Embed()); } IPaginatedMessage paginatedMessage = new PaginatedMessage(pages); foreach (Discord.Messaging.IEmbed page in paginatedMessage.Select(m => m.Embed)) { page.Title = title; page.ThumbnailUrl = thumbnailUrl; page.Description = description.ToString(); if (subItems.Count() > 0 && taxon.GetRank() != TaxonRankType.Genus) { page.Footer += string.Format(" — Empty {0} are not listed.", taxon.GetChildRank().GetName(true)); } } return(paginatedMessage); }