コード例 #1
0
        public async Task Gallery([Remainder] string arg0)
        {
            arg0 = StringUtilities.StripOuterQuotes(arg0);

            // Possible cases:
            // 1. <species>
            // 2. <taxon>

            // Prioritize species galleries first.

            IEnumerable <ISpecies> matchingSpecies = await Db.GetSpeciesAsync(arg0);

            if (matchingSpecies.Count() <= 0)
            {
                // No such species exists, so check if a taxon exists.

                ITaxon taxon = (await Db.GetTaxaAsync(arg0)).FirstOrDefault();

                if (!taxon.IsValid())
                {
                    // If no such taxon exists, show species recommendations to the user.

                    ISpecies species = await ReplySpeciesSuggestionAsync(string.Empty, arg0);

                    if (species.IsValid())
                    {
                        await ShowGalleryAsync(species);
                    }
                }
                else
                {
                    // The taxon does exist, so we'll generate a gallery from this taxon.
                    // First, images for this taxon will be added, followed by the galleries for all species under it.

                    List <IPicture> pictures = new List <IPicture>();

                    pictures.AddRange(taxon.Pictures);

                    foreach (ISpecies species in await Db.GetSpeciesAsync(taxon))
                    {
                        pictures.AddRange(await Db.GetPicturesAsync(species));
                    }

                    await ReplyGalleryAsync(taxon.GetName(), pictures);
                }
            }
            else
            {
                // We got one or more matching species.

                ISpecies species = await ReplyValidateSpeciesAsync(matchingSpecies);

                if (species.IsValid())
                {
                    await ShowGalleryAsync(species);
                }
            }
        }
コード例 #2
0
        public override string GetString(ITaxon taxon)
        {
            if (taxon is ISpecies species)
            {
                return(species.BinomialName.ToString(NameFormat));
            }

            return(taxon.GetName());
        }
コード例 #3
0
        private async Task ReplySetTaxonDescriptionAsync(ITaxon taxon, string description)
        {
            if (taxon.IsValid())
            {
                taxon.Description = description;

                await Db.UpdateTaxonAsync(taxon);

                string name = (taxon is ISpecies species) ? species.GetShortName() : taxon.GetName();

                await ReplySuccessAsync($"Successfully updated description for {taxon.GetRank().GetName()} **{name}**.");
            }
        }
コード例 #4
0
ファイル: IdeaModule.cs プロジェクト: gsemac/OurFoodChain.Bot
        private async Task <IEnumerable <string> > GetSmallGenusIdeasAsync()
        {
            // Checks for genera with only one species

            List <string> ideas = new List <string>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Genus WHERE id IN (SELECT genus_id FROM(SELECT genus_id, COUNT(genus_id) AS c FROM (SELECT * FROM Species WHERE id NOT IN (SELECT species_id FROM Extinctions)) GROUP BY genus_id) WHERE c <= 1)")) {
                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    ITaxon taxon = await Db.CreateTaxonFromDataRowAsync(row, TaxonRankType.Genus);

                    ideas.Add($"Genus {taxon.GetName().ToBold()} only has one species in it. Why not make another?");
                }
            }

            return(ideas);
        }
コード例 #5
0
        private async Task ReplyAddTaxonAsync(TaxonRankType rank, string taxonName, string description)
        {
            // Make sure that the taxon does not already exist before trying to add it.

            ITaxon taxon = (await Db.GetTaxaAsync(taxonName, rank)).FirstOrDefault();

            if (taxon.IsValid())
            {
                await ReplyWarningAsync($"The {rank.GetName()} **{taxon.GetName()}** already exists.");
            }
            else
            {
                taxon = new Common.Taxa.Taxon(rank, taxonName)
                {
                    Name        = taxonName,
                    Description = description
                };

                await Db.AddTaxonAsync(taxon);

                await ReplySuccessAsync($"Successfully created new {rank.GetName()}, **{taxon.GetName()}**.");
            }
        }
コード例 #6
0
        private async Task ReplyDeleteTaxonAsync(ITaxon taxon)
        {
            if (taxon.IsValid())
            {
                if (taxon.GetRank() != TaxonRankType.Species && (await Db.GetSpeciesAsync(taxon)).Count() > 0)
                {
                    // If the taxon still has species underneath of it, don't allow it to be deleted.

                    await ReplyErrorAsync("Taxa containing species cannot be deleted.");
                }
                else if (taxon.GetRank() != TaxonRankType.Species)
                {
                    // The taxon is empty, so delete the taxon.

                    await Db.DeleteTaxonAsync(taxon);

                    string name = (taxon is ISpecies species) ? species.GetShortName() : taxon.GetName();

                    await ReplySuccessAsync($"{taxon.GetRank().GetName().ToSentence()} **{name}** was successfully deleted.");
                }
                else
                {
                    // Species cannot currently be deleted through the bot.

                    await ReplyErrorAsync("Species cannot currently be deleted.");
                }
            }
        }
コード例 #7
0
        private async Task ReplySetTaxonParentAsync(TaxonRankType rank, string childTaxonName, string parentTaxonName)
        {
            ITaxon child = await GetTaxonOrReplyAsync(rank.GetChildRank(), childTaxonName);

            ITaxon parent = child.IsValid() ? await GetTaxonOrReplyAsync(rank, parentTaxonName) : null;

            if (child.IsValid() && parent.IsValid())
            {
                child.ParentId = parent.Id;

                await Db.UpdateTaxonAsync(child);

                await ReplySuccessAsync($"{child.GetRank().GetName().ToSentence()} **{child.GetName().ToTitle()}** has sucessfully been placed under the {parent.GetRank().GetName()} **{parent.GetName().ToTitle()}**.");
            }
        }
コード例 #8
0
        private async Task ReplySetTaxonCommonNameAsync(ITaxon taxon, string commonName)
        {
            if (taxon.IsValid())
            {
                taxon.CommonNames.Clear();
                taxon.CommonNames.Add(commonName);

                await Db.UpdateTaxonAsync(taxon);

                if (taxon is ISpecies species)
                {
                    await ReplySuccessAsync($"{species.GetShortName().ToBold()} is now commonly known as the {species.GetCommonName().ToTitle().ToBold()}.");
                }
                else
                {
                    await ReplySuccessAsync($"Members of the {taxon.GetRank().GetName()} {taxon.GetName().ToTitle().ToBold()} are now commonly known as {taxon.GetCommonName().ToTitle().ToBold()}.");
                }
            }
        }
コード例 #9
0
        private async Task ReplySetTaxonPictureAsync(TaxonRankType rank, string taxonName, string imageUrl)
        {
            ITaxon taxon = await GetTaxonOrReplyAsync(rank, taxonName);

            if (taxon.IsValid() && await ReplyValidateImageUrlAsync(imageUrl))
            {
                taxon.Pictures.Clear();
                taxon.Pictures.Add(new Picture(imageUrl));

                await Db.UpdateTaxonAsync(taxon);

                await ReplySuccessAsync($"Successfully set the picture for for {taxon.GetRank().GetName()} **{taxon.GetName().ToTitle()}**.");
            }
        }
コード例 #10
0
        public async Task SetGenus(string genusName, string speciesName, string newGenusName)
        {
            // Get the specified species.

            ISpecies species = await GetSpeciesOrReplyAsync(genusName, speciesName);

            if (species.IsValid())
            {
                // Get the specified genus.

                ITaxon genus = await GetTaxonOrReplyAsync(TaxonRankType.Genus, newGenusName);

                if (genus.IsValid())
                {
                    // Update the species.

                    species.Genus = genus;

                    await Db.UpdateSpeciesAsync(species);

                    await ReplySuccessAsync($"**{species.GetShortName()}** has successfully been assigned to the genus **{genus.GetName().ToTitle()}**.");
                }
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        public async Task <ITaxon> ReplyNoSuchTaxonExistsAsync(string input, ITaxon suggestion, TaxonRankType rank = TaxonRankType.None)
        {
            string taxonName = rank == TaxonRankType.None ? "taxon" : rank.GetName();

            if (suggestion != null)
            {
                taxonName = suggestion.GetRank().GetName();
            }

            StringBuilder sb = new StringBuilder();

            if (string.IsNullOrWhiteSpace(input))
            {
                sb.Append($"No such {taxonName} exists.");
            }
            else
            {
                sb.Append($"No {taxonName} named \"{input}\" exists.");
            }

            if (suggestion != null)
            {
                string suggestionText = (suggestion is ISpecies species) ? species.GetFullName() : suggestion.GetName().ToTitle();

                sb.Append($" Did you mean **{suggestionText}**?");
            }

            IPaginatedMessage message = new PaginatedMessage(sb.ToString())
            {
                Restricted = true
            };

            if (suggestion != null)
            {
                bool confirmed = false;

                message.AddReaction(PaginatedMessageReactionType.Yes, async(args) => {
                    confirmed = true;

                    await Task.CompletedTask;
                });

                await ReplyAndWaitAsync(message);

                if (!confirmed)
                {
                    suggestion = null;
                }
            }
            else
            {
                await ReplyAsync(message);
            }

            return(suggestion);
        }