Esempio n. 1
0
        internal virtual void Update(Model model)
        {
            if (model.Timestamp.IsSpecified)
            {
                _timestampTicks = model.Timestamp.Value.UtcTicks;
            }

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

            if (model.Application.IsSpecified)
            {
                // create a new Application from the API model
                Application = new MessageApplication()
                {
                    Id          = model.Application.Value.Id,
                    CoverImage  = model.Application.Value.CoverImage,
                    Description = model.Application.Value.Description,
                    Icon        = model.Application.Value.Icon,
                    Name        = model.Application.Value.Name
                };
            }

            if (model.Activity.IsSpecified)
            {
                // create a new Activity from the API model
                Activity = new MessageActivity()
                {
                    Type    = model.Activity.Value.Type.Value,
                    PartyId = model.Activity.Value.PartyId.GetValueOrDefault()
                };
            }

            if (model.Reactions.IsSpecified)
            {
                var value = model.Reactions.Value;
                if (value.Length > 0)
                {
                    var reactions = ImmutableArray.CreateBuilder <RestReaction>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        reactions.Add(RestReaction.Create(value[i]));
                    }
                    _reactions = reactions.ToImmutable();
                }
                else
                {
                    _reactions = ImmutableArray.Create <RestReaction>();
                }
            }
            else
            {
                _reactions = ImmutableArray.Create <RestReaction>();
            }
        }
        internal override void Update(Model model)
        {
            base.Update(model);

            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.Flags.IsSpecified)
            {
                _isSuppressed = model.Flags.Value.HasFlag(API.MessageFlags.Suppressed);
            }

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

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

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

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <IUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val.Object != null)
                        {
                            newMentions.Add(RestUser.Create(Discord, val.Object));
                        }
                    }
                    mentions = newMentions.ToImmutable();
                }
            }

            if (model.Reactions.IsSpecified)
            {
                var value = model.Reactions.Value;
                if (value.Length > 0)
                {
                    var reactions = ImmutableArray.CreateBuilder <RestReaction>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        reactions.Add(RestReaction.Create(value[i]));
                    }
                    _reactions = reactions.ToImmutable();
                }
                else
                {
                    _reactions = ImmutableArray.Create <RestReaction>();
                }
            }
            else
            {
                _reactions = ImmutableArray.Create <RestReaction>();
            }

            if (model.Content.IsSpecified)
            {
                var text    = model.Content.Value;
                var guildId = (Channel as IGuildChannel)?.GuildId;
                var guild   = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;
                _tags         = MessageHelper.ParseTags(text, null, guild, mentions);
                model.Content = text;
            }
        }
        internal virtual void Update(Model model)
        {
            Type = model.Type;

            if (model.Timestamp.IsSpecified)
            {
                _timestampTicks = model.Timestamp.Value.UtcTicks;
            }

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

            if (model.Application.IsSpecified)
            {
                // create a new Application from the API model
                Application = new MessageApplication()
                {
                    Id          = model.Application.Value.Id,
                    CoverImage  = model.Application.Value.CoverImage,
                    Description = model.Application.Value.Description,
                    Icon        = model.Application.Value.Icon,
                    Name        = model.Application.Value.Name
                };
            }

            if (model.Activity.IsSpecified)
            {
                // create a new Activity from the API model
                Activity = new MessageActivity()
                {
                    Type    = model.Activity.Value.Type.Value,
                    PartyId = model.Activity.Value.PartyId.GetValueOrDefault()
                };
            }

            if (model.Reference.IsSpecified)
            {
                // Creates a new Reference from the API model
                Reference = new MessageReference
                {
                    GuildId           = model.Reference.Value.GuildId,
                    InternalChannelId = model.Reference.Value.ChannelId,
                    MessageId         = model.Reference.Value.MessageId
                };
            }

            if (model.Components.IsSpecified)
            {
                Components = model.Components.Value.Select(x => new ActionRowComponent(x.Components.Select <IMessageComponent, IMessageComponent>(y =>
                {
                    switch (y.Type)
                    {
                    case ComponentType.Button:
                        {
                            var parsed = (API.ButtonComponent)y;
                            return(new Discord.ButtonComponent(
                                       parsed.Style,
                                       parsed.Label.GetValueOrDefault(),
                                       parsed.Emote.IsSpecified
                                        ? parsed.Emote.Value.Id.HasValue
                                            ? new Emote(parsed.Emote.Value.Id.Value, parsed.Emote.Value.Name, parsed.Emote.Value.Animated.GetValueOrDefault())
                                            : new Emoji(parsed.Emote.Value.Name)
                                        : null,
                                       parsed.CustomId.GetValueOrDefault(),
                                       parsed.Url.GetValueOrDefault(),
                                       parsed.Disabled.GetValueOrDefault()));
                        }

                    case ComponentType.SelectMenu:
                        {
                            var parsed = (API.SelectMenuComponent)y;
                            return(new SelectMenuComponent(
                                       parsed.CustomId,
                                       parsed.Options.Select(z => new SelectMenuOption(
                                                                 z.Label,
                                                                 z.Value,
                                                                 z.Description.GetValueOrDefault(),
                                                                 z.Emoji.IsSpecified
                                        ? z.Emoji.Value.Id.HasValue
                                            ? new Emote(z.Emoji.Value.Id.Value, z.Emoji.Value.Name, z.Emoji.Value.Animated.GetValueOrDefault())
                                            : new Emoji(z.Emoji.Value.Name)
                                        : null,
                                                                 z.Default.ToNullable())).ToList(),
                                       parsed.Placeholder.GetValueOrDefault(),
                                       parsed.MinValues,
                                       parsed.MaxValues,
                                       parsed.Disabled
                                       ));
                        }

                    default:
                        return(null);
                    }
                }).ToList())).ToImmutableArray();
            }
            else
            {
                Components = new List <ActionRowComponent>();
            }

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

            if (model.Reactions.IsSpecified)
            {
                var value = model.Reactions.Value;
                if (value.Length > 0)
                {
                    var reactions = ImmutableArray.CreateBuilder <RestReaction>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        reactions.Add(RestReaction.Create(value[i]));
                    }
                    _reactions = reactions.ToImmutable();
                }
                else
                {
                    _reactions = ImmutableArray.Create <RestReaction>();
                }
            }
            else
            {
                _reactions = ImmutableArray.Create <RestReaction>();
            }

            if (model.Interaction.IsSpecified)
            {
                Interaction = new MessageInteraction <RestUser>(model.Interaction.Value.Id,
                                                                model.Interaction.Value.Type,
                                                                model.Interaction.Value.Name,
                                                                RestUser.Create(Discord, model.Interaction.Value.User));
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <RestUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val != null)
                        {
                            newMentions.Add(RestUser.Create(Discord, val));
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }
        }