Skip to content

Commit

Permalink
Fix #51 and add additional statements to null out the CurrentTrack if…
Browse files Browse the repository at this point in the history
… needed

- Fixed that the CurrentTrack is not set to null if the player state is NotPlaying
- Fixed that the player state was set to `Playing` where it should not (e.g. after running SkipAsync without having tracks in queue).
  • Loading branch information
angelobreuer committed Sep 9, 2020
1 parent 8ac93a7 commit c7517f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
13 changes: 4 additions & 9 deletions src/Lavalink4NET/Lavalink4NET.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/Lavalink4NET/Player/LavalinkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ public virtual async Task ConnectAsync(ulong voiceChannelId, bool selfDeaf = fal
{
await Client.SendVoiceUpdateAsync(GuildId, voiceChannelId, selfDeaf, selfMute);
VoiceChannelId = voiceChannelId;

State = PlayerState.NotPlaying;
CurrentTrack = null;
}

/// <summary>
Expand Down Expand Up @@ -199,6 +201,7 @@ public virtual Task OnTrackEndAsync(TrackEndEventArgs eventArgs)
{
// The track ended, set to not playing
State = PlayerState.NotPlaying;
CurrentTrack = null;
}

return Task.CompletedTask;
Expand Down Expand Up @@ -421,6 +424,9 @@ public virtual async Task StopAsync(bool disconnect = false)
{
await DisconnectAsync(PlayerDisconnectCause.Stop);
}

State = PlayerState.NotPlaying;
CurrentTrack = null;
}

/// <summary>
Expand Down Expand Up @@ -563,6 +569,7 @@ internal async Task UpdateAsync()
// set initial player state to connected, if player was not connected or destroyed,
// see: https://github.com/angelobreuer/Lavalink4NET/issues/28
State = PlayerState.NotPlaying;
CurrentTrack = null;
}

// trigger event
Expand Down
6 changes: 3 additions & 3 deletions src/Lavalink4NET/Player/QueuedLavalinkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ public virtual Task SkipAsync(int count = 1)
// a track to play was found, dequeue and play
return PlayAsync(track!, false);
}
// no tracks queued, disconnect if wanted
else if (_disconnectOnStop)
// no tracks queued, stop player and disconnect if specified
else
{
return DisconnectAsync();
StopAsync(disconnect: _disconnectOnStop);
}

return Task.CompletedTask;
Expand Down
15 changes: 8 additions & 7 deletions src/Lavalink4NET/Tracking/DefaultInactivityTrackers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ namespace Lavalink4NET.Tracking
{
using System.Linq;
using System.Threading.Tasks;
using Lavalink4NET.Player;

/// <summary>
/// A set of default out-of-box inactivity trackers.
/// </summary>
public static class DefaultInactivityTrackers
{
/// <summary>
/// An inactivity tracker ( <see cref="InactivityTracker"/>) which marks a player as
/// "inactive" when the player is not playing a track.
/// </summary>
public static InactivityTracker ChannelInactivityTracker { get; } = (player, _)
=> Task.FromResult(player.State is PlayerState.NotPlaying);

/// <summary>
/// An inactivity tracker ( <see cref="InactivityTracker"/>) which marks a player as
/// "inactive" when there are no users in the channel except the bot itself.
Expand All @@ -55,12 +63,5 @@ public static class DefaultInactivityTrackers
// check if there are no users in the channel (bot excluded)
return userCount == 0;
};

/// <summary>
/// An inactivity tracker ( <see cref="InactivityTracker"/>) which marks a player as
/// "inactive" when the player is not playing a track.
/// </summary>
public static InactivityTracker ChannelInactivityTracker { get; } = (player, _)
=> Task.FromResult(player.State == Player.PlayerState.NotPlaying);
}
}

0 comments on commit c7517f2

Please sign in to comment.