internal override void Update(Model model) { base.Update(model); if (model.Message.IsSpecified) { if (Message == null) { SocketUser author = null; if (Channel is SocketGuildChannel channel) { if (model.Message.Value.WebhookId.IsSpecified) { author = SocketWebhookUser.Create(channel.Guild, Discord.State, model.Message.Value.Author.Value, model.Message.Value.WebhookId.Value); } else if (model.Message.Value.Author.IsSpecified) { author = channel.Guild.GetUser(model.Message.Value.Author.Value.Id); } } else if (model.Message.Value.Author.IsSpecified) { author = (Channel as SocketChannel).GetUser(model.Message.Value.Author.Value.Id); } Message = SocketUserMessage.Create(Discord, Discord.State, author, Channel, model.Message.Value); } else { Message.Update(Discord.State, model.Message.Value); } } }
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); } }
internal SocketResolvableData(DiscordSocketClient discord, ulong?guildId, T model) { var guild = guildId.HasValue ? discord.GetGuild(guildId.Value) : null; var resolved = model.Resolved.Value; if (resolved.Users.IsSpecified) { foreach (var user in resolved.Users.Value) { var socketUser = discord.GetOrCreateUser(discord.State, user.Value); Users.Add(ulong.Parse(user.Key), socketUser); } } if (resolved.Channels.IsSpecified) { foreach (var channel in resolved.Channels.Value) { SocketChannel socketChannel = guild != null ? guild.GetChannel(channel.Value.Id) : discord.GetChannel(channel.Value.Id); if (socketChannel == null) { var channelModel = guild != null ? discord.Rest.ApiClient.GetChannelAsync(guild.Id, channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult() : discord.Rest.ApiClient.GetChannelAsync(channel.Value.Id).ConfigureAwait(false).GetAwaiter().GetResult(); socketChannel = guild != null ? SocketGuildChannel.Create(guild, discord.State, channelModel) : (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channelModel); } discord.State.AddChannel(socketChannel); Channels.Add(ulong.Parse(channel.Key), socketChannel); } } if (resolved.Members.IsSpecified && guild != null) { foreach (var member in resolved.Members.Value) { member.Value.User = resolved.Users.Value[member.Key]; var user = guild.AddOrUpdateUser(member.Value); GuildMembers.Add(ulong.Parse(member.Key), user); } } if (resolved.Roles.IsSpecified) { foreach (var role in resolved.Roles.Value) { var socketRole = guild.AddOrUpdateRole(role.Value); Roles.Add(ulong.Parse(role.Key), socketRole); } } if (resolved.Messages.IsSpecified) { foreach (var msg in resolved.Messages.Value) { var channel = discord.GetChannel(msg.Value.ChannelId) as ISocketMessageChannel; SocketUser author; if (guild != null) { if (msg.Value.WebhookId.IsSpecified) { author = SocketWebhookUser.Create(guild, discord.State, msg.Value.Author.Value, msg.Value.WebhookId.Value); } else { author = guild.GetUser(msg.Value.Author.Value.Id); } } else { author = (channel as SocketChannel).GetUser(msg.Value.Author.Value.Id); } if (channel == null) { if (!msg.Value.GuildId.IsSpecified) // assume it is a DM { channel = discord.CreateDMChannel(msg.Value.ChannelId, msg.Value.Author.Value, discord.State); } } var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value); Messages.Add(message.Id, message); } } if (resolved.Attachments.IsSpecified) { foreach (var attachment in resolved.Attachments.Value) { var discordAttachment = Attachment.Create(attachment.Value); Attachments.Add(ulong.Parse(attachment.Key), discordAttachment); } } }
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>(); } } }