[Command("skip", RunMode=RunMode.Async)]
public async Task SkipTrack()
{
var userChannel = (Context.User as IGuildUser).VoiceChannel;
var guild = Context.Guild;
var clientVoiceChannel = guild.CurrentUser.VoiceChannel;
if (clientVoiceChannel == null) { await userChannel.SendMessageAsync("I'm not connected to any voice channel"); return; }
await _audioService.SkipTrackAsync(userChannel, guild);
}
public async Task SkipTrackAsync(IVoiceChannel clientChannel, IGuild guild)
{
if (GuildVoiceClientManager.Clients.TryGetValue(guild.Id, out var client))
{
try
{
await client.TrackManager.SkipCurrentTrack();
}
catch (OperationCanceledException)
{
Console.WriteLine("Skipped");
}
}
}
public async Task SkipCurrentTrack()
{
await PlayNextTrack();
}
public async Task PlayNextTrack()
{
AudioTrack nextTrack;
if (TrackQueue.Any())
{
nextTrack = TrackQueue.Dequeue();
await _audioPlayer.StartTrackAsync(nextTrack);
}
else
{
_audioPlayer.Stop();
}
}
public async Task StartTrackAsync(AudioTrack audioTrack)
{
if(audioTrack == null)
{
Console.WriteLine("No track provided");
return;
}
if(PlayingTrack != null)
{
Stop();
}
this.PlayingTrack = audioTrack;
Console.WriteLine($"Playing {PlayingTrack.AudioTrackInfo.Title}");
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
await OnTrackStartAsync(AudioClient, PlayingTrack).ConfigureAwait(false);
await TrackLoopAsync(PlayingTrack, _cancellationTokenSource.Token).ConfigureAwait(false);
await OnTrackEndAsync(AudioClient, _cancellationTokenSource.Token).ConfigureAwait(false);
}
public void Stop()
{
try
{
_cancellationTokenSource.Cancel(false);
}
catch (ObjectDisposedException) { }
}