Implemented ClientDisconnect event for audio client. (#2520)

This commit is contained in:
Frederik P
2022-12-14 07:57:54 +01:00
committed by GitHub
parent 82b772ac03
commit 4cad546d57
4 changed files with 32 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ namespace Discord.Audio
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; }

View File

@@ -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; }
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
namespace Discord.Audio
@@ -47,5 +47,11 @@ namespace Discord.Audio
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>>();
}
}

View File

@@ -11,7 +11,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Generic;
namespace Discord.Audio
{
@@ -279,6 +279,15 @@ namespace Discord.Audio
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;