| @@ -39,7 +39,7 @@ namespace Discord.WebSocket | |||||
| } | } | ||||
| internal new static SocketCategoryChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketCategoryChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketCategoryChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketCategoryChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -34,7 +34,7 @@ namespace Discord.WebSocket | |||||
| internal new static SocketForumChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketForumChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketForumChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketForumChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -23,7 +23,7 @@ namespace Discord.WebSocket | |||||
| } | } | ||||
| internal new static SocketNewsChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketNewsChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketNewsChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketNewsChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -49,7 +49,7 @@ namespace Discord.WebSocket | |||||
| internal new static SocketStageChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketStageChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketStageChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketStageChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -61,12 +61,12 @@ namespace Discord.WebSocket | |||||
| internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) | internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) | ||||
| : base(discord, id, guild) | : base(discord, id, guild) | ||||
| { | { | ||||
| if (Discord.MessageCacheSize > 0) | |||||
| if (Discord?.MessageCacheSize > 0) | |||||
| _messages = new MessageCache(Discord); | _messages = new MessageCache(Discord); | ||||
| } | } | ||||
| internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketTextChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketTextChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketTextChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -50,7 +50,7 @@ namespace Discord.WebSocket | |||||
| } | } | ||||
| internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model) | internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
| { | { | ||||
| var entity = new SocketVoiceChannel(guild.Discord, model.Id, guild); | |||||
| var entity = new SocketVoiceChannel(guild?.Discord, model.Id, guild); | |||||
| entity.Update(state, model); | entity.Update(state, model); | ||||
| return entity; | return entity; | ||||
| } | } | ||||
| @@ -58,8 +58,8 @@ namespace Discord.WebSocket | |||||
| internal override void Update(ClientState state, Model model) | internal override void Update(ClientState state, Model model) | ||||
| { | { | ||||
| base.Update(state, model); | base.Update(state, model); | ||||
| Bitrate = model.Bitrate.Value; | |||||
| UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null; | |||||
| Bitrate = model.Bitrate.GetValueOrDefault(64000); | |||||
| UserLimit = model.UserLimit.GetValueOrDefault() != 0 ? model.UserLimit.Value : (int?)null; | |||||
| RTCRegion = model.RTCRegion.GetValueOrDefault(null); | RTCRegion = model.RTCRegion.GetValueOrDefault(null); | ||||
| } | } | ||||
| @@ -20,9 +20,7 @@ namespace Discord.WebSocket | |||||
| ? (DataModel)model.Data.Value | ? (DataModel)model.Data.Value | ||||
| : null; | : null; | ||||
| ulong? guildId = null; | |||||
| if (Channel is SocketGuildChannel guildChannel) | |||||
| guildId = guildChannel.Guild.Id; | |||||
| ulong? guildId = model.GuildId.ToNullable(); | |||||
| Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId); | Data = SocketMessageCommandData.Create(client, dataModel, model.Id, guildId); | ||||
| } | } | ||||
| @@ -20,9 +20,7 @@ namespace Discord.WebSocket | |||||
| ? (DataModel)model.Data.Value | ? (DataModel)model.Data.Value | ||||
| : null; | : null; | ||||
| ulong? guildId = null; | |||||
| if (Channel is SocketGuildChannel guildChannel) | |||||
| guildId = guildChannel.Guild.Id; | |||||
| ulong? guildId = model.GuildId.ToNullable(); | |||||
| Data = SocketUserCommandData.Create(client, dataModel, model.Id, guildId); | Data = SocketUserCommandData.Create(client, dataModel, model.Id, guildId); | ||||
| } | } | ||||
| @@ -61,7 +61,9 @@ namespace Discord.WebSocket | |||||
| author = channel.Guild.GetUser(model.Message.Value.Author.Value.Id); | author = channel.Guild.GetUser(model.Message.Value.Author.Value.Id); | ||||
| } | } | ||||
| else if (model.Message.Value.Author.IsSpecified) | else if (model.Message.Value.Author.IsSpecified) | ||||
| author = (Channel as SocketChannel).GetUser(model.Message.Value.Author.Value.Id); | |||||
| author = (Channel as SocketChannel)?.GetUser(model.Message.Value.Author.Value.Id); | |||||
| author ??= Discord.State.GetOrAddUser(model.Message.Value.Author.Value.Id, _ => SocketGlobalUser.Create(Discord, Discord.State, model.Message.Value.Author.Value)); | |||||
| Message = SocketUserMessage.Create(Discord, Discord.State, author, Channel, model.Message.Value); | Message = SocketUserMessage.Create(Discord, Discord.State, author, Channel, model.Message.Value); | ||||
| } | } | ||||
| @@ -20,9 +20,7 @@ namespace Discord.WebSocket | |||||
| ? (DataModel)model.Data.Value | ? (DataModel)model.Data.Value | ||||
| : null; | : null; | ||||
| ulong? guildId = null; | |||||
| if (Channel is SocketGuildChannel guildChannel) | |||||
| guildId = guildChannel.Guild.Id; | |||||
| ulong? guildId = model.GuildId.ToNullable(); | |||||
| Data = SocketSlashCommandData.Create(client, dataModel, guildId); | Data = SocketSlashCommandData.Create(client, dataModel, guildId); | ||||
| } | } | ||||
| @@ -19,7 +19,7 @@ namespace Discord.WebSocket | |||||
| /// Gets whether or not this command is a global application command. | /// Gets whether or not this command is a global application command. | ||||
| /// </summary> | /// </summary> | ||||
| public bool IsGlobalCommand | public bool IsGlobalCommand | ||||
| => Guild == null; | |||||
| => GuildId is null; | |||||
| /// <inheritdoc/> | /// <inheritdoc/> | ||||
| public ulong ApplicationId { get; private set; } | public ulong ApplicationId { get; private set; } | ||||
| @@ -43,9 +43,7 @@ namespace Discord.WebSocket | |||||
| ? (DataModel)model.Data.Value | ? (DataModel)model.Data.Value | ||||
| : null; | : null; | ||||
| ulong? guildId = null; | |||||
| if (Channel is SocketGuildChannel guildChannel) | |||||
| guildId = guildChannel.Guild.Id; | |||||
| ulong? guildId = model.GuildId.ToNullable(); | |||||
| Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId); | Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId); | ||||
| } | } | ||||
| @@ -1,3 +1,4 @@ | |||||
| using Discord.Net; | |||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| namespace Discord.WebSocket | namespace Discord.WebSocket | ||||
| @@ -45,13 +46,24 @@ namespace Discord.WebSocket | |||||
| if (socketChannel == null) | 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); | |||||
| try | |||||
| { | |||||
| 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); | |||||
| } | |||||
| catch (HttpException ex) when (ex.DiscordCode == DiscordErrorCode.MissingPermissions) | |||||
| { | |||||
| socketChannel = guildId != null | |||||
| ? SocketGuildChannel.Create(guild, discord.State, channel.Value) | |||||
| : (SocketChannel)SocketChannel.CreatePrivate(discord, discord.State, channel.Value); | |||||
| } | |||||
| } | } | ||||
| discord.State.AddChannel(socketChannel); | discord.State.AddChannel(socketChannel); | ||||
| @@ -73,7 +85,10 @@ namespace Discord.WebSocket | |||||
| { | { | ||||
| foreach (var role in resolved.Roles.Value) | foreach (var role in resolved.Roles.Value) | ||||
| { | { | ||||
| var socketRole = guild.AddOrUpdateRole(role.Value); | |||||
| var socketRole = guild is null | |||||
| ? SocketRole.Create(null, discord.State, role.Value) | |||||
| : guild.AddOrUpdateRole(role.Value); | |||||
| Roles.Add(ulong.Parse(role.Key), socketRole); | Roles.Add(ulong.Parse(role.Key), socketRole); | ||||
| } | } | ||||
| } | } | ||||
| @@ -93,16 +108,19 @@ namespace Discord.WebSocket | |||||
| author = guild.GetUser(msg.Value.Author.Value.Id); | author = guild.GetUser(msg.Value.Author.Value.Id); | ||||
| } | } | ||||
| else | else | ||||
| author = (channel as SocketChannel).GetUser(msg.Value.Author.Value.Id); | |||||
| author = (channel as SocketChannel)?.GetUser(msg.Value.Author.Value.Id); | |||||
| if (channel == null) | if (channel == null) | ||||
| { | { | ||||
| if (!msg.Value.GuildId.IsSpecified) // assume it is a DM | |||||
| if (guildId is null) // assume it is a DM | |||||
| { | { | ||||
| channel = discord.CreateDMChannel(msg.Value.ChannelId, msg.Value.Author.Value, discord.State); | channel = discord.CreateDMChannel(msg.Value.ChannelId, msg.Value.Author.Value, discord.State); | ||||
| author = ((SocketDMChannel)channel).Recipient; | |||||
| } | } | ||||
| } | } | ||||
| author ??= discord.State.GetOrAddUser(msg.Value.Author.Value.Id, _ => SocketGlobalUser.Create(discord, discord.State, msg.Value.Author.Value)); | |||||
| var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value); | var message = SocketMessage.Create(discord, discord.State, author, channel, msg.Value); | ||||
| Messages.Add(message.Id, message); | Messages.Add(message.Id, message); | ||||
| } | } | ||||
| @@ -127,7 +127,7 @@ namespace Discord.WebSocket | |||||
| refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id); | refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id); | ||||
| } | } | ||||
| else | else | ||||
| refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id); | |||||
| refMsgAuthor = (Channel as SocketChannel)?.GetUser(refMsg.Author.Value.Id); | |||||
| if (refMsgAuthor == null) | if (refMsgAuthor == null) | ||||
| refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value); | refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value); | ||||
| } | } | ||||
| @@ -63,7 +63,7 @@ namespace Discord.WebSocket | |||||
| => Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id)); | => Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id)); | ||||
| internal SocketRole(SocketGuild guild, ulong id) | internal SocketRole(SocketGuild guild, ulong id) | ||||
| : base(guild.Discord, id) | |||||
| : base(guild?.Discord, id) | |||||
| { | { | ||||
| Guild = guild; | Guild = guild; | ||||
| } | } | ||||