From de0c303c230e053cd32c991f39ab07c20a3748e1 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Thu, 13 Jul 2017 07:26:37 -0400 Subject: [PATCH 1/3] 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/3] 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); From 4bfd3be573bbc1daf74dd79fc3c7e72c73777eb8 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 15 Jul 2017 22:35:31 -0400 Subject: [PATCH 3/3] Disabled ratelimits on reactions by scrambling the bucket ID --- src/Discord.Net.Rest/DiscordRestApiClient.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index 1fac66ec5..179f88b76 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -577,7 +577,8 @@ namespace Discord.API options = RequestOptions.CreateOrClone(options); - var ids = new BucketIds(channelId: channelId); + var id = new Random().Next(0, int.MaxValue); + var ids = new BucketIds(channelId: (ulong)id); await SendAsync("PUT", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/@me", ids, options: options).ConfigureAwait(false); } @@ -589,7 +590,8 @@ namespace Discord.API options = RequestOptions.CreateOrClone(options); - var ids = new BucketIds(channelId: channelId); + var id = new Random().Next(0, int.MaxValue); + var ids = new BucketIds(channelId: (ulong)id); await SendAsync("DELETE", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}/{userId}", ids, options: options).ConfigureAwait(false); }