From 7cce63a0a3f770aa2d6932924c71a823ef8737d6 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Fri, 16 Mar 2018 23:30:12 +0000 Subject: [PATCH 1/7] Expose VoiceServerUpdate events --- .../BaseSocketClient.Events.cs | 10 +++++++++- .../DiscordSocketClient.cs | 4 ++++ .../Entities/Guilds/SocketVoiceServer.cs | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index e881a78550..cd302fa73a 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -1,4 +1,5 @@ -using System; +using Discord.WebSocket.Entities.Guilds; +using System; using System.Threading.Tasks; namespace Discord.WebSocket @@ -165,6 +166,13 @@ public event Func UserVoic remove { _userVoiceStateUpdatedEvent.Remove(value); } } internal readonly AsyncEvent> _userVoiceStateUpdatedEvent = new AsyncEvent>(); + /// Fired when the bot connects/disconnects to a Discord voice server. + public event Func VoiceServerUpdated + { + add { _voiceServerUpdatedEvent.Add(value); } + remove { _voiceServerUpdatedEvent.Remove(value); } + } + internal readonly AsyncEvent> _voiceServerUpdatedEvent = new AsyncEvent>(); /// Fired when the connected account is updated. public event Func CurrentUserUpdated { add { _selfUpdatedEvent.Add(value); } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 593f796c27..96b9e07b33 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -5,6 +5,7 @@ using Discord.Net.Udp; using Discord.Net.WebSockets; using Discord.Rest; +using Discord.WebSocket.Entities.Guilds; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -1474,6 +1475,9 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false); return; } + + SocketVoiceServer VoiceServer = new SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); + await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), VoiceServer).ConfigureAwait(false); } break; diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs new file mode 100644 index 0000000000..9874e971ed --- /dev/null +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs @@ -0,0 +1,19 @@ +using System.Diagnostics; + +namespace Discord.WebSocket.Entities.Guilds +{ + [DebuggerDisplay(@"{DebuggerDisplay,nq}")] + public class SocketVoiceServer + { + public ulong GuildId { get; private set; } + public string Endpoint { get; private set; } + public string Token { get; private set; } + + internal SocketVoiceServer(ulong GuildId, string Endpoint, string Token) + { + this.GuildId = GuildId; + this.Endpoint = Endpoint; + this.Token = Token; + } + } +} From 29db63d4d18d152006df554b1e41297a74f4d2d8 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Fri, 16 Mar 2018 23:47:49 +0000 Subject: [PATCH 2/7] Amend based on feedback --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 96b9e07b33..0c3677f99a 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -5,7 +5,7 @@ using Discord.Net.Udp; using Discord.Net.WebSockets; using Discord.Rest; -using Discord.WebSocket.Entities.Guilds; +using Discord.WebSocket; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -1465,6 +1465,10 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty var data = (payload as JToken).ToObject(_serializer); var guild = State.GetGuild(data.GuildId); + + var voiceServer = new Entities.Guilds.SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); + await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), voiceServer).ConfigureAwait(false); + if (guild != null) { string endpoint = data.Endpoint.Substring(0, data.Endpoint.LastIndexOf(':')); @@ -1476,8 +1480,6 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty return; } - SocketVoiceServer VoiceServer = new SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); - await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), VoiceServer).ConfigureAwait(false); } break; From 66e5af9eb315802a53b1e1d2581e8a40d6de5c46 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Fri, 16 Mar 2018 23:49:40 +0000 Subject: [PATCH 3/7] Move this out of guild entity --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 2 +- .../{Entities/Guilds => }/SocketVoiceServer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Discord.Net.WebSocket/{Entities/Guilds => }/SocketVoiceServer.cs (91%) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 0c3677f99a..584d3c44fa 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1466,7 +1466,7 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty var data = (payload as JToken).ToObject(_serializer); var guild = State.GetGuild(data.GuildId); - var voiceServer = new Entities.Guilds.SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); + var voiceServer = new SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), voiceServer).ConfigureAwait(false); if (guild != null) diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs b/src/Discord.Net.WebSocket/SocketVoiceServer.cs similarity index 91% rename from src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs rename to src/Discord.Net.WebSocket/SocketVoiceServer.cs index 9874e971ed..53940ab905 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketVoiceServer.cs +++ b/src/Discord.Net.WebSocket/SocketVoiceServer.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -namespace Discord.WebSocket.Entities.Guilds +namespace Discord.WebSocket { [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class SocketVoiceServer From 26bd2830fc241298fd550c6692c5d1001a964c53 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Fri, 16 Mar 2018 23:52:59 +0000 Subject: [PATCH 4/7] Fix namespace issue --- src/Discord.Net.WebSocket/BaseSocketClient.Events.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index cd302fa73a..df4dc9a9a9 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -1,4 +1,3 @@ -using Discord.WebSocket.Entities.Guilds; using System; using System.Threading.Tasks; From 669ba376e6fcb67bc538f38417a5bbfde8ac6607 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Sat, 17 Mar 2018 01:43:13 +0000 Subject: [PATCH 5/7] Adjust based on feedback #2 --- src/Discord.Net.WebSocket/BaseSocketClient.Events.cs | 2 +- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 1 - src/Discord.Net.WebSocket/SocketVoiceServer.cs | 10 ++++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index df4dc9a9a9..c236b10458 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -165,7 +165,7 @@ public event Func UserVoic remove { _userVoiceStateUpdatedEvent.Remove(value); } } internal readonly AsyncEvent> _userVoiceStateUpdatedEvent = new AsyncEvent>(); - /// Fired when the bot connects/disconnects to a Discord voice server. + /// Fired when the bot connects to a Discord voice server. public event Func VoiceServerUpdated { add { _voiceServerUpdatedEvent.Add(value); } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 584d3c44fa..49befb1543 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -5,7 +5,6 @@ using Discord.Net.Udp; using Discord.Net.WebSockets; using Discord.Rest; -using Discord.WebSocket; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; diff --git a/src/Discord.Net.WebSocket/SocketVoiceServer.cs b/src/Discord.Net.WebSocket/SocketVoiceServer.cs index 53940ab905..342c60655f 100644 --- a/src/Discord.Net.WebSocket/SocketVoiceServer.cs +++ b/src/Discord.Net.WebSocket/SocketVoiceServer.cs @@ -9,11 +9,13 @@ public class SocketVoiceServer public string Endpoint { get; private set; } public string Token { get; private set; } - internal SocketVoiceServer(ulong GuildId, string Endpoint, string Token) + internal SocketVoiceServer(ulong guildId, string endpoint, string token) { - this.GuildId = GuildId; - this.Endpoint = Endpoint; - this.Token = Token; + GuildId = guildId; + Endpoint = endpoint; + Token = token; } + + private string DebuggerDisplay => $"SocketVoiceServer ({GuildId})"; } } From 8e454fe46de6ba5451a1c63165caeceae3b7f4f8 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Sun, 18 Mar 2018 18:58:14 +0000 Subject: [PATCH 6/7] Use cacheable instead --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 4 +++- src/Discord.Net.WebSocket/SocketVoiceServer.cs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 49befb1543..a052b1ea27 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1464,8 +1464,10 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty var data = (payload as JToken).ToObject(_serializer); var guild = State.GetGuild(data.GuildId); + var cacheable = new Cacheable(guild, data.GuildId, guild != null, + async () => (IGuild) await ApiClient.GetGuildAsync(data.GuildId)); - var voiceServer = new SocketVoiceServer(data.GuildId, data.Endpoint, data.Token); + var voiceServer = new SocketVoiceServer(cacheable, data.GuildId, data.Endpoint, data.Token); await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), voiceServer).ConfigureAwait(false); if (guild != null) diff --git a/src/Discord.Net.WebSocket/SocketVoiceServer.cs b/src/Discord.Net.WebSocket/SocketVoiceServer.cs index 342c60655f..9ab6afd3f6 100644 --- a/src/Discord.Net.WebSocket/SocketVoiceServer.cs +++ b/src/Discord.Net.WebSocket/SocketVoiceServer.cs @@ -5,17 +5,17 @@ namespace Discord.WebSocket [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class SocketVoiceServer { - public ulong GuildId { get; private set; } + public Cacheable Guild { get; private set; } public string Endpoint { get; private set; } public string Token { get; private set; } - internal SocketVoiceServer(ulong guildId, string endpoint, string token) + internal SocketVoiceServer(Cacheable guild, ulong guildId, string endpoint, string token) { - GuildId = guildId; + Guild = guild; Endpoint = endpoint; Token = token; } - private string DebuggerDisplay => $"SocketVoiceServer ({GuildId})"; + private string DebuggerDisplay => $"SocketVoiceServer ({Guild.Id})"; } } From a4059134342fc36ff08a802f43c3d8d91da3dd11 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Fri, 4 May 2018 02:28:08 +0100 Subject: [PATCH 7/7] Change based on feedback --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index a052b1ea27..5ec00cc475 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1465,7 +1465,7 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty var data = (payload as JToken).ToObject(_serializer); var guild = State.GetGuild(data.GuildId); var cacheable = new Cacheable(guild, data.GuildId, guild != null, - async () => (IGuild) await ApiClient.GetGuildAsync(data.GuildId)); + async () => await ApiClient.GetGuildAsync(data.GuildId).ConfigureAwait(false) as IGuild); var voiceServer = new SocketVoiceServer(cacheable, data.GuildId, data.Endpoint, data.Token); await TimedInvokeAsync(_voiceServerUpdatedEvent, nameof(UserVoiceStateUpdated), voiceServer).ConfigureAwait(false);