Skip to content

Commit

Permalink
Implemented ClientDisconnect event for audio client. (#2520)
Browse files Browse the repository at this point in the history
  • Loading branch information
F0903 committed Dec 14, 2022
1 parent 82b772a commit 4cad546
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Discord.Net.Core/Audio/IAudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IAudioClient : IDisposable
event Func<ulong, AudioInStream, Task> StreamCreated;
event Func<ulong, Task> StreamDestroyed;
event Func<ulong, bool, Task> SpeakingUpdated;
event Func<ulong, Task> ClientDisconnected;

/// <summary> Gets the current connection state of this client. </summary>
ConnectionState ConnectionState { get; }
Expand Down
14 changes: 14 additions & 0 deletions src/Discord.Net.WebSocket/API/Voice/ClientDisconnectEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.API.Voice;
internal class ClientDisconnectEvent
{
[JsonProperty("user_id")]
public ulong UserId { get; set; }
}
8 changes: 7 additions & 1 deletion src/Discord.Net.WebSocket/Audio/AudioClient.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.Audio
Expand Down Expand Up @@ -47,5 +47,11 @@ internal partial class AudioClient
remove { _speakingUpdatedEvent.Remove(value); }
}
private readonly AsyncEvent<Func<ulong, bool, Task>> _speakingUpdatedEvent = new AsyncEvent<Func<ulong, bool, Task>>();
public event Func<ulong, Task> ClientDisconnected
{
add { _clientDisconnectedEvent.Add(value); }
remove { _clientDisconnectedEvent.Remove(value); }
}
private readonly AsyncEvent<Func<ulong, Task>> _clientDisconnectedEvent = new AsyncEvent<Func<ulong, Task>>();
}
}
11 changes: 10 additions & 1 deletion src/Discord.Net.WebSocket/Audio/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Generic;

namespace Discord.Audio
{
Expand Down Expand Up @@ -279,6 +279,15 @@ private async Task ProcessMessageAsync(VoiceOpCode opCode, object payload)
await _speakingUpdatedEvent.InvokeAsync(data.UserId, data.Speaking);
}
break;
case VoiceOpCode.ClientDisconnect:
{
await _audioLogger.DebugAsync("Received ClientDisconnect").ConfigureAwait(false);

var data = (payload as JToken).ToObject<ClientDisconnectEvent>(_serializer);

await _clientDisconnectedEvent.InvokeAsync(data.UserId);
}
break;
default:
await _audioLogger.WarningAsync($"Unknown OpCode ({opCode})").ConfigureAwait(false);
return;
Expand Down

0 comments on commit 4cad546

Please sign in to comment.