public async Task Leaderboard()
        {
            UserRank[] userRanks = await UserUtils.GetRanksAsync();

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

            foreach (UserRank userRank in userRanks)
            {
                IUser user = Context.Guild is null ? null : await Context.Guild.GetUserAsync(userRank.User.Id);

                lines.Add(string.Format("**`{0}`**{1}`{2}` {3}",
                                        string.Format("{0}.", userRank.Rank.ToString("000")),
                                        userRank.Icon,
                                        userRank.User.SubmissionCount.ToString("000"),
                                        string.Format(userRank.Rank <= 3 ? "**{0}**" : "{0}", user is null ? userRank.User.Username : user.Username)
                                        ));
            }

            // Create the embed.

            Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder();
            embed.AddPages(EmbedUtils.LinesToEmbedPages(lines));
            embed.SetTitle(string.Format("🏆 Leaderboard ({0})", lines.Count));
            embed.SetColor(255, 204, 77);
            embed.AddPageNumbers();

            // Send the embed.
            await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build());
        }
        public async Task Extinct()
        {
            List <Species> sp_list = new List <Species>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id IN (SELECT species_id FROM Extinctions);"))
                using (DataTable rows = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in rows.Rows)
                    {
                        sp_list.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }

            sp_list.Sort((lhs, rhs) => lhs.ShortName.CompareTo(rhs.ShortName));

            Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder();
            embed.AddPages(EmbedUtils.SpeciesListToEmbedPages(sp_list, fieldName: string.Format("Extinct species ({0})", sp_list.Count()), flags: EmbedPagesFlag.None));

            await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build(), "There are currently no extinct species.");
        }
        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());
                    }
                }
            }
        }
示例#4
0
        public static async Task <Bot.PaginatedMessageBuilder> BuildRecentEventsEmbedAsync(long startTimestamp, long endTimestamp, TimeUnits timeUnit = 0)
        {
            // Get all species created within the given timespan.

            List <Species> new_species = new List <Species>();
            TimeAmount     time_amount = new TimeAmount(endTimestamp - startTimestamp, TimeUnits.Seconds);

            if (timeUnit != 0)
            {
                time_amount = time_amount.ConvertTo(timeUnit);
            }
            else
            {
                time_amount = time_amount.Reduce();
            }

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE timestamp >= $start_ts AND timestamp < $end_ts")) {
                cmd.Parameters.AddWithValue("$start_ts", startTimestamp);
                cmd.Parameters.AddWithValue("$end_ts", endTimestamp);

                using (DataTable table = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in table.Rows)
                    {
                        new_species.Add(await SpeciesUtils.SpeciesFromDataRow(row));
                    }
            }

            new_species.Sort();

            // Get all extinctions that occurred recently.

            List <Species> extinct_species = new List <Species>();

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Extinctions WHERE timestamp >= $start_ts AND timestamp < $end_ts")) {
                cmd.Parameters.AddWithValue("$start_ts", startTimestamp);
                cmd.Parameters.AddWithValue("$end_ts", endTimestamp);

                using (DataTable table = await Database.GetRowsAsync(cmd))
                    foreach (DataRow row in table.Rows)
                    {
                        extinct_species.Add(await BotUtils.GetSpeciesFromDb(row.Field <long>("species_id")));
                    }
            }

            extinct_species.Sort();

            // Build embed.

            Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder();
            List <EmbedBuilder>         pages = new List <EmbedBuilder>();
            List <string> field_lines         = new List <string>();

            if (new_species.Count() > 0)
            {
                foreach (Species sp in new_species)
                {
                    field_lines.Add(sp.FullName);
                }

                EmbedUtils.AddLongFieldToEmbedPages(pages, field_lines, fieldName: string.Format("New species ({0})", new_species.Count()));

                field_lines.Clear();
            }

            if (extinct_species.Count() > 0)
            {
                foreach (Species sp in extinct_species)
                {
                    field_lines.Add(sp.FullName);
                }

                EmbedUtils.AddLongFieldToEmbedPages(pages, field_lines, fieldName: string.Format("Extinctions ({0})", extinct_species.Count()));

                field_lines.Clear();
            }

            embed.AddPages(pages);

            embed.SetTitle(string.Format("Recent events ({0})", time_amount.ToString()));
            embed.SetFooter(string.Empty); // remove page numbers added automatically
            embed.AddPageNumbers();

            if (embed.FieldCount <= 0)
            {
                embed.SetDescription("No events");
            }

            return(embed);
        }