This allows developers to handle the AudioClient externally (for example with Lavalink)pull/1057/head
| @@ -1,4 +1,4 @@ | |||||
| using Discord.Audio; | |||||
| using Discord.Audio; | |||||
| using System; | using System; | ||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| @@ -8,5 +8,8 @@ namespace Discord | |||||
| { | { | ||||
| /// <summary> Connects to this audio channel. </summary> | /// <summary> Connects to this audio channel. </summary> | ||||
| Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null); | Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null); | ||||
| /// <summary> Connects to this audio channel but can specify if client is handled externally. </summary> | |||||
| Task<IAudioClient> ConnectAsync(bool external, Action<IAudioClient> configAction = null); | |||||
| } | } | ||||
| } | } | ||||
| @@ -144,6 +144,7 @@ namespace Discord.Rest | |||||
| //IAudioChannel | //IAudioChannel | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); } | |||||
| //IChannel | //IChannel | ||||
| Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
| @@ -1,4 +1,4 @@ | |||||
| using Discord.Audio; | |||||
| using Discord.Audio; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Diagnostics; | using System.Diagnostics; | ||||
| @@ -42,6 +42,7 @@ namespace Discord.Rest | |||||
| //IAudioChannel | //IAudioChannel | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); } | |||||
| //IGuildChannel | //IGuildChannel | ||||
| Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
| @@ -207,6 +207,7 @@ namespace Discord.WebSocket | |||||
| //IAudioChannel | //IAudioChannel | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); } | ||||
| Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); } | |||||
| //IChannel | //IChannel | ||||
| Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
| @@ -1,4 +1,4 @@ | |||||
| using Discord.Audio; | |||||
| using Discord.Audio; | |||||
| using Discord.Rest; | using Discord.Rest; | ||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| @@ -42,7 +42,12 @@ namespace Discord.WebSocket | |||||
| public async Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null) | public async Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null) | ||||
| { | { | ||||
| return await Guild.ConnectAudioAsync(Id, false, false, configAction).ConfigureAwait(false); | |||||
| return await Guild.ConnectAudioAsync(Id, false, false, configAction, false).ConfigureAwait(false); | |||||
| } | |||||
| public async Task<IAudioClient> ConnectAsync(bool external, Action<IAudioClient> configAction = null) | |||||
| { | |||||
| return await Guild.ConnectAudioAsync(Id, false, false, configAction, external).ConfigureAwait(false); | |||||
| } | } | ||||
| public override SocketGuildUser GetUser(ulong id) | public override SocketGuildUser GetUser(ulong id) | ||||
| @@ -504,7 +504,7 @@ namespace Discord.WebSocket | |||||
| { | { | ||||
| return _audioClient?.GetInputStream(userId); | 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, Action<IAudioClient> configAction, bool external) | |||||
| { | { | ||||
| selfDeaf = false; | selfDeaf = false; | ||||
| selfMute = false; | selfMute = false; | ||||
| @@ -518,29 +518,37 @@ namespace Discord.WebSocket | |||||
| promise = new TaskCompletionSource<AudioClient>(); | promise = new TaskCompletionSource<AudioClient>(); | ||||
| _audioConnectPromise = promise; | _audioConnectPromise = promise; | ||||
| if (_audioClient == null) | |||||
| if (!external) | |||||
| { | { | ||||
| var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId); | |||||
| audioClient.Disconnected += async ex => | |||||
| if (_audioClient == null) | |||||
| { | { | ||||
| if (!promise.Task.IsCompleted) | |||||
| var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId); | |||||
| audioClient.Disconnected += async ex => | |||||
| { | { | ||||
| try { audioClient.Dispose(); } catch { } | |||||
| _audioClient = null; | |||||
| if (ex != null) | |||||
| await promise.TrySetExceptionAsync(ex); | |||||
| else | |||||
| await promise.TrySetCanceledAsync(); | |||||
| return; | |||||
| } | |||||
| }; | |||||
| audioClient.Connected += () => | |||||
| { | |||||
| var _ = promise.TrySetResultAsync(_audioClient); | |||||
| return Task.Delay(0); | |||||
| }; | |||||
| configAction?.Invoke(audioClient); | |||||
| _audioClient = audioClient; | |||||
| if (!promise.Task.IsCompleted) | |||||
| { | |||||
| try | |||||
| { audioClient.Dispose(); } | |||||
| catch { } | |||||
| _audioClient = null; | |||||
| if (ex != null) | |||||
| await promise.TrySetExceptionAsync(ex); | |||||
| else | |||||
| await promise.TrySetCanceledAsync(); | |||||
| return; | |||||
| } | |||||
| }; | |||||
| audioClient.Connected += () => | |||||
| { | |||||
| var _ = promise.TrySetResultAsync(_audioClient); | |||||
| return Task.Delay(0); | |||||
| }; | |||||
| configAction?.Invoke(audioClient); | |||||
| _audioClient = audioClient; | |||||
| } | |||||
| } else | |||||
| { | |||||
| var _ = promise.TrySetResultAsync(null); | |||||
| } | } | ||||
| await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); | await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); | ||||