Example #1
0
        public async Task Prey(string genus, string species = "")
        {
            // If no species argument was provided, assume the user omitted the genus.
            if (string.IsNullOrEmpty(species))
            {
                species = genus;
                genus   = string.Empty;
            }

            // Get the specified species.

            Species sp = await BotUtils.ReplyFindSpeciesAsync(Context, genus, species);

            if (sp is null)
            {
                return;
            }

            // Get the preyed-upon species.

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

                using (DataTable rows = await Database.GetRowsAsync(cmd)) {
                    if (rows.Rows.Count <= 0)
                    {
                        await BotUtils.ReplyAsync_Info(Context, string.Format("**{0}** does not prey upon any other species.", sp.ShortName));
                    }
                    else
                    {
                        List <Tuple <Species, string> > prey_list = new List <Tuple <Species, string> >();

                        foreach (DataRow row in rows.Rows)
                        {
                            prey_list.Add(new Tuple <Species, string>(
                                              await BotUtils.GetSpeciesFromDb(row.Field <long>("eats_id")),
                                              row.Field <string>("notes")));
                        }

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

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

                        foreach (Tuple <Species, string> prey in prey_list)
                        {
                            string line = prey.Item1.IsExtinct ? BotUtils.Strikeout(prey.Item1.ShortName) : prey.Item1.ShortName;

                            if (!string.IsNullOrEmpty(prey.Item2))
                            {
                                line += (string.Format(" ({0})", prey.Item2.ToLower()));
                            }

                            lines.Add(line);
                        }

                        PaginatedMessageBuilder embed = new PaginatedMessageBuilder();

                        embed.AddPages(EmbedUtils.LinesToEmbedPages(lines));

                        embed.SetTitle(string.Format("Species preyed upon by {0} ({1})", sp.ShortName, prey_list.Count()));
                        embed.AddPageNumbers();

                        await DiscordUtils.SendMessageAsync(Context, embed.Build());
                    }
                }
            }
        }
        public static async Task ShowSpeciesInfoAsync(ICommandContext context, Species species)
        {
            if (await BotUtils.ReplyValidateSpeciesAsync(context, species))
            {
                EmbedBuilder  embed = new EmbedBuilder();
                StringBuilder descriptionBuilder = new StringBuilder();

                string embed_title = species.FullName;
                Color  embed_color = Color.Blue;

                CommonName[] common_names = await SpeciesUtils.GetCommonNamesAsync(species);

                if (common_names.Count() > 0)
                {
                    embed_title += string.Format(" ({0})", string.Join(", ", (object[])common_names));
                }

                // Show generation only if generations are enabled.

                if (OurFoodChainBot.Instance.Config.GenerationsEnabled)
                {
                    Generation gen = await GenerationUtils.GetGenerationByTimestampAsync(species.Timestamp);

                    embed.AddField("Gen", gen is null ? "???" : gen.Number.ToString(), inline: true);
                }

                embed.AddField("Owner", await SpeciesUtils.GetOwnerOrDefaultAsync(species, context), inline: true);

                SpeciesZone[] zone_list = await SpeciesUtils.GetZonesAsync(species);

                if (zone_list.Count() > 0)
                {
                    embed_color = Bot.DiscordUtils.ConvertColor((await ZoneUtils.GetZoneTypeAsync(zone_list
                                                                                                  .GroupBy(x => x.Zone.ZoneTypeId)
                                                                                                  .OrderBy(x => x.Count())
                                                                                                  .Last()
                                                                                                  .Key)).Color);
                }

                string zones_value = new SpeciesZoneCollection(zone_list).ToString(SpeciesZoneCollectionToStringOptions.Default, Bot.DiscordUtils.MaxFieldLength);

                embed.AddField("Zone(s)", string.IsNullOrEmpty(zones_value) ? "None" : zones_value, inline: true);

                // Check if the species is extinct.
                using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Extinctions WHERE species_id=$species_id;")) {
                    cmd.Parameters.AddWithValue("$species_id", species.Id);

                    DataRow row = await Database.GetRowAsync(cmd);

                    if (!(row is null))
                    {
                        embed_title = "[EXTINCT] " + embed_title;
                        embed_color = Color.Red;

                        string reason    = row.Field <string>("reason");
                        long   timestamp = (long)row.Field <decimal>("timestamp");

                        if (!string.IsNullOrEmpty(reason))
                        {
                            descriptionBuilder.AppendLine(string.Format("**Extinct ({0}):** _{1}_\n", await BotUtils.TimestampToDateStringAsync(timestamp), reason));
                        }
                    }
                }

                descriptionBuilder.Append(species.GetDescriptionOrDefault());

                embed.WithTitle(embed_title);
                embed.WithThumbnailUrl(species.Picture);
                embed.WithColor(embed_color);

                if (!string.IsNullOrEmpty(OurFoodChainBot.Instance.Config.WikiUrlFormat))
                {
                    // Discord automatically encodes certain characters in URIs, which doesn't allow us to update the config via Discord when we have "{0}" in the URL.
                    // Replace this with the proper string before attempting to call string.Format.
                    string format = OurFoodChainBot.Instance.Config.WikiUrlFormat.Replace("%7B0%7D", "{0}");

                    embed.WithUrl(string.Format(format, Uri.EscapeUriString(GetWikiPageTitleForSpecies(species, common_names))));
                }

                if (embed.Length + descriptionBuilder.Length > DiscordUtils.MaxEmbedLength)
                {
                    // If the description puts us over the character limit, we'll paginate.

                    int pageLength = DiscordUtils.MaxEmbedLength - embed.Length;

                    List <EmbedBuilder> pages = new List <EmbedBuilder>();

                    foreach (string pageText in new StringPaginator(descriptionBuilder.ToString())
                    {
                        MaxPageLength = pageLength
                    })
                    {
                        EmbedBuilder page = new EmbedBuilder();

                        page.WithTitle(embed.Title);
                        page.WithThumbnailUrl(embed.ThumbnailUrl);
                        page.WithFields(embed.Fields);
                        page.WithDescription(pageText);

                        pages.Add(page);
                    }

                    PaginatedMessageBuilder builder = new Bot.PaginatedMessageBuilder(pages);
                    builder.AddPageNumbers();
                    builder.SetColor(embed_color);

                    await DiscordUtils.SendMessageAsync(context, builder.Build());
                }
                else
                {
                    embed.WithDescription(descriptionBuilder.ToString());

                    await context.Channel.SendMessageAsync("", false, embed.Build());
                }
            }
        }