From cead0e303a693dab0beba79614532833424fbaca Mon Sep 17 00:00:00 2001 From: Angelo Breuer Date: Mon, 11 Dec 2023 13:21:00 +0100 Subject: [PATCH] feat(discordnet): Add ICommandContext overloads --- .../PlayerManagerExtensions.cs | 223 ++++++++++++++++++ 1 file changed, 223 insertions(+) diff --git a/src/Lavalink4NET.DiscordNet/PlayerManagerExtensions.cs b/src/Lavalink4NET.DiscordNet/PlayerManagerExtensions.cs index 8aefb946..090b93e9 100644 --- a/src/Lavalink4NET.DiscordNet/PlayerManagerExtensions.cs +++ b/src/Lavalink4NET.DiscordNet/PlayerManagerExtensions.cs @@ -5,6 +5,7 @@ namespace Lavalink4NET.DiscordNet; using System.Threading; using System.Threading.Tasks; using Discord; +using Discord.Commands; using Lavalink4NET.Extensions; using Lavalink4NET.Players; using Microsoft.Extensions.Options; @@ -237,6 +238,228 @@ public static ValueTask> RetrieveAsync( cancellationToken: cancellationToken); } + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + IOptions options, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + where TPlayer : class, ILavalinkPlayer + where TOptions : LavalinkPlayerOptions + { + var userVoiceState = commandContext.User as IVoiceState; + + return playerManager.RetrieveAsync( + guildId: commandContext.Guild.Id, + memberVoiceChannel: userVoiceState?.VoiceChannel?.Id, + playerFactory: playerFactory, + options: options, + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + Action? configure, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + where TPlayer : class, ILavalinkPlayer + where TOptions : LavalinkPlayerOptions, new() + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + ArgumentNullException.ThrowIfNull(configure); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: playerFactory, + options: CreateOptions(configure), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext interactionContext, + PlayerFactory playerFactory, + TOptions options, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + where TPlayer : class, ILavalinkPlayer + where TOptions : LavalinkPlayerOptions + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + ArgumentNullException.ThrowIfNull(options); + + return playerManager.RetrieveAsync( + commandContext: interactionContext, + playerFactory: playerFactory, + options: Options.Create(options), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + where TPlayer : class, ILavalinkPlayer + where TOptions : LavalinkPlayerOptions, new() + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: playerFactory, + options: Options.Create(new TOptions()), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + Action? configure, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + ArgumentNullException.ThrowIfNull(configure); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory, + options: CreateOptions(configure), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + LavalinkPlayerOptions options, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + ArgumentNullException.ThrowIfNull(options); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: playerFactory, + options: Options.Create(options), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerFactory playerFactory, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(playerFactory); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: playerFactory, + options: Options.Create(new LavalinkPlayerOptions()), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + IOptions options, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(options); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: PlayerFactory.Default, + options: options, + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + Action? configure, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: PlayerFactory.Default, + options: CreateOptions(configure), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + LavalinkPlayerOptions options, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + ArgumentNullException.ThrowIfNull(options); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: PlayerFactory.Default, + options: Options.Create(options), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + + public static ValueTask> RetrieveAsync( + this IPlayerManager playerManager, + ICommandContext commandContext, + PlayerRetrieveOptions retrieveOptions = default, + CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + ArgumentNullException.ThrowIfNull(playerManager); + + return playerManager.RetrieveAsync( + commandContext: commandContext, + playerFactory: PlayerFactory.Default, + options: Options.Create(new LavalinkPlayerOptions()), + retrieveOptions: retrieveOptions, + cancellationToken: cancellationToken); + } + public static ValueTask GetPlayerAsync(this IPlayerManager playerManager, IGuild guild, CancellationToken cancellationToken = default) where T : class, ILavalinkPlayer { cancellationToken.ThrowIfCancellationRequested();