Exemple #1
0
        public void Update(MessageReactionAddJsonModel model)
        {
            var emoji     = Emoji.Create(model.Emoji);
            var reactions = Reactions;

            if (!reactions.HasValue)
            {
                var newReactions = new Dictionary <IEmoji, MessageReaction>();
                newReactions.Add(emoji, new MessageReaction(emoji, 1, model.UserId == Client.CurrentUser.Id));
                Reactions = new Optional <IReadOnlyDictionary <IEmoji, MessageReaction> >(newReactions.ReadOnly());
            }
            else
            {
                var newReactions = new Dictionary <IEmoji, MessageReaction>(reactions.Value);
                var reaction     = newReactions.GetValueOrDefault(emoji);
                newReactions[emoji] = new MessageReaction(emoji, (reaction?.Count ?? 0) + 1, (reaction?.HasOwnReaction ?? false) || model.UserId == Client.CurrentUser.Id);
                Reactions           = newReactions;
            }
        }
Exemple #2
0
        public void Update(MessageReactionRemoveJsonModel model)
        {
            var reactions = Reactions;

            if (reactions.HasValue)
            {
                var emoji = Emoji.Create(model.Emoji);
                if (reactions.Value.TryGetValue(emoji, out var reaction))
                {
                    var newReactions = new Dictionary <IEmoji, MessageReaction>(reactions.Value);
                    if (reaction.Count == 1)
                    {
                        newReactions.Remove(emoji);
                    }
                    else
                    {
                        newReactions[emoji] = new MessageReaction(emoji, reaction.Count - 1, reaction.HasOwnReaction && model.UserId == Client.CurrentUser.Id
                            ? false
                            : reaction.HasOwnReaction);
                    }
                    Reactions = newReactions;
                }
            }
        }
Exemple #3
0
        public void Update(MessageUpdateJsonModel model)
        {
            if (model.Author.HasValue)
            {
                if (_transientAuthor != null)
                {
                    if (model.Member.HasValue)
                    {
                        model.Member.Value.User = model.Author;
                        _transientAuthor        = new TransientMember(Client, GuildId.Value, model.Member.Value);
                    }
                    else
                    {
                        _transientAuthor = new TransientUser(Client, model.Author.Value);
                    }
                }
            }

            if (model.Content.HasValue)
            {
                Content = model.Content.Value;
            }

            if (model.Mentions.HasValue)
            {
                MentionedUsers = model.Mentions.Value.ToReadOnlyList(Client, (x, client) =>
                {
                    var user = client.GetUser(x.Id);
                    if (user != null)
                    {
                        return(user);
                    }

                    return(new TransientUser(client, x) as IUser);
                });
            }

            if (model.Reactions.HasValue)
            {
                Reactions = Optional.Convert(model.Reactions, x => x.ToReadOnlyDictionary(x => Emoji.Create(x.Emoji), x => new MessageReaction(x)));
            }

            if (model.EditedTimestamp.HasValue)
            {
                EditedAt = model.EditedTimestamp.Value;
            }

            if (model.Pinned.HasValue)
            {
                IsPinned = model.Pinned.Value;
            }

            if (model.MentionEveryone.HasValue)
            {
                MentionsEveryone = model.MentionEveryone.Value;
            }

            if (model.MentionRoles.HasValue)
            {
                MentionedRoleIds = model.MentionRoles.Value.ReadOnly();
            }

            if (model.Attachments.HasValue)
            {
                Attachments = model.Attachments.Value.ToReadOnlyList(x => new Attachment(x));
            }

            if (model.Embeds.HasValue)
            {
                Embeds = model.Embeds.Value.ToReadOnlyList(x => new Embed(x));
            }

            if (model.Activity.HasValue)
            {
                Activity = Optional.ConvertOrDefault(model.Activity, x => new MessageActivity(x));
            }

            if (model.Application.HasValue)
            {
                Application = Optional.ConvertOrDefault(model.Application, x => new MessageApplication(x));
            }

            if (model.MessageReference.HasValue)
            {
                Reference = Optional.ConvertOrDefault(model.MessageReference, x => new MessageReference(x));
            }

            if (model.Flags.HasValue)
            {
                Flags = model.Flags.Value;
            }

            if (model.ReferencedMessage.HasValue)
            {
                ReferencedMessage = Optional.Convert(model.ReferencedMessage, x => new TransientUserMessage(Client, x) as IUserMessage);
            }

            if (model.Components.HasValue)
            {
                Components = Optional.ConvertOrDefault(model.Components, (models, client) => models.ToReadOnlyList(client, (model, client) => TransientComponent.Create(client, model)), Client) ?? Array.Empty <IComponent>();
            }

            if (model.StickerItems.HasValue)
            {
                Stickers = Optional.ConvertOrDefault(model.StickerItems, x => x.ToReadOnlyList(y => new MessageSticker(y)), Array.Empty <MessageSticker>());
            }
        }
Exemple #4
0
        public virtual void Update(MessageJsonModel model)
        {
            if (_transientAuthor != null)
            {
                if (model.Member.HasValue)
                {
                    model.Member.Value.User = model.Author;
                    _transientAuthor        = new TransientMember(Client, GuildId.Value, model.Member.Value);
                }
                else
                {
                    _transientAuthor = new TransientUser(Client, model.Author);
                }
            }
            Content        = model.Content;
            MentionedUsers = model.Mentions.ToReadOnlyList(Client, (x, client) =>
            {
                var user = client.GetUser(x.Id);
                if (user != null)
                {
                    return(user);
                }

                return(new TransientUser(client, x) as IUser);
            });
            Reactions = Optional.Convert(model.Reactions, x => x.ToReadOnlyDictionary(x => Emoji.Create(x.Emoji), x => new Reaction(x)));
        }
        public override ValueTask <ReactionRemovedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, MessageReactionRemoveJsonModel model)
        {
            CachedUserMessage message;

            if (CacheProvider.TryGetMessages(model.ChannelId, out var messageCache))
            {
                message = messageCache.GetValueOrDefault(model.MessageId);
                message?.Update(model);
            }
            else
            {
                message = null;
            }

            var e = new ReactionRemovedEventArgs(model.UserId, model.ChannelId, model.MessageId, message, model.GuildId.GetValueOrNullable(), Emoji.Create(model.Emoji));

            return(new(e));
        }
Exemple #6
0
        public override ValueTask <ReactionAddedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, MessageReactionAddJsonModel model)
        {
            CachedUserMessage message;
            IMember           member = null;

            if (CacheProvider.TryGetMessages(model.ChannelId, out var messageCache))
            {
                message = messageCache.GetValueOrDefault(model.MessageId);
                message?.Update(model);
            }
            else
            {
                message = null;
            }

            if (model.GuildId.HasValue)
            {
                member = Dispatcher.GetOrAddMember(model.GuildId.Value, model.Member.Value);
                if (member == null)
                {
                    member = new TransientMember(Client, model.GuildId.Value, model.Member.Value);
                }
            }

            var e = new ReactionAddedEventArgs(model.UserId, model.ChannelId, model.MessageId, message, model.GuildId.GetValueOrNullable(), member, Emoji.Create(model.Emoji));

            return(new(e));
        }
Exemple #7
0
        public void Update(MessageReactionRemoveEmojiJsonModel model)
        {
            var emoji = Emoji.Create(model.Emoji);

            Reactions = Optional.Convert(Reactions, x => x.Where(x => !x.Key.Equals(emoji)).ToReadOnlyDictionary(x => x.Key, x => x.Value));
        }
Exemple #8
0
        public override ValueTask <ReactionsClearedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, MessageReactionRemoveEmojiJsonModel model)
        {
            CachedUserMessage message;
            Optional <IReadOnlyDictionary <IEmoji, MessageReaction> > oldReactions;

            if (CacheProvider.TryGetMessages(model.ChannelId, out var messageCache))
            {
                message      = messageCache.GetValueOrDefault(model.MessageId);
                oldReactions = message?.Reactions ?? default;
                message?.Update(model);
            }
            else
            {
                message      = null;
                oldReactions = default;
            }

            var e = new ReactionsClearedEventArgs(model.ChannelId, model.MessageId, message, model.GuildId.GetValueOrNullable(), Emoji.Create(model.Emoji), oldReactions);

            return(new(e));
        }