protected internal override void Setup(DiscordClient client)
 {
     this.Client = client;
     this.MessageCreatedWaiter     = new EventWaiter <MessageCreateEventArgs>(this.Client);
     this.MessageReactionAddWaiter = new EventWaiter <MessageReactionAddEventArgs>(this.Client);
     this.TypingStartWaiter        = new EventWaiter <TypingStartEventArgs>(this.Client);
     this.Poller            = new Poller(this.Client);
     this.ReactionCollector = new ReactionCollector(this.Client);
     this.Paginator         = new Paginator(this.Client);
 }
Esempio n. 2
0
        public static void CreateReactionCollector(this RestUserMessage userMessage, Action <ulong, IEmote, bool> onReactionChanged, params IEmote[] emotes)
        {
            ReactionCollector rCollector = new ReactionCollector()
            {
                ListeningEmotes   = emotes,
                MessageHandle     = userMessage,
                OnReactionChanged = onReactionChanged
            };

            userMessage.AddReactionsAsync(emotes);
            activeReactionCollectors.Add(userMessage.Id, rCollector);
        }
Esempio n. 3
0
        public bool CombineMaterials(IMaterial[] materialsToCombine, out IMaterial result, out ArrayList observedReactions, out ArrayList observedReactionInstances)
        {
            Mixture scratch = new Mixture(null, "scratch mixture");

            Watch(scratch);
            ReactionCollector rc = new ReactionCollector(scratch);

            foreach (IMaterial material in materialsToCombine)
            {
                scratch.AddMaterial(material);
            }
            observedReactions         = rc.Reactions;
            observedReactionInstances = rc.ReactionInstances;
            rc.Disconnect();
            result = scratch;
            return(observedReactions != null && observedReactions.Count > 0);
        }
Esempio n. 4
0
        public async Task StartAsync(IMessageChannel channel)
        {
            var firstPage = _pages[0];

            if (firstPage is string page)
            {
                _message = await channel
                           .SendMessageAsync(RenderPageNumber(page))
                           .ConfigureAwait(false);
            }
            else
            {
                _message = await channel
                           .SendMessageAsync(_content, embed : MergeEmbedTemplate((Embed)firstPage))
                           .ConfigureAwait(false);
            }

            // Don't bother adding reactions if there's only one page.
            if (_pageCount == 0)
            {
                return;
            }

            await AddReactionsAsync().ConfigureAwait(false);

            var options = new ReactionCollectorConfig(_client)
            {
                Message = _message,
                Timeout = _timeout
            };

            _collector          = new ReactionCollector(x => _filter(x), options);
            _collector.Collect += OnCollect;

            if (_deleteOnTimeout)
            {
                _collector.End += (_, __) => _message.DeleteAsync();
            }

            await _collector.StartAsync(false).ConfigureAwait(false);
        }
Esempio n. 5
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.");
            }
        }
Esempio n. 6
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.");
            }
        }