public async Task SetPic(string genusName, string speciesName, string imageUrl)
        {
            // Updates the default picture for the given species.
            // While any users can add pictures for species, only moderators can update the default picture.

            Species species = await BotUtils.ReplyFindSpeciesAsync(Context, genusName, speciesName);

            if (species is null)
            {
                return;
            }

            if (await BotUtils.ReplyHasPrivilegeOrOwnershipAsync(Context, PrivilegeLevel.ServerModerator, species) && await BotUtils.ReplyIsImageUrlValidAsync(Context, imageUrl))
            {
                Picture[] pictures = await GalleryUtils.GetPicturesAsync(await GalleryUtils.GetGalleryAsync(species));

                bool first_picture = pictures.Count() <= 0;

                await SpeciesUtils.SetPictureAsync(species, new Picture {
                    url    = imageUrl,
                    artist = first_picture ? species.OwnerName : Context.User.Username
                });

                await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully set the picture for **{0}**.", species.ShortName));
            }
        }
Ejemplo n.º 2
0
        public async Task SetMap(string primaryImageUrl, string labeledImageUrl)
        {
            // Create an image gallery for storing the map images if one hasn't been created yet.

            using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR IGNORE INTO Gallery(name) VALUES($name);")) {
                cmd.Parameters.AddWithValue("$name", MAP_GALLERY_NAME);

                await Database.ExecuteNonQuery(cmd);
            }

            Gallery gallery = await GalleryUtils.GetGalleryAsync(MAP_GALLERY_NAME);

            // Remove existing images from the gallery.

            using (SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Picture WHERE gallery_id = $gallery_id;")) {
                cmd.Parameters.AddWithValue("$gallery_id", gallery.id);

                await Database.ExecuteNonQuery(cmd);
            }

            // Insert the primary map image.

            if (!await BotUtils.ReplyIsImageUrlValidAsync(Context, primaryImageUrl))
            {
                return;
            }

            using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR REPLACE INTO Picture(url, gallery_id, name, artist) VALUES($url, $gallery_id, $name, $artist);")) {
                cmd.Parameters.AddWithValue("$url", primaryImageUrl);
                cmd.Parameters.AddWithValue("$gallery_id", gallery.id);
                cmd.Parameters.AddWithValue("$name", "primary");
                cmd.Parameters.AddWithValue("$artist", Context.User.Username);

                await Database.ExecuteNonQuery(cmd);
            }

            // Insert the secondary map image (although it does not necessarily have to be provided).

            if (!string.IsNullOrEmpty(labeledImageUrl))
            {
                if (!await BotUtils.ReplyIsImageUrlValidAsync(Context, labeledImageUrl))
                {
                    return;
                }

                using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR REPLACE INTO Picture(url, gallery_id, name, artist) VALUES($url, $gallery_id, $name, $artist);")) {
                    cmd.Parameters.AddWithValue("$url", labeledImageUrl);
                    cmd.Parameters.AddWithValue("$gallery_id", gallery.id);
                    cmd.Parameters.AddWithValue("$name", "labeled");
                    cmd.Parameters.AddWithValue("$artist", Context.User.Username);

                    await Database.ExecuteNonQuery(cmd);
                }
            }

            await BotUtils.ReplyAsync_Success(Context, "Successfully updated map images.");
        }
        public async Task PlusPic(string genusName, string speciesName, string imageUrl, string description)
        {
            // Get the species.

            Species species = await BotUtils.ReplyFindSpeciesAsync(Context, genusName, speciesName);

            if (species is null)
            {
                return;
            }

            // Validate the image URL.

            if (!await BotUtils.ReplyIsImageUrlValidAsync(Context, imageUrl))
            {
                return;
            }

            // Add the new picture to the gallery.

            // If this is the first picture we've added to the species, set the artist as the species' owner.
            // Otherwise, set the artist to the person submitting the image.

            Picture[] pictures = await GalleryUtils.GetPicturesAsync(await GalleryUtils.GetGalleryAsync(species));

            bool firstPicture         = pictures.Count() <= 0;
            bool pictureAlreadyExists = pictures.Any(x => x.url == imageUrl);

            Picture picture = pictures.Where(x => x.url == imageUrl).FirstOrDefault() ?? new Picture();

            picture.url         = imageUrl;
            picture.description = description;

            if (string.IsNullOrEmpty(picture.artist))
            {
                picture.artist = firstPicture ? species.OwnerName : Context.User.Username;
            }

            await SpeciesUtils.AddPictureAsync(species, picture);

            if (pictureAlreadyExists)
            {
                await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully updated [picture]({1}) for **{0}**.", species.ShortName, imageUrl));
            }
            else
            {
                await BotUtils.ReplyAsync_Success(Context, string.Format("Successfully added new [picture]({1}) for **{0}**.", species.ShortName, imageUrl));
            }
        }
Ejemplo n.º 4
0
        public async Task Map()
        {
            // Get map images from the database.

            Gallery gallery = await GalleryUtils.GetGalleryAsync(MAP_GALLERY_NAME);

            Picture primary = null;
            Picture labeled = null;

            if (!(gallery is null))
            {
                primary = await BotUtils.GetPicFromDb(gallery, "primary");

                labeled = await BotUtils.GetPicFromDb(gallery, "labeled");
            }

            // If no primary image has been provided, display an error message.

            if (primary is null)
            {
                await BotUtils.ReplyAsync_Error(Context, string.Format("No map images have been set. Use the \"{0}setmap\" command to set map images.",
                                                                       OurFoodChainBot.Instance.Config.Prefix));

                return;
            }

            // Build the embed.

            string worldName = OurFoodChainBot.Instance.Config.WorldName;
            string title     = string.IsNullOrEmpty(worldName) ? "" : string.Format("Map of {0}", StringUtils.ToTitleCase(worldName));
            string footer    = (labeled is null) ? "" : "Click the Z reaction to toggle zone labels.";

            Bot.PaginatedMessage paginatedMessage = new Bot.PaginatedMessage();

            // Add the first page (primary image without zone labels).

            paginatedMessage.Pages.Add(new EmbedBuilder {
                Title    = title,
                ImageUrl = primary.url,
                Footer   = new EmbedFooterBuilder {
                    Text = footer
                }
            }.Build());

            // A second page (with zone labels) is only included in the case an image has been provided.

            if (!(labeled is null))
            {
                paginatedMessage.Pages.Add(new EmbedBuilder {
                    Title    = title,
                    ImageUrl = labeled.url,
                    Footer   = new EmbedFooterBuilder {
                        Text = footer
                    }
                }.Build());
            }

            // Send the embed.

            paginatedMessage.PrevEmoji = string.Empty;
            paginatedMessage.NextEmoji = string.Empty;

            if (paginatedMessage.Pages.Count > 1)
            {
                paginatedMessage.ToggleEmoji = "🇿";
            }

            await Bot.DiscordUtils.SendMessageAsync(Context, paginatedMessage);
        }