Beispiel #1
0
        public async Task GetZoneType(string arg0)
        {
            if (!string.IsNullOrEmpty(arg0))
            {
                // If the given argument is a zone type, display information for that type.
                // If the given argument is a zone name, display information for the type corresponding to that zone.

                IZoneType type = await Db.GetZoneTypeAsync(arg0);

                if (!type.IsValid())
                {
                    // If no zone type exists with this name, attempt to get the type of the zone with this name.

                    IZone zone = await Db.GetZoneAsync(arg0);

                    if (zone != null)
                    {
                        type = await Db.GetZoneTypeAsync(zone.TypeId);
                    }
                }

                if (type.IsValid())
                {
                    // We got a valid zone type, so show information about the zone type.

                    IEnumerable <IZone> zones = await Db.GetZonesAsync(type);

                    string embedTitle       = string.Format("{0} {1} Zones ({2})", type.Icon, type.Name, zones.Count()).ToTitle();
                    string embedDescription = type.Description + "\n\n";

                    IEnumerable <IEmbed> pages = await BotUtils.ZonesToEmbedPagesAsync(embedTitle.Length + embedDescription.Length, zones, Db, showIcon : false);

                    foreach (IEmbed page in pages)
                    {
                        page.Description = embedDescription + page.Description;
                    }

                    IPaginatedMessage message = new PaginatedMessage(pages);

                    message.SetTitle(embedTitle);
                    message.SetColor(type.Color);

                    await ReplyAsync(message);
                }
                else
                {
                    await ReplyErrorAsync("No such zone type exists.");
                }
            }
            else
            {
                await GetZoneTypes();
            }
        }
Beispiel #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);

                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);
            }
        }
Beispiel #3
0
        // Private members

        private async Task ShowZonesAsync(IEnumerable <IZone> zones, IZoneType 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.

                string embedTitle       = string.Format("{0} zones ({1})", type.IsValid() ? type.Name : "All", zones.Count()).ToTitle();
                string embedDescription = string.Format("For detailed zone information, use `{0}zone <zone>` (e.g. `{0}zone {1}`).\n\n",
                                                        Config.Prefix,
                                                        zones.First().GetShortName().Contains(" ") ? string.Format("\"{0}\"", zones.First().GetShortName().ToLowerInvariant()) : zones.First().GetShortName().ToLowerInvariant());

                // Build paginated message.

                IEnumerable <IEmbed> pages = await BotUtils.ZonesToEmbedPagesAsync(embedTitle.Length + embedDescription.Length, zones, Db);

                foreach (IEmbed page in pages)
                {
                    page.Description = embedDescription + page.Description;
                }

                IPaginatedMessage message = new PaginatedMessage(pages);

                message.SetTitle(embedTitle);

                if (type.IsValid())
                {
                    message.SetColor(type.Color);
                }

                await ReplyAsync(message);
            }
            else
            {
                await ReplyInfoAsync("No zones have been added yet.");
            }
        }
        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}.");
                    }
                }
            }
        }