Exemplo n.º 1
0
        public Task HandleMessageReactionRemoveAsync(PayloadModel payload)
        {
            var model   = payload.D.ToType <MessageReactionRemoveModel>();
            var channel = model.GuildId != null
                ? GetGuildChannel(model.ChannelId) as ICachedMessageChannel
                : GetPrivateChannel(model.ChannelId);

            if (channel == null)
            {
                Log(LogSeverity.Warning, $"Uncached channel in MessageReactionRemove. Id: {model.ChannelId}");
                return(Task.CompletedTask);
            }

            var          message  = channel.GetMessage(model.MessageId);
            ReactionData reaction = null;

            if (message != null)
            {
                message._reactions.TryGetValue(model.Emoji.ToEmoji(), out reaction);
                if (reaction != null)
                {
                    var count = reaction.Count - 1;
                    if (count == 0)
                    {
                        message._reactions.TryRemove(reaction.Emoji, out _);
                    }
                    else
                    {
                        reaction.Count--;
                        if (model.UserId == _currentUser.Id)
                        {
                            reaction.HasCurrentUserReacted = false;
                        }
                    }
                }
            }

            var messageOptional = FetchableSnowflakeOptional.Create <CachedMessage, RestMessage, IMessage>(
                model.MessageId, message, RestFetchable.Create((this, model), (tuple, options) =>
            {
                var(@this, model) = tuple;
                return(@this._client.GetMessageAsync(model.ChannelId, model.MessageId, options));
            }));
            var userOptional = FetchableSnowflakeOptional.Create <CachedUser, RestUser, IUser>(
                model.UserId, channel is CachedTextChannel textChannel
                    ? textChannel.Guild.GetMember(model.UserId) ?? GetUser(model.UserId)
                    : GetUser(model.UserId),
                RestFetchable.Create((this, model), async(tuple, options) =>
            {
                var(@this, model) = tuple;
                return(model.GuildId != null
                        ? await @this._client.GetMemberAsync(model.GuildId.Value, model.UserId, options).ConfigureAwait(false)
                       ?? await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false)
                        : await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false));
            }));

            return(_client._reactionRemoved.InvokeAsync(new ReactionRemovedEventArgs(
                                                            channel, messageOptional, userOptional, reaction ?? Optional <ReactionData> .Empty, model.Emoji.ToEmoji())));
        }
        public Task HandleMessageReactionRemoveAllAsync(PayloadModel payload)
        {
            var model     = Serializer.ToObject <MessageReactionRemoveAllModel>(payload.D);
            var channel   = GetGuild(model.GuildId).GetTextChannel(model.ChannelId);
            var message   = channel.GetMessage(model.MessageId);
            var reactions = message?._reactions.ToDictionary(x => x.Key, x => x.Value);

            message?._reactions.Clear();

            var messageOptional = FetchableSnowflakeOptional.Create <CachedMessage, RestMessage, IMessage>(
                model.MessageId, message, RestFetchable.Create((this, model), (tuple, options) =>
            {
                var(@this, model) = tuple;
                return(@this._client.GetMessageAsync(model.ChannelId, model.MessageId, options));
            }));

            return(_client._reactionsCleared.InvokeAsync(new ReactionsClearedEventArgs(
                                                             channel, messageOptional, reactions?.ReadOnly() ?? ReadOnlyDictionary <IEmoji, ReactionData> .Empty)));
        }
        public Task HandleMessageReactionRemoveEmojiAsync(PayloadModel payload)
        {
            // TODO: intents will ruin everything
            var          model   = Serializer.ToObject <MessageReactionRemoveEmojiModel>(payload.D);
            var          channel = GetGuild(model.GuildId).GetTextChannel(model.ChannelId);
            var          message = channel.GetMessage(model.MessageId);
            var          emoji   = model.Emoji.ToEmoji();
            ReactionData data    = null;

            message?._reactions.TryRemove(emoji, out data);

            var messageOptional = FetchableSnowflakeOptional.Create <CachedMessage, RestMessage, IMessage>(
                model.MessageId, message, RestFetchable.Create((this, model), (tuple, options) =>
            {
                var(@this, model) = tuple;
                return(@this._client.GetMessageAsync(model.ChannelId, model.MessageId, options));
            }));

            return(_client._emojiReactionsCleared.InvokeAsync(new EmojiReactionsClearedEventArgs(
                                                                  channel, messageOptional, emoji, data)));
        }
Exemplo n.º 4
0
        public Task HandleTypingStartedAsync(PayloadModel payload)
        {
            var                   model = payload.D.ToType <TypingStartModel>();
            CachedGuild           guild = null;
            ICachedMessageChannel channel;
            CachedUser            user;

            if (model.GuildId != null)
            {
                guild   = GetGuild(model.GuildId.Value);
                channel = guild.GetTextChannel(model.ChannelId);
                user    = guild.GetMember(model.UserId);
            }
            else
            {
                channel = GetPrivateChannel(model.ChannelId);
                user    = GetUser(model.UserId);
            }

            var userOptional = FetchableSnowflakeOptional.Create <CachedUser, RestUser, IUser>(
                model.UserId, channel is CachedTextChannel textChannel
                    ? textChannel.Guild.GetMember(model.UserId) ?? GetUser(model.UserId)
                    : GetUser(model.UserId),
                RestFetchable.Create((this, model), async(tuple, options) =>
            {
                var(@this, model) = tuple;
                return(model.GuildId != null
                        ? await @this._client.GetMemberAsync(model.GuildId.Value, model.UserId, options).ConfigureAwait(false)
                       ?? await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false)
                        : await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false));
            }));

            return(_client._typingStarted.InvokeAsync(new TypingStartedEventArgs(_client,
                                                                                 new SnowflakeOptional <ICachedMessageChannel>(channel, model.ChannelId),
                                                                                 userOptional,
                                                                                 DateTimeOffset.FromUnixTimeSeconds(model.Timestamp))));
        }
Exemplo n.º 5
0
        public Task HandleMessageReactionAddAsync(PayloadModel payload)
        {
            var model   = Serializer.ToObject <MessageReactionAddModel>(payload.D);
            var channel = model.GuildId != null
                ? GetGuildChannel(model.ChannelId) as ICachedMessageChannel
                : GetPrivateChannel(model.ChannelId);

            if (channel == null)
            {
                Log(LogMessageSeverity.Warning, $"Uncached channel in MessageReactionAdd. Id: {model.ChannelId}");
                return(Task.CompletedTask);
            }

            var          message = channel.GetMessage(model.MessageId);
            IEmoji       emoji;
            ReactionData reaction = null;

            if (message != null)
            {
                if (message._reactions.TryGetValue(model.Emoji.ToEmoji(), out reaction))
                {
                    reaction.Count++;
                    if (model.UserId == _currentUser.Id)
                    {
                        reaction.HasCurrentUserReacted = true;
                    }
                }
                else
                {
                    reaction = new ReactionData(new ReactionModel
                    {
                        Count = 1,
                        Me    = model.UserId == _currentUser.Id,
                        Emoji = model.Emoji
                    });
                    message._reactions.TryAdd(reaction.Emoji, reaction);
                }
                emoji = reaction.Emoji;
            }
            else
            {
                emoji = model.Emoji.ToEmoji();
            }

            var messageOptional = FetchableSnowflakeOptional.Create <CachedMessage, RestMessage, IMessage>(
                model.MessageId, message, RestFetchable.Create((this, model), (tuple, options) =>
            {
                var(@this, model) = tuple;
                return(@this._client.GetMessageAsync(model.ChannelId, model.MessageId, options));
            }));
            var userOptional = FetchableSnowflakeOptional.Create <CachedUser, RestUser, IUser>(
                model.UserId, channel is CachedTextChannel textChannel
                    ? textChannel.Guild.GetMember(model.UserId) ?? GetUser(model.UserId)
                    : GetUser(model.UserId),
                RestFetchable.Create((this, model), async(tuple, options) =>
            {
                var(@this, model) = tuple;
                return(model.GuildId != null
                        ? await @this._client.GetMemberAsync(model.GuildId.Value, model.UserId, options).ConfigureAwait(false)
                       ?? await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false)
                        : await @this._client.GetUserAsync(model.UserId, options).ConfigureAwait(false));
            }));

            return(_client._reactionAdded.InvokeAsync(new ReactionAddedEventArgs(
                                                          channel, messageOptional, userOptional, reaction ?? Optional <ReactionData> .Empty, emoji)));
        }