コード例 #1
0
        public async Task AddEmoji(CommandContext c, [Description("Name of the emoji to add")] string name,
                                   [Description("Optional name override")] string nameOverride = null)
        {
            if (c.Guild == null)
            {
                throw new InvalidOperationException("You cannot modify emojis in a DM.");
            }

            InteractivityModule i = c.Client.GetInteractivityModule();

            Emoji emoji = Emoji.FromName(name);

            if (emoji == null)
            {
                await c.RespondAsync("No emoji by that name was found on DiscordEmoji." +
                                     "\nPlease select a valid emoji from the catalog at https://discordemoji.com" +
                                     "\n\n(The emoji name is case sensitive. Don't include the colons in your command!)" +
                                     "\n\n If you're too lazy to go on the website, you can use the ``emojis`` command to list emojis." +
                                     $"\n``{EmojiButler.Configuration.Prefix}emojis <category> <page (Optional)>``");

                return;
            }

            if (!c.Channel.IsNSFW && emoji.GetCategoryName() == "NSFW")
            {
                await c.RespondAsync("Woah, that's an NSFW emoji. Use this command in an NSFW channel.");

                return;
            }

            string addedName = nameOverride ?? emoji.Title;

            var allEmoji = await c.Guild.GetEmojisAsync();

            if (allEmoji.Count(x => !x.IsAnimated) >= 50 && emoji.GetCategoryName() != "Animated")
            {
                await c.RespondAsync("It seems like you already have 50 emojis. That's the limit. Remove some before adding more.");

                return;
            }

            if (allEmoji.Count(x => x.IsAnimated) >= 50 && emoji.GetCategoryName() == "Animated")
            {
                await c.RespondAsync("It seems like you already have 50 *animated* emojis. That's the limit. Remove some before adding more.");

                return;
            }

            DiscordGuildEmoji conflictingEmoji = (allEmoji.FirstOrDefault(x => x.Name == addedName));

            if (conflictingEmoji != null)
            {
                DiscordEmbedBuilder overwrite = new DiscordEmbedBuilder
                {
                    Title       = "Overwrite Confirmation",
                    Description = $"An emoji that currently exists on this server that" +
                                  " has a conflicting name with the emoji that you are attempting to add.\nOverwrite? This will delete the emoji. React in less than 30 seconds to confirm.",
                    ThumbnailUrl = $"https://cdn.discordapp.com/emojis/{conflictingEmoji.Id}.png"
                };
                overwrite.AddField("Name", conflictingEmoji.Name);
                DiscordMessage overwriteConfirm = await c.RespondAsync(embed : overwrite);

                await overwriteConfirm.CreateReactionAsync(Reactions.YES);

                await overwriteConfirm.CreateReactionAsync(Reactions.NO);

                ReactionContext overwriteReact = await i.WaitForReactionAsync(x => x == Reactions.YES || x == Reactions.NO, c.User,
                                                                              TimeSpan.FromSeconds(30));

                await overwriteConfirm.DeleteAsync();

                if (overwriteReact != null)
                {
                    if (overwriteReact.Message == overwriteConfirm)
                    {
                        if (overwriteReact.Emoji == Reactions.NO)
                        {
                            await c.RespondAsync("Alright, I won't add the emoji.");

                            return;
                        }

                        try { await c.Guild.DeleteEmojiAsync(conflictingEmoji); }
                        catch (Exception e)
                        {
                            if (e is RatelimitTooHighException exr)
                            {
                                await c.RespondAsync($"I couldn't process the request due to the extreme ratelimits Discord has placed on emoji management. Try again in {(int)exr.RemainingTime.TotalMinutes} minute(s).");

                                return;
                            }
                            else if (e is NotFoundException) /*Emoji doesn't exist anymore, ignore.*/ } {
                    }
                }
                else
                {
                    await c.RespondAsync("You did not react to the original message. Aborting.");

                    return;
                }
            }
            else
            {
                await c.RespondAsync("No response was given. Aborting.");

                return;
            }
        }

        DiscordEmbedBuilder embed = new DiscordEmbedBuilder
        {
            Title       = "Confirmation",
            Description = "Are you sure that you want to add this emoji to your server?" +
                          "\nReact in less than 30 seconds to confirm.",
            ThumbnailUrl = emoji.Image
        };

        embed.AddField("Name", emoji.Title);

        if (nameOverride != null)
        {
            embed.AddField("Name Override", addedName);
        }

        embed.AddField("Author", emoji.Author);

        DiscordMessage m = await c.RespondAsync(embed : embed);

        await m.CreateReactionAsync(Reactions.YES);

        await m.CreateReactionAsync(Reactions.NO);

        ReactionContext react = await i.WaitForReactionAsync(x => x == Reactions.YES || x == Reactions.NO, c.User,
                                                             TimeSpan.FromSeconds(30));

        await m.DeleteAsync();

        if (react != null)
        {
            if (react.Message == m)
            {
                if (react.Emoji == Reactions.YES)
                {
                    DiscordMessage resp = await c.RespondAsync("Adding emoji...");

                    try
                    {
                        using (Stream s = await emoji.GetImageAsync())
                            await c.Guild.CreateEmojiAsync(addedName, s, null, $"Added by {c.User.Username}");
                    }
                    catch (Exception e)
                    {
                        if (e is BadRequestException)
                        {
                            await resp.ModifyAsync("I failed to upload the requested emoji to Discord. It was probably too big, or you selected an invalid name override.");

                            return;
                        }
                        else if (e is RatelimitTooHighException exr)
                        {
                            await c.RespondAsync($"I couldn't process the request due to the extreme ratelimits Discord has placed on emoji management. Try again in {(int)exr.RemainingTime.TotalMinutes} minute(s).");

                            return;
                        }
                    }

                    await resp.ModifyAsync("", new DiscordEmbedBuilder
                        {
                            Title        = "Success!",
                            Description  = $"You've added :{addedName}: to your server.",
                            ThumbnailUrl = emoji.Image
                        });
                }
                else if (react.Emoji == Reactions.NO)
                {
                    await c.RespondAsync("Okay then, I won't be adding that emoji.");
                }
            }
            else
            {
                await c.RespondAsync("You did not react to the original message. Aborting.");

                return;
            }
        }
        else
        {
            await c.RespondAsync("No response was given. Aborting.");
        }
    }