示例#1
0
 public static MessageSource GetSource(Model msg)
 {
     if (msg.Type != MessageType.Default)
     {
         return(MessageSource.System);
     }
     else if (msg.WebhookId.IsSpecified)
     {
         return(MessageSource.Webhook);
     }
     else if (msg.Author.GetValueOrDefault()?.Bot.GetValueOrDefault(false) == true)
     {
         return(MessageSource.Bot);
     }
     return(MessageSource.User);
 }
示例#2
0
        internal virtual void Update(ClientState state, 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.Reference.IsSpecified)
            {
                // Creates a new Reference from the API model
                Reference = new MessageReference
                {
                    GuildId   = model.Reference.Value.GuildId,
                    ChannelId = model.Reference.Value.ChannelId,
                    MessageId = model.Reference.Value.MessageId
                };
            }
        }
示例#3
0
        public virtual void Update(Model model, UpdateSource source)
        {
            if (source == UpdateSource.Rest && IsAttached)
            {
                return;
            }

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

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

            if (model.Content.IsSpecified)
            {
                Content = model.Content.Value;
            }
        }
示例#4
0
        internal override void Update(ClientState state, Model model)
        {
            base.Update(state, model);

            SocketGuild guild = (Channel as SocketGuildChannel)?.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.RoleMentions.IsSpecified)
            {
                _roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
            }

            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>();
                }
            }

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, Channel, guild, MentionedUsers);
                model.Content = text;
            }

            if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null)
            {
                var        refMsg       = model.ReferencedMessage.Value;
                ulong?     webhookId    = refMsg.WebhookId.ToNullable();
                SocketUser refMsgAuthor = null;
                if (refMsg.Author.IsSpecified)
                {
                    if (guild != null)
                    {
                        if (webhookId != null)
                        {
                            refMsgAuthor = SocketWebhookUser.Create(guild, state, refMsg.Author.Value, webhookId.Value);
                        }
                        else
                        {
                            refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id);
                        }
                    }
                    else
                    {
                        refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id);
                    }
                    if (refMsgAuthor == null)
                    {
                        refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value);
                    }
                }
                else
                {
                    // Message author wasn't specified in the payload, so create a completely anonymous unknown user
                    refMsgAuthor = new SocketUnknownUser(Discord, id: 0);
                }
                _referencedMessage = SocketUserMessage.Create(Discord, state, refMsgAuthor, Channel, refMsg);
            }

            if (model.StickerItems.IsSpecified)
            {
                var value = model.StickerItems.Value;
                if (value.Length > 0)
                {
                    var stickers = ImmutableArray.CreateBuilder <SocketSticker>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var           stickerItem = value[i];
                        SocketSticker sticker     = null;

                        if (guild != null)
                        {
                            sticker = guild.GetSticker(stickerItem.Id);
                        }

                        if (sticker == null)
                        {
                            sticker = Discord.GetSticker(stickerItem.Id);
                        }

                        // if they want to auto resolve
                        if (Discord.AlwaysResolveStickers)
                        {
                            sticker = Task.Run(async() => await Discord.GetStickerAsync(stickerItem.Id).ConfigureAwait(false)).GetAwaiter().GetResult();
                        }

                        // if its still null, create an unknown
                        if (sticker == null)
                        {
                            sticker = SocketUnknownSticker.Create(Discord, stickerItem);
                        }

                        stickers.Add(sticker);
                    }

                    _stickers = stickers.ToImmutable();
                }
                else
                {
                    _stickers = ImmutableArray.Create <SocketSticker>();
                }
            }
        }
示例#5
0
 internal MessageChunk(Model model)
 {
 }
 internal new void Update(Model model)
 {
     base.Update(model);
 }
示例#7
0
        internal override void Update(ClientState state, Model model)
        {
            base.Update(state, model);

            SocketGuild guild = (Channel as SocketGuildChannel)?.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.RoleMentions.IsSpecified)
            {
                _roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
            }

            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>();
                }
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <SocketUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val.Object != null)
                        {
                            var user = Channel.GetUserAsync(val.Object.Id, CacheMode.CacheOnly).GetAwaiter().GetResult() as SocketUser;
                            if (user != null)
                            {
                                newMentions.Add(user);
                            }
                            else
                            {
                                newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object));
                            }
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, Channel, guild, _userMentions);
                model.Content = text;
            }

            if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null)
            {
                var        refMsg       = model.ReferencedMessage.Value;
                ulong?     webhookId    = refMsg.WebhookId.ToNullable();
                SocketUser refMsgAuthor = null;
                if (refMsg.Author.IsSpecified)
                {
                    if (guild != null)
                    {
                        if (webhookId != null)
                        {
                            refMsgAuthor = SocketWebhookUser.Create(guild, state, refMsg.Author.Value, webhookId.Value);
                        }
                        else
                        {
                            refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id);
                        }
                    }
                    else
                    {
                        refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id);
                    }
                    if (refMsgAuthor == null)
                    {
                        refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value);
                    }
                }
                else
                {
                    // Message author wasn't specified in the payload, so create a completely anonymous unknown user
                    refMsgAuthor = new SocketUnknownUser(Discord, id: 0);
                }
                _referencedMessage = SocketUserMessage.Create(Discord, state, refMsgAuthor, Channel, refMsg);
            }
        }
示例#8
0
        internal override void Update(Model model)
        {
            base.Update(model);

            Type = model.Type;
        }
示例#9
0
        internal override void Update(ClientState state, Model model)
        {
            base.Update(state, model);

            Type = model.Type;
        }
示例#10
0
 internal static SocketMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model)
 {
     if (model.Type == MessageType.Default)
     {
         return(SocketUserMessage.Create(discord, state, author, channel, model));
     }
     else
     {
         return(SocketSystemMessage.Create(discord, state, author, channel, model));
     }
 }
 internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
 {
     if (model.Type == MessageType.Default ||
         model.Type == MessageType.Reply ||
         model.Type == MessageType.ApplicationCommand ||
         model.Type == MessageType.ThreadStarterMessage)
     {
         return(RestUserMessage.Create(discord, channel, author, model));
     }
     else
     {
         return(RestSystemMessage.Create(discord, channel, author, model));
     }
 }
示例#12
0
 private UserMessage CreateOutgoingMessage(MessageModel model)
 {
     return(new UserMessage(this, new User(model.Author.Value), model));
 }
        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.RoleMentions.IsSpecified)
            {
                _roleMentionIds = model.RoleMentions.Value.ToImmutableArray();
            }

            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>();
                }
            }

            var guildId = (Channel as IGuildChannel)?.GuildId;
            var guild   = guildId != null ? (Discord as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, null, guild, MentionedUsers);
                model.Content = text;
            }

            if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null)
            {
                var   refMsg       = model.ReferencedMessage.Value;
                IUser refMsgAuthor = MessageHelper.GetAuthor(Discord, guild, refMsg.Author.Value, refMsg.WebhookId.ToNullable());
                _referencedMessage = RestUserMessage.Create(Discord, Channel, refMsgAuthor, refMsg);
            }

            if (model.StickerItems.IsSpecified)
            {
                var value = model.StickerItems.Value;
                if (value.Length > 0)
                {
                    var stickers = ImmutableArray.CreateBuilder <StickerItem>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        stickers.Add(new StickerItem(Discord, value[i]));
                    }
                    _stickers = stickers.ToImmutable();
                }
                else
                {
                    _stickers = ImmutableArray.Create <StickerItem>();
                }
            }
        }
示例#14
0
 internal override void Update(Model model)
 {
     base.Update(model);
 }
示例#15
0
 public SocketUserMessage(ISocketMessageChannel channel, IUser author, Model model) 
     : base(channel, author, model)
 {
 }
示例#16
0
 public ISocketMessage CreateMessage(ISocketUser author, MessageModel model)
 {
     return(_messages.Create(author, model));
 }
示例#17
0
        internal override void Update(ClientState state, Model model)
        {
            base.Update(state, 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.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>();
                }
            }

            IReadOnlyCollection <IUser> mentions = ImmutableArray.Create <SocketUnknownUser>(); //Is passed to ParseTags to get real mention collection

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

            if (model.Content.IsSpecified)
            {
                var text  = model.Content.Value;
                var guild = (Channel as SocketGuildChannel)?.Guild;
                _tags         = MessageHelper.ParseTags(text, Channel, guild, mentions);
                model.Content = text;
            }
        }
示例#18
0
        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.RoleMentions.IsSpecified)
            {
                _roleMentionIds = model.RoleMentions.Value.ToImmutableArray();
            }

            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>();
                }
            }

            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.Object != null)
                        {
                            newMentions.Add(RestUser.Create(Discord, val.Object));
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

            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, _userMentions);
                model.Content = text;
            }
        }
示例#19
0
        internal static new RestUserMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
        {
            var entity = new RestUserMessage(discord, model.Id, channel, author, MessageHelper.GetSource(model));

            entity.Update(model);
            return(entity);
        }
示例#20
0
        internal new static RestSystemMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
        {
            var entity = new RestSystemMessage(discord, model.Id, channel, author);

            entity.Update(model);
            return(entity);
        }
示例#21
0
 internal static SocketMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model)
 {
     if (model.Type == MessageType.Default ||
         model.Type == MessageType.Reply ||
         model.Type == MessageType.ApplicationCommand ||
         model.Type == MessageType.ThreadStarterMessage ||
         model.Type == MessageType.ContextMenuCommand)
     {
         return(SocketUserMessage.Create(discord, state, author, channel, model));
     }
     else
     {
         return(SocketSystemMessage.Create(discord, state, author, channel, model));
     }
 }
示例#22
0
        internal new static SocketUserMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model)
        {
            var entity = new SocketUserMessage(discord, model.Id, channel, author, MessageHelper.GetSource(model));

            entity.Update(state, model);
            return(entity);
        }
示例#23
0
        internal virtual void Update(ClientState state, 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,
                    FailIfNotExists   = model.Reference.Value.FailIfNotExists
                };
            }

            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.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <SocketUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val != null)
                        {
                            var user = Channel.GetUserAsync(val.Id, CacheMode.CacheOnly).GetAwaiter().GetResult() as SocketUser;
                            if (user != null)
                            {
                                newMentions.Add(user);
                            }
                            else
                            {
                                newMentions.Add(SocketUnknownUser.Create(Discord, state, val));
                            }
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

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

            if (model.Flags.IsSpecified)
            {
                Flags = model.Flags.Value;
            }
        }
示例#24
0
 public SystemMessage(IMessageChannel channel, IUser author, Model model)
     : base(channel, author, model)
 {
     Type = model.Type;
 }
示例#25
0
        internal override void Update(ClientState state, Model model)
        {
            base.Update(state, model);

            SocketGuild guild = (Channel as SocketGuildChannel)?.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.Flags.IsSpecified)
            {
                _isSuppressed = model.Flags.Value.HasFlag(API.MessageFlags.Suppressed);
            }
            if (model.RoleMentions.IsSpecified)
            {
                _roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray();
            }

            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>();
                }
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;
                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <SocketUser>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val       = value[i];
                        var guildUser = guild.GetUser(val.Id);
                        if (guildUser != null)
                        {
                            newMentions.Add(guildUser);
                        }
                        else if (val.Object != null)
                        {
                            newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object));
                        }
                    }
                    _userMentions = newMentions.ToImmutable();
                }
            }

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                _tags         = MessageHelper.ParseTags(text, Channel, guild, _userMentions);
                model.Content = text;
            }
        }
示例#26
0
 internal static RestMessage Create(BaseDiscordClient discord, IMessageChannel channel, IUser author, Model model)
 {
     if (model.Type == MessageType.Default)
     {
         return(RestUserMessage.Create(discord, channel, author, model));
     }
     else
     {
         return(RestSystemMessage.Create(discord, channel, author, model));
     }
 }
示例#27
0
        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.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>();
            }
        }
示例#28
0
        internal void Update(Model model)
        {
            Id        = model.Id;
            CreatedAt = SnowflakeUtils.FromSnowflake(Id);

            if (model.IsTextToSpeech.IsSpecified)
            {
                IsTTS = model.IsTextToSpeech.Value;
            }
            if (model.Pinned.IsSpecified)
            {
                IsPinned = model.Pinned.Value;
            }
            if (model.EditedTimestamp.IsSpecified)
            {
                EditedTimestamp = model.EditedTimestamp.Value;
            }
            if (model.MentionEveryone.IsSpecified)
            {
                MentionedEveryone = model.MentionEveryone.Value;
            }
            if (model.RoleMentions.IsSpecified)
            {
                MentionedRoleIds = model.RoleMentions.Value.ToImmutableArray();
            }

            MentionedUserIds    = new List <ulong>();
            MentionedChannelIds = new List <ulong>();

            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>();
                }
            }

            if (model.UserMentions.IsSpecified)
            {
                var value = model.UserMentions.Value;

                if (value.Length > 0)
                {
                    var newMentions = ImmutableArray.CreateBuilder <ulong>(value.Length);
                    for (int i = 0; i < value.Length; i++)
                    {
                        var val = value[i];
                        if (val.Object != null)
                        {
                            newMentions.Add(val.Object.Id);
                        }
                    }
                    MentionedUserIds = newMentions.ToReadOnlyCollection();
                }
            }

            if (model.Content.IsSpecified)
            {
                var text = model.Content.Value;
                model.Content = text;
            }
        }
示例#29
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);
        }