From de0c303c230e053cd32c991f39ab07c20a3748e1 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Thu, 13 Jul 2017 07:26:37 -0400 Subject: [PATCH 1/2] Try to pull DM channels from cache on CHANNEL_CREATE This resoles #741 Since Discord now dispatches a CHANNEL_CREATE for every message sent to a bot, we check to see if a channel has already been added to the state before creating a new one. --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index b13ceca1d..828292a71 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1631,7 +1631,10 @@ namespace Discord.WebSocket internal ISocketPrivateChannel AddPrivateChannel(API.Channel model, ClientState state) { - var channel = SocketChannel.CreatePrivate(this, state, model); + ISocketPrivateChannel channel; + if ((channel = state.GetChannel(model.Id) as ISocketPrivateChannel) != null) + return channel; + channel = SocketChannel.CreatePrivate(this, state, model); state.AddChannel(channel as SocketChannel); if (channel is SocketDMChannel dm) dm.Recipient.GlobalUser.DMChannel = dm; From 7f880806f29d02ea352f6b541a5d101dec1b3c7f Mon Sep 17 00:00:00 2001 From: Christopher F Date: Thu, 13 Jul 2017 09:16:06 -0400 Subject: [PATCH 2/2] Use C#7 pattern matching --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 828292a71..1511a7e88 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1631,8 +1631,7 @@ namespace Discord.WebSocket internal ISocketPrivateChannel AddPrivateChannel(API.Channel model, ClientState state) { - ISocketPrivateChannel channel; - if ((channel = state.GetChannel(model.Id) as ISocketPrivateChannel) != null) + if (state.GetChannel(model.Id) is ISocketPrivateChannel channel) return channel; channel = SocketChannel.CreatePrivate(this, state, model); state.AddChannel(channel as SocketChannel);