Skip to content

Commit

Permalink
feat: Add player state preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Jan 21, 2024
1 parent 9774152 commit 8ecb4fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/PlayerPrecondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public static IPlayerPrecondition All(ImmutableArray<IPlayerPrecondition> precon

public static IPlayerPrecondition All(params IPlayerPrecondition[] preconditions)
=> new AggregateAllPrecondition(preconditions.ToImmutableArray());

public static IPlayerPrecondition Status(ImmutableArray<PlayerState> states)
=> new PlayerStatePrecondition(states);

public static IPlayerPrecondition Status(params PlayerState[] states)
=> new PlayerStatePrecondition(states.ToImmutableArray());
}
17 changes: 17 additions & 0 deletions src/Lavalink4NET/Players/Preconditions/PlayerStatePrecondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Lavalink4NET.Players.Preconditions;

using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

internal sealed record class PlayerStatePrecondition(ImmutableArray<PlayerState> AllowedStates) : IPlayerPrecondition
{
public ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentNullException.ThrowIfNull(player);

return new ValueTask<bool>(AllowedStates.Contains(player.State));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public ValueTask<bool> CheckAsync(ILavalinkPlayer player, CancellationToken canc

return new ValueTask<bool>(player.State == State);
}
}
}

0 comments on commit 8ecb4fe

Please sign in to comment.