private async Task _displaySpeciesAddedBy(string username, string thumbnailUrl, List <Species> speciesList)
        {
            if (speciesList.Count() <= 0)
            {
                await BotUtils.ReplyAsync_Info(Context, string.Format("**{0}** has not submitted any species yet.", username));
            }
            else
            {
                Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder(EmbedUtils.SpeciesListToEmbedPages(speciesList,
                                                                                                                       fieldName: string.Format("Species owned by {0} ({1})", username, speciesList.Count())));

                embed.SetThumbnailUrl(thumbnailUrl);

                await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build());
            }
        }
Пример #2
0
        public async Task Favs()
        {
            // Get all species fav'd by this user.

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

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Species WHERE id IN (SELECT species_id FROM Favorites WHERE user_id = $user_id);")) {
                cmd.Parameters.AddWithValue("$user_id", Context.User.Id);

                using (DataTable rows = await Database.GetRowsAsync(cmd)) {
                    foreach (DataRow row in rows.Rows)
                    {
                        Species sp = await SpeciesUtils.SpeciesFromDataRow(row);

                        long fav_count = 0;

                        // Get the number of times this species has been favorited.

                        using (SQLiteCommand cmd2 = new SQLiteCommand("SELECT COUNT(*) FROM Favorites WHERE species_id = $species_id;")) {
                            cmd2.Parameters.AddWithValue("$species_id", sp.Id);

                            fav_count = await Database.GetScalar <long>(cmd2);
                        }

                        lines.Add(sp.ShortName + (fav_count > 1 ? string.Format(" (+{0})", fav_count) : ""));
                    }

                    lines.Sort();
                }
            }

            // Display the species list.

            if (lines.Count() <= 0)
            {
                await BotUtils.ReplyAsync_Info(Context, string.Format("**{0}** has not favorited any species.", Context.User.Username));
            }
            else
            {
                Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder(EmbedUtils.LinesToEmbedPages(lines));
                embed.SetTitle(string.Format("⭐ Species favorited by {0} ({1})", Context.User.Username, lines.Count()));
                embed.SetThumbnailUrl(Context.User.GetAvatarUrl(size: 32));
                embed.AddPageNumbers();

                await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build());
            }
        }
        public async Task Zone(string arg0 = "")
        {
            ZoneType zone_type = await ZoneUtils.GetZoneTypeAsync(arg0);

            if (string.IsNullOrEmpty(arg0) || ZoneUtils.ZoneTypeIsValid(zone_type))
            {
                // Display all zones, or, if the user passed in a valid zone type, all zones of that type.

                Zone[] zones = await ZoneUtils.GetZonesAsync(zone_type);

                if (zones.Count() > 0)
                {
                    // We need to make sure that even if the "short" description is actually long, we can show n zones per page.

                    Bot.PaginatedMessageBuilder embed = new Bot.PaginatedMessageBuilder {
                        Title       = StringUtils.ToTitleCase(string.Format("{0} zones ({1})", string.IsNullOrEmpty(arg0) ? "All" : arg0, zones.Count())),
                        Description = string.Format("For detailed zone information, use `{0}zone <zone>` (e.g. `{0}zone {1}`).\n\n",
                                                    OurFoodChainBot.Instance.Config.Prefix,
                                                    zones[0].ShortName.Contains(" ") ? string.Format("\"{0}\"", zones[0].ShortName.ToLower()) : zones[0].ShortName.ToLower())
                    };

                    // Build paginated message.

                    await BotUtils.ZonesToEmbedPagesAsync(embed, zones);

                    embed.AddPageNumbers();

                    if (ZoneUtils.ZoneTypeIsValid(zone_type))
                    {
                        embed.SetColor(Bot.DiscordUtils.ConvertColor(zone_type.Color));
                    }

                    await Bot.DiscordUtils.SendMessageAsync(Context, embed.Build());
                }
                else
                {
                    await BotUtils.ReplyAsync_Info(Context, "No zones have been added yet.");
                }

                return;
            }
            else
            {
                Zone zone = await ZoneUtils.GetZoneAsync(arg0);

                if (await BotUtils.ReplyValidateZoneAsync(Context, zone))
                {
                    List <Embed> pages = new List <Embed>();

                    ZoneType type = await ZoneUtils.GetZoneTypeAsync(zone.ZoneTypeId) ?? new ZoneType();

                    string title       = string.Format("{0} {1}", type.Icon, zone.FullName);
                    string description = zone.GetDescriptionOrDefault();
                    Color  color       = Bot.DiscordUtils.ConvertColor(type.Color);

                    // Get all species living in this zone.

                    List <Species> species_list = new List <Species>(await BotUtils.GetSpeciesFromDbByZone(zone));

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

                    // Starting building a paginated message.
                    // The message will have a paginated species list, and a toggle button to display the species sorted by role.

                    List <EmbedBuilder>         embed_pages = EmbedUtils.SpeciesListToEmbedPages(species_list, fieldName: (string.Format("Extant species in this zone ({0}):", species_list.Count())));
                    Bot.PaginatedMessageBuilder paginated   = new Bot.PaginatedMessageBuilder(embed_pages);

                    if (embed_pages.Count() <= 0)
                    {
                        embed_pages.Add(new EmbedBuilder());
                    }

                    // Add title, decription, etc., to all pages.

                    paginated.SetTitle(title);
                    paginated.SetDescription(description);
                    paginated.SetThumbnailUrl(zone.Pics);
                    paginated.SetColor(color);

                    // This page will have species organized by role.
                    // Only bother with the role page if species actually exist in this zone.

                    if (species_list.Count() > 0)
                    {
                        EmbedBuilder role_page = new EmbedBuilder();

                        role_page.WithTitle(title);
                        role_page.WithDescription(description);
                        //role_page.WithThumbnailUrl(zone.pics);
                        role_page.WithColor(color);

                        Dictionary <string, List <Species> > roles_map = new Dictionary <string, List <Species> >();

                        foreach (Species sp in species_list)
                        {
                            Role[] roles_list = await SpeciesUtils.GetRolesAsync(sp);

                            if (roles_list.Count() <= 0)
                            {
                                if (!roles_map.ContainsKey("no role"))
                                {
                                    roles_map["no role"] = new List <Species>();
                                }

                                roles_map["no role"].Add(sp);

                                continue;
                            }

                            foreach (Role role in roles_list)
                            {
                                if (!roles_map.ContainsKey(role.name))
                                {
                                    roles_map[role.name] = new List <Species>();
                                }

                                roles_map[role.name].Add(sp);
                            }
                        }

                        // Sort the list of species belonging to each role.

                        foreach (List <Species> i in roles_map.Values)
                        {
                            i.Sort((lhs, rhs) => lhs.ShortName.CompareTo(rhs.ShortName));
                        }

                        // Create a sorted list of keys so that the roles are in order.

                        List <string> sorted_keys = new List <string>(roles_map.Keys);
                        sorted_keys.Sort();

                        foreach (string i in sorted_keys)
                        {
                            StringBuilder lines = new StringBuilder();

                            foreach (Species j in roles_map[i])
                            {
                                lines.AppendLine(j.ShortName);
                            }

                            role_page.AddField(string.Format("{0}s ({1})", StringUtils.ToTitleCase(i), roles_map[i].Count()), lines.ToString(), inline: true);
                        }

                        // Add the page to the builder.

                        paginated.AddReaction("🇷");
                        paginated.SetCallback(async(args) => {
                            if (args.Reaction != "🇷")
                            {
                                return;
                            }

                            args.PaginatedMessage.PaginationEnabled = !args.ReactionAdded;

                            if (args.ReactionAdded)
                            {
                                await args.DiscordMessage.ModifyAsync(msg => msg.Embed = role_page.Build());
                            }
                            else
                            {
                                await args.DiscordMessage.ModifyAsync(msg => msg.Embed = args.PaginatedMessage.Pages[args.PaginatedMessage.PageIndex]);
                            }
                        });
                    }

                    await Bot.DiscordUtils.SendMessageAsync(Context, paginated.Build());
                }
            }
        }