Allow external VoiceChannel client API implementation (#1057)

* ConnectAsync(bool external) API implemented

This allows developers to handle the AudioClient externally (for example with Lavalink)

* Modify ConnectAsync API and add DisconnectAsync to IAudioChannel

* Update summary message on IAudioChannel.DisconnectAsync()

* Review changes:
- Fix `if (!external)` styling
- Remove unecessary ConnectAsync overload

* SocketVoiceChannel overload update

* Update IAudioChannel.ConnectAsync()
- Remove ConfigAction from parameters
- Add SelfDeafen/SelfMute to parameters

* Remove SocketVoiceChannel.ConnectAsync() default params (Inherit)
This commit is contained in:
NovusTheory
2018-05-31 16:21:57 -05:00
committed by Finite Reality
parent 3acf2a9a6b
commit 890904f32c
6 changed files with 32 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using System;
using System.Threading.Tasks;
@@ -7,6 +7,9 @@ namespace Discord
public interface IAudioChannel : IChannel
{
/// <summary> Connects to this audio channel. </summary>
Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null);
Task<IAudioClient> ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false);
/// <summary> Disconnects from this audio channel. </summary>
Task DisconnectAsync();
}
}

View File

@@ -147,7 +147,8 @@ namespace Discord.Rest
=> EnterTypingState(options);
//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -45,7 +45,8 @@ namespace Discord.Rest
private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IGuildChannel
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -211,7 +211,8 @@ namespace Discord.WebSocket
=> EnterTypingState(options);
//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool selfDeaf, bool selfMute, bool external) { throw new NotSupportedException(); }
Task IAudioChannel.DisconnectAsync() { throw new NotSupportedException(); }
//IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)

View File

@@ -43,11 +43,14 @@ namespace Discord.WebSocket
public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
=> ChannelHelper.ModifyAsync(this, Discord, func, options);
public async Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null)
public async Task<IAudioClient> ConnectAsync(bool selfDeaf, bool selfMute, bool external)
{
return await Guild.ConnectAudioAsync(Id, false, false, configAction).ConfigureAwait(false);
return await Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external).ConfigureAwait(false);
}
public async Task DisconnectAsync()
=> await Guild.DisconnectAudioAsync();
public override SocketGuildUser GetUser(ulong id)
{
var user = Guild.GetUser(id);

View File

@@ -508,11 +508,8 @@ namespace Discord.WebSocket
{
return _audioClient?.GetInputStream(userId);
}
internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, Action<IAudioClient> configAction)
internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, bool external)
{
selfDeaf = false;
selfMute = false;
TaskCompletionSource<AudioClient> promise;
await _audioLock.WaitAsync().ConfigureAwait(false);
@@ -522,6 +519,13 @@ namespace Discord.WebSocket
promise = new TaskCompletionSource<AudioClient>();
_audioConnectPromise = promise;
if (external)
{
var _ = promise.TrySetResultAsync(null);
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
return null;
}
if (_audioClient == null)
{
var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId);
@@ -529,7 +533,9 @@ namespace Discord.WebSocket
{
if (!promise.Task.IsCompleted)
{
try { audioClient.Dispose(); } catch { }
try
{ audioClient.Dispose(); }
catch { }
_audioClient = null;
if (ex != null)
await promise.TrySetExceptionAsync(ex);
@@ -543,7 +549,6 @@ namespace Discord.WebSocket
var _ = promise.TrySetResultAsync(_audioClient);
return Task.Delay(0);
};
configAction?.Invoke(audioClient);
_audioClient = audioClient;
}
@@ -602,8 +607,11 @@ namespace Discord.WebSocket
await _audioLock.WaitAsync().ConfigureAwait(false);
try
{
await RepopulateAudioStreamsAsync().ConfigureAwait(false);
await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
if (_audioClient != null)
{
await RepopulateAudioStreamsAsync().ConfigureAwait(false);
await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
}
}
catch (OperationCanceledException)
{