#Need to integrate skipping songs to my music bot

1 messages · Page 1 of 1 (latest)

sharp osprey
#
[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) { }
}
#
private async Task TrackLoopAsync(AudioTrack audioTrack, CancellationToken cancellationToken)
{
    int read = -1;
    try
    {
        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (DiscordStream == null)
            {
                Console.Error.WriteLine("Error when playing the audio track: Discord stream gone");
                return;
            }

            if (!_paused)
            {
                read = await audioTrack.AudioTrackStream.ReadAsync(audioTrack.BufferFrame, 0, audioTrack.BufferFrame.Length, cancellationToken).ConfigureAwait(false);

                if (read > 0)
                {
                    await DiscordStream.WriteAsync(audioTrack.BufferFrame, 0, read, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    return;
                }
            }
            else
            {
                await Task.Delay(1000, cancellationToken);
            }
        }
    }
    catch (OperationCanceledException)
    {
        PlayingTrack?.Dispose();
        PlayingTrack = null;
    }
    catch (ObjectDisposedException)
    {
        PlayingTrack?.Dispose();
        PlayingTrack = null;
    }
    
}
#

This might be the most interesting part of the code related to the problem I'm encountering. TLDR, whenever I call the skip command the code will just run the StartTrackAsync event. Basically what it is doing is: If a track is being played then stop the current track by requesting a cancellation for the running tasks. After that the TrackLoopAsync Task should be terminated and run the code inside the catch. After that, the code should follow its flow running the remaining code inside the StartTrackAsync Task. Here comes the problem. Since it is asynchronous, it's not working like that

#

Sometimes, after the Close() method, follows the assignemet of PlayingTrack to the new track that should be played but I expect that the TrackLoopAsync Task should be killed before the assignment of the new track. In fact, sometimes I get the NullReferenceException since the AudioTrackStream is null after the PlayingTrack= newTrack assignment

covert creek
#

so... i hav no idea how to solve your problem

#

but i am interested in what the planed source of your music is

#

i am tinkering with a personal music bot and i settled on streaming amazon music from my device

#

but thats like barly a music bot