From 8c67bf8c62cef6757c5fea12341666b1e0a3a0c8 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Mon, 2 Sep 2024 17:11:17 +0300 Subject: [PATCH 1/2] Allow overriding ILavalinkSessionProvider while resolving/joining player to allow creating player on the certain node --- src/Lavalink4NET/Players/IPlayerManager.cs | 4 ++++ src/Lavalink4NET/Players/PlayerManager.cs | 19 ++++++++++++++++--- .../Players/PlayerRetrieveOptions.cs | 3 ++- .../PlayerManagerMock.cs | 7 +++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Lavalink4NET/Players/IPlayerManager.cs b/src/Lavalink4NET/Players/IPlayerManager.cs index 870a6cd0..39fc042f 100644 --- a/src/Lavalink4NET/Players/IPlayerManager.cs +++ b/src/Lavalink4NET/Players/IPlayerManager.cs @@ -46,4 +46,8 @@ ValueTask> RetrieveAsync( CancellationToken cancellationToken = default) where TPlayer : class, ILavalinkPlayer where TOptions : LavalinkPlayerOptions; + + ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, IOptions options, ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default) + where TPlayer : ILavalinkPlayer + where TOptions : LavalinkPlayerOptions; } \ No newline at end of file diff --git a/src/Lavalink4NET/Players/PlayerManager.cs b/src/Lavalink4NET/Players/PlayerManager.cs index 7f7df6aa..c10d5d3e 100644 --- a/src/Lavalink4NET/Players/PlayerManager.cs +++ b/src/Lavalink4NET/Players/PlayerManager.cs @@ -116,8 +116,15 @@ public bool HasPlayer(ulong guildId) return _handles.TryGetValue(guildId, out var handle) && handle is { Player.State: not PlayerState.Destroyed }; } + + public ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, IOptions options, CancellationToken cancellationToken = default) + where TPlayer : ILavalinkPlayer + where TOptions : LavalinkPlayerOptions + { + return JoinAsync(guildId, voiceChannelId, playerFactory, options, null, cancellationToken); + } - public async ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, IOptions options, CancellationToken cancellationToken = default) + public async ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, IOptions options, ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default) where TPlayer : ILavalinkPlayer where TOptions : LavalinkPlayerOptions { @@ -125,9 +132,15 @@ public async ValueTask JoinAsync(ulong guildId, ulon LavalinkPlayerHandle Create(ulong guildId) { + var playerContext = _playerContext; + if (overridenLavalinkSessionProvider is not null) + { + playerContext = playerContext with { SessionProvider = overridenLavalinkSessionProvider }; + } + return new LavalinkPlayerHandle( guildId: guildId, - playerContext: _playerContext, + playerContext: playerContext, playerFactory: playerFactory, options: options, logger: _loggerFactory.CreateLogger()); @@ -296,7 +309,7 @@ await DiscordClient return PlayerResult.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); } diff --git a/src/Lavalink4NET/Players/PlayerRetrieveOptions.cs b/src/Lavalink4NET/Players/PlayerRetrieveOptions.cs index 3cb754a9..47ad0c78 100644 --- a/src/Lavalink4NET/Players/PlayerRetrieveOptions.cs +++ b/src/Lavalink4NET/Players/PlayerRetrieveOptions.cs @@ -7,4 +7,5 @@ public readonly record struct PlayerRetrieveOptions( PlayerChannelBehavior ChannelBehavior = PlayerChannelBehavior.None, MemberVoiceStateBehavior VoiceStateBehavior = MemberVoiceStateBehavior.Ignore, - ImmutableArray Preconditions = default); \ No newline at end of file + ImmutableArray Preconditions = default, + ILavalinkSessionProvider? OverridenSessionProvider = null); \ No newline at end of file diff --git a/tests/Lavalink4NET.InactivityTracking.Tests/PlayerManagerMock.cs b/tests/Lavalink4NET.InactivityTracking.Tests/PlayerManagerMock.cs index 700cfd33..c3b9db6c 100644 --- a/tests/Lavalink4NET.InactivityTracking.Tests/PlayerManagerMock.cs +++ b/tests/Lavalink4NET.InactivityTracking.Tests/PlayerManagerMock.cs @@ -54,6 +54,13 @@ public ValueTask JoinAsync(ulong guildId, ulong voic { throw new NotImplementedException(); } + + public ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, + IOptions 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) { From 604d09b6b8f759c8131687ed1ad48f6db2813e14 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Mon, 2 Sep 2024 17:14:46 +0300 Subject: [PATCH 2/2] Add default implementation for new overload of JoinAsync, to not introduce breaking changes --- src/Lavalink4NET/Players/IPlayerManager.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Lavalink4NET/Players/IPlayerManager.cs b/src/Lavalink4NET/Players/IPlayerManager.cs index 39fc042f..f9582844 100644 --- a/src/Lavalink4NET/Players/IPlayerManager.cs +++ b/src/Lavalink4NET/Players/IPlayerManager.cs @@ -37,6 +37,15 @@ ValueTask JoinAsync(ulong guildId, ulong voiceChanne where TPlayer : ILavalinkPlayer where TOptions : LavalinkPlayerOptions; + ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, + PlayerFactory playerFactory, IOptions options, + ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default) + where TPlayer : ILavalinkPlayer + where TOptions : LavalinkPlayerOptions + { + return JoinAsync(guildId, voiceChannelId, playerFactory, options, cancellationToken); + } + ValueTask> RetrieveAsync( ulong guildId, ulong? memberVoiceChannel, @@ -46,8 +55,4 @@ ValueTask> RetrieveAsync( CancellationToken cancellationToken = default) where TPlayer : class, ILavalinkPlayer where TOptions : LavalinkPlayerOptions; - - ValueTask JoinAsync(ulong guildId, ulong voiceChannelId, PlayerFactory playerFactory, IOptions options, ILavalinkSessionProvider? overridenLavalinkSessionProvider, CancellationToken cancellationToken = default) - where TPlayer : ILavalinkPlayer - where TOptions : LavalinkPlayerOptions; } \ No newline at end of file