Browse Source

ConnectAsync(bool external) API implemented

This allows developers to handle the AudioClient externally (for example with Lavalink)
pull/1057/head
NovusTheory 7 years ago
parent
commit
63d2a42c5e
6 changed files with 44 additions and 25 deletions
  1. +4
    -1
      src/Discord.Net.Core/Entities/Channels/IAudioChannel.cs
  2. +1
    -0
      src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs
  3. +2
    -1
      src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
  4. +1
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs
  5. +7
    -2
      src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
  6. +29
    -21
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 4
- 1
src/Discord.Net.Core/Entities/Channels/IAudioChannel.cs View File

@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using System;
using System.Threading.Tasks;

@@ -8,5 +8,8 @@ namespace Discord
{
/// <summary> Connects to this audio channel. </summary>
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);
}
}

+ 1
- 0
src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs View File

@@ -144,6 +144,7 @@ namespace Discord.Rest

//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); }

//IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)


+ 2
- 1
src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs View File

@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -42,6 +42,7 @@ namespace Discord.Rest

//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); }

//IGuildChannel
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)


+ 1
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs View File

@@ -207,6 +207,7 @@ namespace Discord.WebSocket

//IAudioChannel
Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
Task<IAudioClient> IAudioChannel.ConnectAsync(bool external, Action<IAudioClient> configAction) { throw new NotSupportedException(); }

//IChannel
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)


+ 7
- 2
src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs View File

@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using Discord.Rest;
using System;
using System.Collections.Generic;
@@ -42,7 +42,12 @@ namespace Discord.WebSocket

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)


+ 29
- 21
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -504,7 +504,7 @@ 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, Action<IAudioClient> configAction, bool external)
{
selfDeaf = false;
selfMute = false;
@@ -518,29 +518,37 @@ namespace Discord.WebSocket
promise = new TaskCompletionSource<AudioClient>();
_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);


Loading…
Cancel
Save