Beispiel #1
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, models => models.ToReadOnlyDictionary(
                                             x => TransientEmoji.Create(x.Emoji),
                                             x => new TransientMessageReaction(x) as IMessageReaction));

            Flags = model.Flags.GetValueOrDefault();
        }
Beispiel #2
0
        public void Update(MessageReactionRemoveJsonModel model)
        {
            var reactions = Reactions;

            if (!reactions.HasValue)
            {
                return;
            }

            var emoji = TransientEmoji.Create(model.Emoji);

            if (reactions.Value.GetValueOrDefault(emoji) is not TransientMessageReaction reaction)
            {
                return;
            }

            var newReactions = new Dictionary <IEmoji, IMessageReaction>(reactions.Value);

            if (reaction.Count == 1)
            {
                newReactions.Remove(emoji);
            }
            else
            {
                var emojiModel = reaction.Model;
                emojiModel.Count--;
                if (reaction.HasOwnReaction && model.UserId != Client.CurrentUser.Id)
                {
                    emojiModel.Me = false;
                }
            }

            Reactions = newReactions;
        }
Beispiel #3
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) ?? new TransientMember(Client, model.GuildId.Value, model.Member.Value) as IMember;
            }

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

            return(new(e));
        }
        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, models => models.ToReadOnlyDictionary(model => TransientEmoji.Create(model.Emoji), model => new TransientMessageReaction(model) as IMessageReaction));
            }

            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(model => new TransientAttachment(model));
            }

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

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

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

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

            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) => new TransientRowComponent(client, model) as IRowComponent), Client) ?? Array.Empty <IRowComponent>();
            }

            if (model.StickerItems.HasValue)
            {
                Stickers = Optional.ConvertOrDefault(model.StickerItems, models => models.ToReadOnlyList(model => new TransientMessageSticker(model) as IMessageSticker), Array.Empty <IMessageSticker>());
            }
        }
Beispiel #5
0
        public override ValueTask <ReactionsClearedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, MessageReactionRemoveEmojiJsonModel model)
        {
            CachedUserMessage message;
            Optional <IReadOnlyDictionary <IEmoji, IMessageReaction> > 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.GuildId.GetValueOrNullable(), model.ChannelId, model.MessageId, message, TransientEmoji.Create(model.Emoji), oldReactions);

            return(new(e));
        }
Beispiel #6
0
        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.GuildId.GetValueOrNullable(), model.UserId, model.ChannelId, model.MessageId, message, TransientEmoji.Create(model.Emoji));

            return(new(e));
        }