Skip to content

Commit

Permalink
Merge pull request #179 from SKProCH/overrideSessionProvider
Browse files Browse the repository at this point in the history
Allow overriding ILavalinkSessionProvider while resolving/joining player
  • Loading branch information
SKProCH authored Sep 8, 2024
2 parents 13954de + 604d09b commit 03cc0a8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Lavalink4NET/Players/IPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChanne
where TPlayer : ILavalinkPlayer
where TOptions : LavalinkPlayerOptions;

ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChannelId,
PlayerFactory<TPlayer, TOptions> playerFactory, IOptions<TOptions> options,
ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default)
where TPlayer : ILavalinkPlayer
where TOptions : LavalinkPlayerOptions
{
return JoinAsync(guildId, voiceChannelId, playerFactory, options, cancellationToken);
}

ValueTask<PlayerResult<TPlayer>> RetrieveAsync<TPlayer, TOptions>(
ulong guildId,
ulong? memberVoiceChannel,
Expand Down
19 changes: 16 additions & 3 deletions src/Lavalink4NET/Players/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,31 @@ public bool HasPlayer(ulong guildId)
return _handles.TryGetValue(guildId, out var handle)
&& handle is { Player.State: not PlayerState.Destroyed };
}

public ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChannelId, PlayerFactory<TPlayer, TOptions> playerFactory, IOptions<TOptions> options, CancellationToken cancellationToken = default)
where TPlayer : ILavalinkPlayer
where TOptions : LavalinkPlayerOptions
{
return JoinAsync(guildId, voiceChannelId, playerFactory, options, null, cancellationToken);
}

public async ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChannelId, PlayerFactory<TPlayer, TOptions> playerFactory, IOptions<TOptions> options, CancellationToken cancellationToken = default)
public async ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChannelId, PlayerFactory<TPlayer, TOptions> playerFactory, IOptions<TOptions> options, ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default)
where TPlayer : ILavalinkPlayer
where TOptions : LavalinkPlayerOptions
{
cancellationToken.ThrowIfCancellationRequested();

LavalinkPlayerHandle<TPlayer, TOptions> Create(ulong guildId)
{
var playerContext = _playerContext;
if (overridenLavalinkSessionProvider is not null)
{
playerContext = playerContext with { SessionProvider = overridenLavalinkSessionProvider };
}

return new LavalinkPlayerHandle<TPlayer, TOptions>(
guildId: guildId,
playerContext: _playerContext,
playerContext: playerContext,
playerFactory: playerFactory,
options: options,
logger: _loggerFactory.CreateLogger<TPlayer>());
Expand Down Expand Up @@ -296,7 +309,7 @@ await DiscordClient
return PlayerResult<TPlayer>.BotNotConnected;
}

player = await JoinAsync(guildId, memberVoiceChannel.Value, playerFactory, options, cancellationToken).ConfigureAwait(false);
player = await JoinAsync(guildId, memberVoiceChannel.Value, playerFactory, options, retrieveOptions.OverridenSessionProvider, cancellationToken).ConfigureAwait(false);

return await CheckPreconditionsAsync(player, preconditions, cancellationToken).ConfigureAwait(false);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Lavalink4NET/Players/PlayerRetrieveOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public readonly record struct PlayerRetrieveOptions(
PlayerChannelBehavior ChannelBehavior = PlayerChannelBehavior.None,
MemberVoiceStateBehavior VoiceStateBehavior = MemberVoiceStateBehavior.Ignore,
ImmutableArray<IPlayerPrecondition> Preconditions = default);
ImmutableArray<IPlayerPrecondition> Preconditions = default,
ILavalinkSessionProvider? OverridenSessionProvider = null);
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voic
{
throw new NotImplementedException();
}

public ValueTask<TPlayer> JoinAsync<TPlayer, TOptions>(ulong guildId, ulong voiceChannelId, PlayerFactory<TPlayer, TOptions> playerFactory,
IOptions<TOptions> options, ILavalinkSessionProvider? overridenLavalinkSessionProvider,
CancellationToken cancellationToken = default) where TPlayer : ILavalinkPlayer where TOptions : LavalinkPlayerOptions
{
throw new NotImplementedException();
}

public bool TryGetPlayer(ulong guildId, [MaybeNullWhen(false)] out ILavalinkPlayer player)
{
Expand Down

0 comments on commit 03cc0a8

Please sign in to comment.