Exemple #1
0
        public override void Update(Model model, UpdateSource source)
        {
            if (source == UpdateSource.Rest && IsAttached)
            {
                return;
            }

            var guildChannel = Channel as GuildChannel;
            var guild        = guildChannel?.Guild;

            if (model.IsTextToSpeech.IsSpecified)
            {
                _isTTS = model.IsTextToSpeech.Value;
            }
            if (model.Pinned.IsSpecified)
            {
                _isPinned = model.Pinned.Value;
            }
            if (model.EditedTimestamp.IsSpecified)
            {
                _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
            }
            if (model.MentionEveryone.IsSpecified)
            {
                _isMentioningEveryone = model.MentionEveryone.Value;
            }

            if (model.Attachments.IsSpecified)
            {
                var value = model.Attachments.Value;
                if (value.Length > 0)
                {
                    var attachments = new Attachment[value.Length];
                    for (int i = 0; i < attachments.Length; i++)
                    {
                        attachments[i] = new Attachment(value[i]);
                    }
                    _attachments = ImmutableArray.Create(attachments);
                }
                else
                {
                    _attachments = ImmutableArray.Create <Attachment>();
                }
            }

            if (model.Embeds.IsSpecified)
            {
                var value = model.Embeds.Value;
                if (value.Length > 0)
                {
                    var embeds = new Embed[value.Length];
                    for (int i = 0; i < embeds.Length; i++)
                    {
                        embeds[i] = new Embed(value[i]);
                    }
                    _embeds = ImmutableArray.Create(embeds);
                }
                else
                {
                    _embeds = ImmutableArray.Create <Embed>();
                }
            }

            ImmutableArray <IUser> mentions = ImmutableArray.Create <IUser>();

            if (model.Mentions.IsSpecified)
            {
                var value = model.Mentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = new IUser[value.Length];
                    for (int i = 0; i < value.Length; i++)
                    {
                        newMentions[i] = new User(value[i]);
                    }
                    mentions = ImmutableArray.Create(newMentions);
                }
            }

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;

                if (guildChannel != null)
                {
                    _mentionedUsers      = MentionUtils.GetUserMentions(text, Channel, mentions);
                    _mentionedChannelIds = MentionUtils.GetChannelMentions(text, guildChannel.Guild);
                    _mentionedRoles      = MentionUtils.GetRoleMentions(text, guildChannel.Guild);
                }
                model.Content = text;
            }

            base.Update(model, source);
        }