@@ -205,6 +205,9 @@ | |||||
<Compile Include="..\Discord.Net\DiscordClient.Users.cs"> | <Compile Include="..\Discord.Net\DiscordClient.Users.cs"> | ||||
<Link>DiscordClient.Users.cs</Link> | <Link>DiscordClient.Users.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net\DiscordClient.Voice.cs"> | |||||
<Link>DiscordClient.Voice.cs</Link> | |||||
</Compile> | |||||
<Compile Include="..\Discord.Net\DiscordClientConfig.cs"> | <Compile Include="..\Discord.Net\DiscordClientConfig.cs"> | ||||
<Link>DiscordClientConfig.cs</Link> | <Link>DiscordClientConfig.cs</Link> | ||||
</Compile> | </Compile> | ||||
@@ -0,0 +1,84 @@ | |||||
using Discord.Audio; | |||||
using System; | |||||
namespace Discord | |||||
{ | |||||
public partial class DiscordClient | |||||
{ | |||||
public IDiscordVoiceClient GetVoiceClient(Server server) | |||||
=> GetVoiceClient(server.Id); | |||||
public IDiscordVoiceClient GetVoiceClient(string serverId) | |||||
{ | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (!Config.EnableVoiceMultiserver) | |||||
{ | |||||
if (serverId == _voiceServerId) | |||||
return this; | |||||
else | |||||
return null; | |||||
} | |||||
DiscordWSClient client; | |||||
if (_voiceClients.TryGetValue(serverId, out client)) | |||||
return client; | |||||
else | |||||
return null; | |||||
} | |||||
private async Task<IDiscordVoiceClient> CreateVoiceClient(string serverId) | |||||
{ | |||||
if (!Config.EnableVoiceMultiserver) | |||||
{ | |||||
_voiceServerId = serverId; | |||||
return this; | |||||
} | |||||
var client = _voiceClients.GetOrAdd(serverId, _ => | |||||
{ | |||||
var config = _config.Clone(); | |||||
config.LogLevel = _config.LogLevel;// (LogMessageSeverity)Math.Min((int)_config.LogLevel, (int)LogMessageSeverity.Warning); | |||||
config.VoiceOnly = true; | |||||
config.VoiceClientId = unchecked(++_nextVoiceClientId); | |||||
return new DiscordWSClient(config, serverId); | |||||
}); | |||||
client.LogMessage += (s, e) => RaiseOnLog(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}"); | |||||
await client.Connect(_gateway, _token).ConfigureAwait(false); | |||||
return client; | |||||
} | |||||
public Task<IDiscordVoiceClient> JoinVoiceServer(Channel channel) | |||||
=> JoinVoiceServer(channel?.ServerId, channel?.Id); | |||||
public Task<IDiscordVoiceClient> JoinVoiceServer(Server server, string channelId) | |||||
=> JoinVoiceServer(server?.Id, channelId); | |||||
public async Task<IDiscordVoiceClient> JoinVoiceServer(string serverId, string channelId) | |||||
{ | |||||
CheckReady(); //checkVoice is done inside the voice client | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId)); | |||||
var client = await CreateVoiceClient(serverId).ConfigureAwait(false); | |||||
await client.JoinChannel(channelId).ConfigureAwait(false); | |||||
return client; | |||||
} | |||||
public Task LeaveVoiceServer(Server server) | |||||
=> LeaveVoiceServer(server?.Id); | |||||
public async Task LeaveVoiceServer(string serverId) | |||||
{ | |||||
CheckReady(checkVoice: true); | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (Config.EnableVoiceMultiserver) | |||||
{ | |||||
DiscordWSClient client; | |||||
if (_voiceClients.TryRemove(serverId, out client)) | |||||
await client.Disconnect().ConfigureAwait(false); | |||||
} | |||||
else | |||||
{ | |||||
await _voiceSocket.Disconnect().ConfigureAwait(false); | |||||
_dataSocket.SendLeaveVoice(serverId); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -1,14 +1,11 @@ | |||||
using Discord.API; | using Discord.API; | ||||
using Discord.Audio; | |||||
using Discord.Collections; | using Discord.Collections; | ||||
using Discord.Net; | |||||
using Discord.Net.WebSockets; | using Discord.Net.WebSockets; | ||||
using Newtonsoft.Json; | using Newtonsoft.Json; | ||||
using System; | using System; | ||||
using System.Collections.Concurrent; | using System.Collections.Concurrent; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Net; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Discord | namespace Discord | ||||
@@ -714,82 +711,6 @@ namespace Discord | |||||
} | } | ||||
} | } | ||||
public IDiscordVoiceClient GetVoiceClient(Server server) | |||||
=> GetVoiceClient(server.Id); | |||||
public IDiscordVoiceClient GetVoiceClient(string serverId) | |||||
{ | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (!Config.EnableVoiceMultiserver) | |||||
{ | |||||
if (serverId == _voiceServerId) | |||||
return this; | |||||
else | |||||
return null; | |||||
} | |||||
DiscordWSClient client; | |||||
if (_voiceClients.TryGetValue(serverId, out client)) | |||||
return client; | |||||
else | |||||
return null; | |||||
} | |||||
private async Task<IDiscordVoiceClient> CreateVoiceClient(string serverId) | |||||
{ | |||||
if (!Config.EnableVoiceMultiserver) | |||||
{ | |||||
_voiceServerId = serverId; | |||||
return this; | |||||
} | |||||
var client = _voiceClients.GetOrAdd(serverId, _ => | |||||
{ | |||||
var config = _config.Clone(); | |||||
config.LogLevel = _config.LogLevel;// (LogMessageSeverity)Math.Min((int)_config.LogLevel, (int)LogMessageSeverity.Warning); | |||||
config.VoiceOnly = true; | |||||
config.VoiceClientId = unchecked(++_nextVoiceClientId); | |||||
return new DiscordWSClient(config, serverId); | |||||
}); | |||||
client.LogMessage += (s, e) => RaiseOnLog(e.Severity, e.Source, $"(#{client.Config.VoiceClientId}) {e.Message}"); | |||||
await client.Connect(_gateway, _token).ConfigureAwait(false); | |||||
return client; | |||||
} | |||||
public Task<IDiscordVoiceClient> JoinVoiceServer(Channel channel) | |||||
=> JoinVoiceServer(channel?.ServerId, channel?.Id); | |||||
public Task<IDiscordVoiceClient> JoinVoiceServer(Server server, string channelId) | |||||
=> JoinVoiceServer(server?.Id, channelId); | |||||
public async Task<IDiscordVoiceClient> JoinVoiceServer(string serverId, string channelId) | |||||
{ | |||||
CheckReady(); //checkVoice is done inside the voice client | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId)); | |||||
var client = await CreateVoiceClient(serverId).ConfigureAwait(false); | |||||
await client.JoinChannel(channelId).ConfigureAwait(false); | |||||
return client; | |||||
} | |||||
public Task LeaveVoiceServer(Server server) | |||||
=> LeaveVoiceServer(server?.Id); | |||||
public async Task LeaveVoiceServer(string serverId) | |||||
{ | |||||
CheckReady(checkVoice: true); | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (Config.EnableVoiceMultiserver) | |||||
{ | |||||
DiscordWSClient client; | |||||
if (_voiceClients.TryRemove(serverId, out client)) | |||||
await client.Disconnect().ConfigureAwait(false); | |||||
} | |||||
else | |||||
{ | |||||
await _voiceSocket.Disconnect().ConfigureAwait(false); | |||||
_dataSocket.SendLeaveVoice(serverId); | |||||
} | |||||
} | |||||
private void SendInitialLog() | private void SendInitialLog() | ||||
{ | { | ||||
if (_config.LogLevel >= LogMessageSeverity.Verbose) | if (_config.LogLevel >= LogMessageSeverity.Verbose) | ||||