diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index c419bb764..5ae1a0350 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1329,21 +1329,8 @@ namespace Discord.WebSocket return; } - if (channel == null) - { - if (!data.GuildId.IsSpecified) // assume it is a DM - { - channel = CreateDMChannel(data.ChannelId, data.Author.Value, State); - } - else - { - await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false); - return; - } - } - SocketMessage before = null, after = null; - SocketMessage cachedMsg = channel.GetCachedMessage(data.Id); + SocketMessage cachedMsg = channel?.GetCachedMessage(data.Id); bool isCached = cachedMsg != null; if (isCached) { @@ -1365,7 +1352,7 @@ namespace Discord.WebSocket author = guild.GetUser(data.Author.Value.Id); } else - author = (channel as SocketChannel).GetUser(data.Author.Value.Id); + author = (channel as SocketChannel)?.GetUser(data.Author.Value.Id); if (author == null) { @@ -1381,17 +1368,32 @@ namespace Discord.WebSocket } else if (channel is SocketGroupChannel groupChannel) author = groupChannel.GetOrAddUser(data.Author.Value); - else - { - await UnknownChannelUserAsync(type, data.Author.Value.Id, channel.Id).ConfigureAwait(false); - return; - } } } else // Message author wasn't specified in the payload, so create a completely anonymous unknown user author = new SocketUnknownUser(this, id: 0); + if (channel == null) + { + if (!data.GuildId.IsSpecified) // assume it is a DM + { + if (data.Author.IsSpecified) + { + var dmChannel = CreateDMChannel(data.ChannelId, data.Author.Value, State); + channel = dmChannel; + author = dmChannel.Recipient; + } + else + channel = CreateDMChannel(data.ChannelId, author, State); + } + else + { + await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false); + return; + } + } + after = SocketMessage.Create(this, State, author, channel, data); } var cacheableBefore = new Cacheable(before, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id).ConfigureAwait(false)); @@ -1989,6 +1991,10 @@ namespace Discord.WebSocket { return SocketDMChannel.Create(this, state, channelId, model); } + internal SocketDMChannel CreateDMChannel(ulong channelId, SocketUser user, ClientState state) + { + return new SocketDMChannel(this, channelId, user); + } internal ISocketPrivateChannel RemovePrivateChannel(ulong id) { var channel = State.RemoveChannel(id) as ISocketPrivateChannel;