public async Task SetArtist(string genusName, string speciesName, int pictureIndex, string artist)
        {
            // Decrease the picture index by 1 (since users are expected to use the indices as shown by the "gallery" command, which begin at 1).
            --pictureIndex;

            Species species = await BotUtils.ReplyFindSpeciesAsync(Context, genusName, speciesName);

            if (species is null)
            {
                return;
            }

            Picture[] pictures = await SpeciesUtils.GetPicturesAsync(species);

            if (pictureIndex >= 0 && pictureIndex < pictures.Count())
            {
                Picture picture = pictures[pictureIndex];
                picture.artist = artist;

                await SpeciesUtils.AddPictureAsync(species, picture);

                await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully updated artist for {0} [picture]({1}) to **{2}**.",
                                                                         StringUtils.ToPossessive(species.ShortName),
                                                                         picture.url,
                                                                         artist));
            }
            else
            {
                await BotUtils.ReplyAsync_Error(Context, string.Format("**{0}** has no picture at this index.", species.ShortName));
            }
        }
        public async Task MinusPic(string genusName, string speciesName, int pictureIndex)
        {
            // Decrease the picture index by 1 (since users are expected to use the indices as shown by the "gallery" command, which begin at 1).
            --pictureIndex;

            if (await BotUtils.ReplyHasPrivilegeAsync(Context, PrivilegeLevel.ServerModerator))
            {
                // Get the species.

                Species species = await BotUtils.ReplyFindSpeciesAsync(Context, genusName, speciesName);

                if (species is null)
                {
                    return;
                }

                Picture[] pictures = await SpeciesUtils.GetPicturesAsync(species);

                Picture picture = (pictureIndex >= 0 && pictureIndex < pictures.Count()) ? pictures[pictureIndex] : null;

                // If the image was removed from anywhere (gallery or default species picture), this is set to "true".
                bool success = await SpeciesUtils.RemovePictureAsync(species, picture);

                if (success)
                {
                    await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully removed [picture]({1}) from **{0}**.", species.ShortName, picture.url));
                }
                else
                {
                    await BotUtils.ReplyAsync_Warning(Context, string.Format("**{0}** has no picture at this index.", species.ShortName));
                }
            }
        }
Esempio n. 3
0
        private static async Task <string[]> UploadSpeciesGalleryAsync(MediaWikiClient client, EditHistory history, Species species)
        {
            // Upload all images in the given species' gallery.

            Picture[] pictures = await SpeciesUtils.GetPicturesAsync(species);

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

            if (pictures != null)
            {
                foreach (Picture picture in pictures)
                {
                    // Skip the image if it's the same as the species' default image, because we would've already uploaded it

                    if (picture.url == species.Picture)
                    {
                        continue;
                    }

                    string uploadedFilename = GeneratePictureFilenameFromSpecies(species, picture.url);

                    if (!string.IsNullOrEmpty(uploadedFilename))
                    {
                        UploadParameters uploadParameters = new UploadParameters {
                            UploadFileName = uploadedFilename,
                            FilePath       = picture.url,
                            Text           = picture.description
                        };

                        uploadedFilename = await UploadPictureAsync(client, history, uploadParameters, true);

                        if (!string.IsNullOrEmpty(uploadedFilename))
                        {
                            uploadedFilenames.Add(uploadedFilename);
                        }
                    }
                    else
                    {
                        _log(string.Format("Failed to generate filename for picture: {0}", picture.url));
                    }
                }
            }

            return(uploadedFilenames.ToArray());
        }
        public async Task Gallery(string speciesOrTaxon)
        {
            // Prioritize species galleries first.

            Species[] species = await BotUtils.GetSpeciesFromDb("", speciesOrTaxon);

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

                Taxon taxon = await BotUtils.GetTaxonFromDb(speciesOrTaxon);

                if (taxon is null)
                {
                    // If no such taxon exists, show species recommendations to the user.
                    await BotUtils.ReplyAsync_SpeciesSuggestions(Context, "", speciesOrTaxon);
                }
                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 <Picture> pictures = new List <Picture>();

                    if (!string.IsNullOrEmpty(taxon.pics))
                    {
                        pictures.Add(new Picture(taxon.pics));
                    }

                    foreach (Species sp in await BotUtils.GetSpeciesInTaxonFromDb(taxon))
                    {
                        pictures.AddRange(await SpeciesUtils.GetPicturesAsync(sp));
                    }

                    await ShowGalleryAsync(Context, taxon.GetName(), pictures.ToArray());
                }
            }
        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());
                    }
                }
            }
        }