Exemplo n.º 1
0
        public async Task GetZoneTypes()
        {
            IEnumerable <IZone> zones = await Db.GetZonesAsync();

            IEnumerable <IZoneType> zoneTypes = (await Db.GetZoneTypesAsync())
                                                .OrderBy(type => type.Name)
                                                .Where(type => zones.Any(zone => zone.TypeId == type.Id));

            if (zoneTypes.Any())
            {
                List <string> lines = new List <string>();

                foreach (IZoneType zoneType in zoneTypes)
                {
                    StringBuilder lineBuilder = new StringBuilder();

                    int zoneCount = zones.Where(zone => zone.TypeId == zoneType.Id).Count();

                    lineBuilder.Append($"{zoneType.Icon} {zoneType.Name.ToTitle().ToBold()} ({zoneCount})");

                    if (!string.IsNullOrWhiteSpace(zoneType.Description))
                    {
                        lineBuilder.Append("\t—\t");
                        lineBuilder.Append(zoneType.Description.GetFirstSentence());
                    }

                    lines.Add(lineBuilder.ToString().Truncate(DiscordUtilities.MaxEmbedLineLength - 2, true));
                }

                string description = $"For detailed type information, use `{Config.Prefix}zonetype <type>` (e.g. `{Config.Prefix}zonetype {zoneTypes.First().Name.ToLowerInvariant()}`).\n\n";

                IPaginatedMessage message = new PaginatedMessage();

                message.AddLines($"All Zone Types ({zoneTypes.Count()})", lines, columnsPerPage: 1, options: EmbedPaginationOptions.AddPageNumbers);

                foreach (IEmbed embed in message.Select(page => page.Embed))
                {
                    embed.Description = description + embed.Description;
                    embed.Footer     += " — Empty types are not listed.";
                }


                await ReplyAsync(message);
            }
            else
            {
                await ReplyInfoAsync("No zone types have been added.");
            }
        }
Exemplo n.º 2
0
        public async Task ReplyMatchingSpeciesAsync(IEnumerable <ISpecies> matchingSpecies)
        {
            IPaginatedMessage message = new PaginatedMessage();

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

            foreach (ISpecies species in matchingSpecies.OrderBy(species => species.GetFullName()))
            {
                lines.Add(species.GetFullName());
            }

            message.AddLines($"Matching species ({matchingSpecies.Count()})", lines, options: EmbedPaginationOptions.AddPageNumbers);

            await ReplyAsync(message);
        }
Exemplo n.º 3
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);

                foreach (DataRow row in await Db.GetRowsAsync(cmd))
                {
                    ISpecies sp = await Db.CreateSpeciesFromDataRowAsync(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 Db.GetScalarAsync <long>(cmd2);
                    }

                    lines.Add(sp.GetShortName() + (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
            {
                IPaginatedMessage message = new PaginatedMessage();

                message.AddLines(lines);
                message.SetTitle($"⭐ Species favorited by {Context.User.Username} ({lines.Count()})");
                message.SetThumbnailUrl(Context.User.GetAvatarUrl(size: 32));
                message.AddPageNumbers();

                await ReplyAsync(message);
            }
        }
Exemplo n.º 4
0
        private async Task <SQLiteDatabase> GetDatabaseFromDMAsync()
        {
            if (Config.SingleDatabase)
            {
                // If we're running in single-database mode, there is only one database to choose from.

                return(await DatabaseService.GetDatabaseAsync(Context.Guild));
            }
            else
            {
                // Find guilds that we share with the user.

                List <IGuild> sharedGuilds = new List <IGuild>();

                foreach (IGuild guild in await Context.Client.GetGuildsAsync())
                {
                    if (await guild.GetUserAsync(Context.User.Id) != null)
                    {
                        sharedGuilds.Add(guild);
                    }
                }

                // If the user has a DM guild set and is still in that guild, use it.

                IGuild userDMGuild = await GetUserDMGuildAsync(Context.User);

                if (userDMGuild != null && sharedGuilds.Any(guild => guild.Id == userDMGuild.Id))
                {
                    return(await DatabaseService.GetDatabaseAsync(userDMGuild));
                }
                else
                {
                    // The user does not have a DM guild set.

                    List <SQLiteDatabase> databases = new List <SQLiteDatabase>();

                    foreach (IGuild guild in sharedGuilds)
                    {
                        databases.Add(await DatabaseService.GetDatabaseAsync(guild));
                    }

                    if (databases.Count() == 1)
                    {
                        return(databases.First());
                    }
                    else if (databases.Count() > 0)
                    {
                        IPaginatedMessage message = new PaginatedMessage();

                        message.AddLines(string.Empty, sharedGuilds.Select(
                                             (guild, index) => $"`{(index + 1).ToString().PadLeft(sharedGuilds.Count().ToString().Length)}.` {guild.Name}"),
                                         options: EmbedPaginationOptions.Default | EmbedPaginationOptions.AddPageNumbers);

                        foreach (Discord.Messaging.IEmbed embed in message.Select(page => page.Embed))
                        {
                            embed.Description = $"You share more than one guild with {Bot.Name}. You can pick which guild you would like to interact with using the `{Config.Prefix}pickserver` command.\n\n"
                                                + embed.Description;
                        }

                        message.SetTitle($"Shared Guilds ({sharedGuilds.Count()})");

                        await ReplyAsync(message);

                        throw new Exception($"You must specify which guild you would like to interact with using the `{Config.Prefix}pickserver` command.");
                    }
                    else
                    {
                        throw new Exception($"You must share at least one guild with {Bot.Name}.");
                    }
                }
            }
        }