Skip to content

Commit

Permalink
Expose VoiceServerUpdate events (discord-net#984)
Browse files Browse the repository at this point in the history
* Expose VoiceServerUpdate events

* Amend based on feedback

* Move this out of guild entity

* Fix namespace issue

* Adjust based on feedback #2

* Use cacheable instead

* Change based on feedback
  • Loading branch information
Luke authored and foxbot committed May 4, 2018
1 parent 660fec0 commit e775853
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Discord.Net.WebSocket/BaseSocketClient.Events.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;

namespace Discord.WebSocket
Expand Down Expand Up @@ -165,6 +165,13 @@ public event Func<SocketUser, SocketVoiceState, SocketVoiceState, Task> UserVoic
remove { _userVoiceStateUpdatedEvent.Remove(value); }
}
internal readonly AsyncEvent<Func<SocketUser, SocketVoiceState, SocketVoiceState, Task>> _userVoiceStateUpdatedEvent = new AsyncEvent<Func<SocketUser, SocketVoiceState, SocketVoiceState, Task>>();
/// <summary> Fired when the bot connects to a Discord voice server. </summary>
public event Func<SocketVoiceServer, Task> VoiceServerUpdated
{
add { _voiceServerUpdatedEvent.Add(value); }
remove { _voiceServerUpdatedEvent.Remove(value); }
}
internal readonly AsyncEvent<Func<SocketVoiceServer, Task>> _voiceServerUpdatedEvent = new AsyncEvent<Func<SocketVoiceServer, Task>>();
/// <summary> Fired when the connected account is updated. </summary>
public event Func<SocketSelfUser, SocketSelfUser, Task> CurrentUserUpdated {
add { _selfUpdatedEvent.Add(value); }
Expand Down
7 changes: 7 additions & 0 deletions src/Discord.Net.WebSocket/DiscordSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,12 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty

var data = (payload as JToken).ToObject<VoiceServerUpdateEvent>(_serializer);
var guild = State.GetGuild(data.GuildId);
var cacheable = new Cacheable<IGuild, ulong>(guild, data.GuildId, guild != null,
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);

if (guild != null)
{
string endpoint = data.Endpoint.Substring(0, data.Endpoint.LastIndexOf(':'));
Expand All @@ -1476,6 +1482,7 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
return;
}

}
break;

Expand Down
21 changes: 21 additions & 0 deletions src/Discord.Net.WebSocket/SocketVoiceServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Diagnostics;

namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class SocketVoiceServer
{
public Cacheable<IGuild, ulong> Guild { get; private set; }
public string Endpoint { get; private set; }
public string Token { get; private set; }

internal SocketVoiceServer(Cacheable<IGuild, ulong> guild, ulong guildId, string endpoint, string token)
{
Guild = guild;
Endpoint = endpoint;
Token = token;
}

private string DebuggerDisplay => $"SocketVoiceServer ({Guild.Id})";
}
}

0 comments on commit e775853

Please sign in to comment.