Пример #1
0
        public async Task AddEmoji(string name, string nameOverride = null)
        {
            var emoji = DiscordEmoji.EmoteFromName(name);

            if (emoji == null)
            {
                await ReplyAsync("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``{Configuration.Prefix}emojis <category> <page (Optional)>``");

                return;
            }

            if (!(Context.Channel as ITextChannel).IsNsfw && DiscordEmoji.GetCategoryName(emoji.Category) == "NSFW")
            {
                await ReplyAsync("That's an NSFW emoji, go into an NSFW channel for that.");

                return;
            }

            var guildEmoji = Context.Guild.Emotes;

            if (guildEmoji.Count(x => x.Animated) >= 50 && emoji.Animated)
            {
                await ReplyAsync("You already have fifty (50) animated emoji on this server. That is Discord's limit. Remove some before adding more.");

                return;
            }

            if (guildEmoji.Count(x => !x.Animated) >= 50 && !emoji.Animated)
            {
                await ReplyAsync("You already have fifty (50) emoji on this server. That is Discord's limit. Remove some before adding more.");

                return;
            }

            EmbedBuilder embed = new EmbedBuilder
            {
                Title        = "Confirmation",
                Description  = "Are you sure that you want to add this emoji to your server?\nReact with ✅ in less than 20 seconds to confirm. React with ❌ to abort.",
                ThumbnailUrl = emoji.Image
            };

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

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

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

            SocketReaction reaction;

            try
            {
                var sent = await ReplyAsync(embed : embed.Build());

                ChoiceTracker.AddUser(Context.User.Id);

                var t = Task.Run(async() => await sent.AddReactionsAsync(new[] { reactionYes, reactionNo }));

                reaction = await ReactionCollector.GrabReaction(Context.User, sent,
                                                                x => x.Emote.Equals(reactionYes) || x.Emote.Equals(reactionNo), 20);
            }
            finally
            {
                ChoiceTracker.RemoveUser(Context.User.Id);
            }


            if (reaction == null)
            {
                await ReplyAsync("No reaction was given, aborting.");

                return;
            }

            if (reaction.Emote.Equals(reactionYes))
            {
                var successMessage = await ReplyAsync("Hold on while I add your emoji...");

                try
                {
                    using (Stream image = await DiscordEmoji.GetImageAsync(emoji))
                    {
                        // Check for Discord's 256kb emoji size cap
                        if (image.Length / 1024 > 256)
                        {
                            await ReplyAsync("The selected emoji is above 256kb and cannot be uploaded to Discord. Go bother Kohai (DiscordEmoji developer) to fix it.");

                            return;
                        }

                        await Context.Guild.CreateEmoteAsync(nameOverride ?? emoji.Title, new Image(image), options : new RequestOptions
                        {
                            AuditLogReason = $"Emoji added by {Context.User.Username}#{Context.User.Discriminator}",
                            RetryMode      = RetryMode.AlwaysFail // Will fail on too long of a ratelimit so the bot doesn't hang on emoji upload ratelimit
                        });
                    }

                    var addMessage = $"Cool beans, you've added {emoji.Title} to your server.";

                    if (nameOverride != null)
                    {
                        addMessage += $" Name override: {nameOverride}";
                    }

                    await successMessage.ModifyAsync(x => x.Content = addMessage);
                }
                catch (Exception ex)
                {
                    if (ex is RateLimitedException)
                    {
                        await ReplyAsync("Discord has a ratelimit on adding emoji, and you've just hit it. Try adding your emoji at a later time.");

                        return;
                    }
                    else if (ex is HttpException hex)
                    {
                        string message;

                        switch (hex.HttpCode)
                        {
                        case (HttpStatusCode)400:
                            message = $"The emoji failed to submit to Discord, it may have been too big. {hex.Reason ?? ""}";
                            break;

                        default:
                            message = "Unexpected status code received from Discord, something went wrong. Report it, I guess?";
                            break;
                        }

                        await ReplyAsync(message);

                        return;
                    }
                }
            }
            else
            {
                await ReplyAsync("Alright then, I won't be adding that emoji.");
            }
        }
Пример #2
0
        public async Task RemoveEmoji(string name)
        {
            var serverEmoji = Context.Guild.Emotes;

            var toRemove = serverEmoji.FirstOrDefault(x => x.Name == name);

            if (toRemove == null)
            {
                await ReplyAsync("No emoji was found by that name on this server.");

                return;
            }

            EmbedBuilder embed = new EmbedBuilder
            {
                Title        = "Confirmation",
                Description  = "Are you sure that you want to remove this emoji?\nReact with ✅ in less than 20 seconds to confirm. React with ❌ to abort.",
                ThumbnailUrl = toRemove.Url
            };

            embed.AddField("Name", toRemove.Name);

            SocketReaction reaction;

            try
            {
                var sent = await ReplyAsync(embed : embed.Build());

                ChoiceTracker.AddUser(Context.User.Id);

                var t = Task.Run(async() => await sent.AddReactionsAsync(new[] { reactionYes, reactionNo }));

                reaction = await ReactionCollector.GrabReaction(Context.User, sent,
                                                                x => x.Emote.Equals(reactionYes) || x.Emote.Equals(reactionNo), 20);
            }
            finally
            {
                ChoiceTracker.RemoveUser(Context.User.Id);
            }

            if (reaction == null)
            {
                await ReplyAsync("No reaction was given, aborting.");

                return;
            }

            if (reaction.Emote.Equals(reactionYes))
            {
                try
                {
                    await Context.Guild.DeleteEmoteAsync(toRemove);
                }
                catch (HttpException)
                {
                    await ReplyAsync("I failed to remove the mentioned emoji, maybe somebody else removed it first?");

                    return;
                }

                await ReplyAsync("Emoji successfully removed.");
            }
            else
            {
                await ReplyAsync("Alright, I won't remove that emoji.");
            }
        }