From 4bb6547484734705391f250677789fed39eee9c8 Mon Sep 17 00:00:00 2001 From: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com> Date: Sun, 26 Jan 2025 20:07:50 +0100 Subject: [PATCH] Add non-generic Add and Use extension methods (#88) * Add non-generic Add and Use extension methods * Fix a wrong argument being logged * Update docs, readmes, tests * Cleanup * Remove "the" --- .../HttpInteractions/Program.cs | 6 +-- .../basic-concepts/http-interactions.md | 5 +-- .../IntroductionHosting/Program.cs | 3 +- .../LocalizationsHosting/Program.cs | 3 +- .../application-commands/introduction.md | 4 +- .../application-commands/localizations.md | 2 +- .../IntroductionHosting/Program.cs | 3 +- .../services/text-commands/introduction.md | 4 +- ...tionCommandServiceHostBuilderExtensions.cs | 40 +++++++++++++++++++ ...mmandServiceServiceCollectionExtensions.cs | 40 +++++++++++++++++++ .../CommandServiceHostBuilderExtensions.cs | 20 ++++++++++ ...mmandServiceServiceCollectionExtensions.cs | 20 ++++++++++ .../ComponentInteractionResultHandler.cs | 4 +- ...InteractionServiceHostBuilderExtensions.cs | 40 +++++++++++++++++++ ...ctionServiceServiceCollectionExtensions.cs | 40 +++++++++++++++++++ .../AutocompleteInteractionContexts.cs | 12 ++++++ README.md | 2 +- Resources/NuGet/README.md | 2 +- .../EchoAutocompleteProvider.cs | 16 ++++++++ .../Program.cs | 6 +-- Tests/NetCord.Test.Hosting/Program.cs | 4 +- Tests/NetCord.Test.Sharded.Hosting/Program.cs | 7 ++-- 22 files changed, 252 insertions(+), 31 deletions(-) create mode 100644 Tests/NetCord.Test.Hosting.AspNetCore/EchoAutocompleteProvider.cs diff --git a/Documentation/guides/basic-concepts/HttpInteractions/Program.cs b/Documentation/guides/basic-concepts/HttpInteractions/Program.cs index 2c2a80cc..7415a3d8 100644 --- a/Documentation/guides/basic-concepts/HttpInteractions/Program.cs +++ b/Documentation/guides/basic-concepts/HttpInteractions/Program.cs @@ -1,14 +1,12 @@ -using NetCord; -using NetCord.Hosting.AspNetCore; +using NetCord.Hosting.AspNetCore; using NetCord.Hosting.Rest; using NetCord.Hosting.Services.ApplicationCommands; -using NetCord.Services.ApplicationCommands; var builder = WebApplication.CreateBuilder(args); builder.Services .AddDiscordRest() - .AddApplicationCommands(); + .AddHttpApplicationCommands(); var app = builder.Build(); diff --git a/Documentation/guides/basic-concepts/http-interactions.md b/Documentation/guides/basic-concepts/http-interactions.md index ad37a0f3..f20752f6 100644 --- a/Documentation/guides/basic-concepts/http-interactions.md +++ b/Documentation/guides/basic-concepts/http-interactions.md @@ -14,9 +14,8 @@ Before you get started, ensure that you've installed the necessary native depend ## Setting Up HTTP Interactions in C# -To handle HTTP interactions from Discord in your bot, you need to use @NetCord.Hosting.Rest.RestClientServiceCollectionExtensions.AddDiscordRest* to add the @NetCord.Rest.RestClient and then call @NetCord.Hosting.AspNetCore.EndpointRouteBuilderExtensions.UseHttpInteractions* to map the HTTP interactions route. - -[!code-cs[Program.cs](HttpInteractions/Program.cs?highlight=10,18)] +To handle HTTP interactions from Discord in your bot, you need to use @NetCord.Hosting.Rest.RestClientServiceCollectionExtensions.AddDiscordRest* to add the @NetCord.Rest.RestClient and then call @NetCord.Hosting.AspNetCore.EndpointRouteBuilderExtensions.UseHttpInteractions* to map the HTTP interactions route. You can also use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddHttpApplicationCommands* to add the application command service with preconfigured HTTP contexts to your host builder. +[!code-cs[Program.cs](HttpInteractions/Program.cs?highlight=8,16)] ### Receiving HTTP Interactions via HTTP Interaction Handler diff --git a/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs b/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs index 6f9fd621..25a7e186 100644 --- a/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs +++ b/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs @@ -5,13 +5,12 @@ using NetCord.Hosting.Services; using NetCord.Hosting.Services.ApplicationCommands; using NetCord.Rest; -using NetCord.Services.ApplicationCommands; var builder = Host.CreateApplicationBuilder(args); builder.Services .AddDiscordGateway() - .AddApplicationCommands(); + .AddApplicationCommands(); var host = builder.Build(); diff --git a/Documentation/guides/services/application-commands/LocalizationsHosting/Program.cs b/Documentation/guides/services/application-commands/LocalizationsHosting/Program.cs index 555ebc13..72a5672b 100644 --- a/Documentation/guides/services/application-commands/LocalizationsHosting/Program.cs +++ b/Documentation/guides/services/application-commands/LocalizationsHosting/Program.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Hosting; -using NetCord; using NetCord.Hosting.Gateway; using NetCord.Hosting.Services; using NetCord.Hosting.Services.ApplicationCommands; @@ -9,7 +8,7 @@ var builder = Host.CreateApplicationBuilder(args); builder.Services - .AddApplicationCommands(options => + .AddApplicationCommands(options => { options.LocalizationsProvider = new JsonLocalizationsProvider(); }) diff --git a/Documentation/guides/services/application-commands/introduction.md b/Documentation/guides/services/application-commands/introduction.md index f29ed5fb..3ca2af66 100644 --- a/Documentation/guides/services/application-commands/introduction.md +++ b/Documentation/guides/services/application-commands/introduction.md @@ -6,8 +6,8 @@ uid: application-commands ## [.NET Generic Host](#tab/generic-host) -Adding application commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddApplicationCommands``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add application command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. -[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=14,19-21,24,27)] +Adding application commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddApplicationCommands(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add application command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. +[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13,18-20,23,26)] ## [Bare Bones](#tab/bare-bones) diff --git a/Documentation/guides/services/application-commands/localizations.md b/Documentation/guides/services/application-commands/localizations.md index 2c9645e8..20c2d0b2 100644 --- a/Documentation/guides/services/application-commands/localizations.md +++ b/Documentation/guides/services/application-commands/localizations.md @@ -7,7 +7,7 @@ To localize application commands, you need to use @NetCord.Services.ApplicationC The samples below show how to specify the @NetCord.Services.ApplicationCommands.JsonLocalizationsProvider. ## [.NET Generic Host](#tab/generic-host) -[!code-cs[Program.cs](LocalizationsHosting/Program.cs?highlight=4#L11-L15)] +[!code-cs[Program.cs](LocalizationsHosting/Program.cs?highlight=4#L10-L14)] ## [Bare Bones](#tab/bare-bones) [!code-cs[Program.cs](Localizations/Program.cs?highlight=3#L12-L15)] diff --git a/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs b/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs index 9247123d..2af5f85a 100644 --- a/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs +++ b/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs @@ -3,13 +3,12 @@ using NetCord.Hosting.Gateway; using NetCord.Hosting.Services; using NetCord.Hosting.Services.Commands; -using NetCord.Services.Commands; var builder = Host.CreateApplicationBuilder(args); builder.Services .AddDiscordGateway() - .AddCommands(); + .AddCommands(); var host = builder.Build(); diff --git a/Documentation/guides/services/text-commands/introduction.md b/Documentation/guides/services/text-commands/introduction.md index 813abbad..0ca05430 100644 --- a/Documentation/guides/services/text-commands/introduction.md +++ b/Documentation/guides/services/text-commands/introduction.md @@ -2,8 +2,8 @@ ## [.NET Generic Host](#tab/generic-host) -Adding commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceServiceCollectionExtensions.AddCommands``1(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. -[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=12,17,20,23)] +Adding commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceServiceCollectionExtensions.AddCommands(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. +[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=11,16,19,22)] ### Specifying a prefix diff --git a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceHostBuilderExtensions.cs b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceHostBuilderExtensions.cs index c11ffcad..f82bbb72 100644 --- a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceHostBuilderExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceHostBuilderExtensions.cs @@ -71,6 +71,46 @@ public static IHostBuilder ConfigureApplicationCommands( // Use + public static IHostBuilder UseApplicationCommands( + this IHostBuilder builder) + { + return builder.UseApplicationCommands((_, _) => { }); + } + + public static IHostBuilder UseApplicationCommands( + this IHostBuilder builder, + Action> configureOptions) + { + return builder.UseApplicationCommands((options, _) => configureOptions(options)); + } + + public static IHostBuilder UseApplicationCommands( + this IHostBuilder builder, + Action, IServiceProvider> configureOptions) + { + return builder.UseApplicationCommands(configureOptions); + } + + public static IHostBuilder UseHttpApplicationCommands( + this IHostBuilder builder) + { + return builder.UseApplicationCommands((_, _) => { }); + } + + public static IHostBuilder UseHttpApplicationCommands( + this IHostBuilder builder, + Action> configureOptions) + { + return builder.UseApplicationCommands((options, _) => configureOptions(options)); + } + + public static IHostBuilder UseHttpApplicationCommands( + this IHostBuilder builder, + Action, IServiceProvider> configureOptions) + { + return builder.UseApplicationCommands(configureOptions); + } + public static IHostBuilder UseApplicationCommands( this IHostBuilder builder) diff --git a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs index ef111b7b..14dcef34 100644 --- a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs @@ -87,6 +87,46 @@ public static IServiceCollection ConfigureApplicationCommands( // Add + public static IServiceCollection AddApplicationCommands( + this IServiceCollection services) + { + return services.AddApplicationCommands((_, _) => { }); + } + + public static IServiceCollection AddApplicationCommands( + this IServiceCollection services, + Action> configureOptions) + { + return services.AddApplicationCommands((options, _) => configureOptions(options)); + } + + public static IServiceCollection AddApplicationCommands( + this IServiceCollection services, + Action, IServiceProvider> configureOptions) + { + return services.AddApplicationCommands(configureOptions); + } + + public static IServiceCollection AddHttpApplicationCommands( + this IServiceCollection services) + { + return services.AddApplicationCommands((_, _) => { }); + } + + public static IServiceCollection AddHttpApplicationCommands( + this IServiceCollection services, + Action> configureOptions) + { + return services.AddApplicationCommands((options, _) => configureOptions(options)); + } + + public static IServiceCollection AddHttpApplicationCommands( + this IServiceCollection services, + Action, IServiceProvider> configureOptions) + { + return services.AddApplicationCommands(configureOptions); + } + public static IServiceCollection AddApplicationCommands( this IServiceCollection services) diff --git a/Hosting/NetCord.Hosting.Services/Commands/CommandServiceHostBuilderExtensions.cs b/Hosting/NetCord.Hosting.Services/Commands/CommandServiceHostBuilderExtensions.cs index e4c4440a..517759ab 100644 --- a/Hosting/NetCord.Hosting.Services/Commands/CommandServiceHostBuilderExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/Commands/CommandServiceHostBuilderExtensions.cs @@ -42,6 +42,26 @@ public static IHostBuilder ConfigureCommands( // Use + public static IHostBuilder UseCommands( + this IHostBuilder builder) + { + return builder.UseCommands((_, _) => { }); + } + + public static IHostBuilder UseCommands( + this IHostBuilder builder, + Action> configureOptions) + { + return builder.UseCommands((options, _) => configureOptions(options)); + } + + public static IHostBuilder UseCommands( + this IHostBuilder builder, + Action, IServiceProvider> configureOptions) + { + return builder.UseCommands(configureOptions); + } + public static IHostBuilder UseCommands<[DAM(DAMT.PublicConstructors)] TContext>( this IHostBuilder builder) diff --git a/Hosting/NetCord.Hosting.Services/Commands/CommandServiceServiceCollectionExtensions.cs b/Hosting/NetCord.Hosting.Services/Commands/CommandServiceServiceCollectionExtensions.cs index e4c02fe4..c395c40a 100644 --- a/Hosting/NetCord.Hosting.Services/Commands/CommandServiceServiceCollectionExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/Commands/CommandServiceServiceCollectionExtensions.cs @@ -53,6 +53,26 @@ public static IServiceCollection ConfigureCommands( // Add + public static IServiceCollection AddCommands( + this IServiceCollection services) + { + return services.AddCommands((_, _) => { }); + } + + public static IServiceCollection AddCommands( + this IServiceCollection services, + Action> configureOptions) + { + return services.AddCommands((options, _) => configureOptions(options)); + } + + public static IServiceCollection AddCommands( + this IServiceCollection services, + Action, IServiceProvider> configureOptions) + { + return services.AddCommands(configureOptions); + } + public static IServiceCollection AddCommands<[DAM(DAMT.PublicConstructors)] TContext>( this IServiceCollection services) diff --git a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionResultHandler.cs b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionResultHandler.cs index b3d32b5d..ec355873 100644 --- a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionResultHandler.cs +++ b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionResultHandler.cs @@ -20,9 +20,9 @@ public ValueTask HandleResultAsync(IExecutionResult result, TContext context, Ga var interaction = context.Interaction; if (failResult is IExceptionResult exceptionResult) - logger.LogError(exceptionResult.Exception, "Execution of an interaction of custom ID '{Id}' failed with an exception", interaction.Id); + logger.LogError(exceptionResult.Exception, "Execution of an interaction of custom ID '{Id}' failed with an exception", interaction.Data.CustomId); else - logger.LogDebug("Execution of an interaction of custom ID '{Id}' failed with '{Message}'", interaction.Id, resultMessage); + logger.LogDebug("Execution of an interaction of custom ID '{Id}' failed with '{Message}'", interaction.Data.CustomId, resultMessage); InteractionMessageProperties message = new() { diff --git a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceHostBuilderExtensions.cs b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceHostBuilderExtensions.cs index 464b6208..fda9e3a5 100644 --- a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceHostBuilderExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceHostBuilderExtensions.cs @@ -46,6 +46,46 @@ public static IHostBuilder ConfigureComponentInteractions( // Use + public static IHostBuilder UseComponentInteractions( + this IHostBuilder builder) + { + return builder.UseComponentInteractions((_, _) => { }); + } + + public static IHostBuilder UseComponentInteractions( + this IHostBuilder builder, + Action> configureOptions) + { + return builder.UseComponentInteractions((options, _) => configureOptions(options)); + } + + public static IHostBuilder UseComponentInteractions( + this IHostBuilder builder, + Action, IServiceProvider> configureOptions) + { + return builder.UseComponentInteractions(configureOptions); + } + + public static IHostBuilder UseHttpComponentInteractions( + this IHostBuilder builder) + { + return builder.UseComponentInteractions((_, _) => { }); + } + + public static IHostBuilder UseHttpComponentInteractions( + this IHostBuilder builder, + Action> configureOptions) + { + return builder.UseComponentInteractions((options, _) => configureOptions(options)); + } + + public static IHostBuilder UseHttpComponentInteractions( + this IHostBuilder builder, + Action, IServiceProvider> configureOptions) + { + return builder.UseComponentInteractions(configureOptions); + } + public static IHostBuilder UseComponentInteractions( this IHostBuilder builder) diff --git a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceServiceCollectionExtensions.cs b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceServiceCollectionExtensions.cs index d097f576..c451f95a 100644 --- a/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceServiceCollectionExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/ComponentInteractions/ComponentInteractionServiceServiceCollectionExtensions.cs @@ -57,6 +57,46 @@ public static IServiceCollection ConfigureComponentInteractions( // Add + public static IServiceCollection AddComponentInteractions( + this IServiceCollection services) + { + return services.AddComponentInteractions((_, _) => { }); + } + + public static IServiceCollection AddComponentInteractions( + this IServiceCollection services, + Action> configureOptions) + { + return services.AddComponentInteractions((options, _) => configureOptions(options)); + } + + public static IServiceCollection AddComponentInteractions( + this IServiceCollection services, + Action, IServiceProvider> configureOptions) + { + return services.AddComponentInteractions(configureOptions); + } + + public static IServiceCollection AddHttpComponentInteractions( + this IServiceCollection services) + { + return services.AddComponentInteractions((_, _) => { }); + } + + public static IServiceCollection AddHttpComponentInteractions( + this IServiceCollection services, + Action> configureOptions) + { + return services.AddComponentInteractions((options, _) => configureOptions(options)); + } + + public static IServiceCollection AddHttpComponentInteractions( + this IServiceCollection services, + Action, IServiceProvider> configureOptions) + { + return services.AddComponentInteractions(configureOptions); + } + public static IServiceCollection AddComponentInteractions( this IServiceCollection services) diff --git a/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs b/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs index 577d368d..6e9f6ab3 100644 --- a/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs +++ b/NetCord.Services/ApplicationCommands/AutocompleteInteractionContexts.cs @@ -1,4 +1,5 @@ using NetCord.Gateway; +using NetCord.Rest; namespace NetCord.Services.ApplicationCommands; @@ -21,3 +22,14 @@ public class AutocompleteInteractionContext(AutocompleteInteraction interaction, ulong? IGuildContext.GuildId => Interaction.GuildId; } + +public class HttpAutocompleteInteractionContext(AutocompleteInteraction interaction, RestClient client) + : BaseAutocompleteInteractionContext(interaction), + IRestClientContext, + IChannelContext, + IUserContext +{ + public RestClient Client => client; + public TextChannel Channel => Interaction.Channel; + public User User => Interaction.User; +} diff --git a/README.md b/README.md index 545137ec..9787f89b 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ The following example sets up a bot with a minimal API-style approach for the `/ ```cs var builder = Host.CreateDefaultBuilder(args) .UseDiscordGateway() - .UseApplicationCommands(); + .UseApplicationCommands(); var host = builder.Build() .AddSlashCommand("square", "Square!", (int a) => $"{a}² = {a * a}") diff --git a/Resources/NuGet/README.md b/Resources/NuGet/README.md index 206a2221..aa056315 100644 --- a/Resources/NuGet/README.md +++ b/Resources/NuGet/README.md @@ -36,7 +36,7 @@ The following example sets up a bot with a minimal API-style approach for the `/ ```cs var builder = Host.CreateDefaultBuilder(args) .UseDiscordGateway() - .UseApplicationCommands(); + .UseApplicationCommands(); var host = builder.Build() .AddSlashCommand("square", "Square!", (int a) => $"{a}² = {a * a}") diff --git a/Tests/NetCord.Test.Hosting.AspNetCore/EchoAutocompleteProvider.cs b/Tests/NetCord.Test.Hosting.AspNetCore/EchoAutocompleteProvider.cs new file mode 100644 index 00000000..8cf6ed51 --- /dev/null +++ b/Tests/NetCord.Test.Hosting.AspNetCore/EchoAutocompleteProvider.cs @@ -0,0 +1,16 @@ +using NetCord.Rest; +using NetCord.Services.ApplicationCommands; + +namespace NetCord.Test.Hosting.AspNetCore; + +public class EchoAutocompleteProvider : IAutocompleteProvider +{ + public ValueTask?> GetChoicesAsync(ApplicationCommandInteractionDataOption option, HttpAutocompleteInteractionContext context) + { + return new ValueTask?>( + [ + new ApplicationCommandOptionChoiceProperties("Hello", "Hello!"), + new ApplicationCommandOptionChoiceProperties("World", "World!"), + ]); + } +} diff --git a/Tests/NetCord.Test.Hosting.AspNetCore/Program.cs b/Tests/NetCord.Test.Hosting.AspNetCore/Program.cs index a051c2db..ef044fb3 100644 --- a/Tests/NetCord.Test.Hosting.AspNetCore/Program.cs +++ b/Tests/NetCord.Test.Hosting.AspNetCore/Program.cs @@ -6,15 +6,14 @@ using NetCord.Hosting.Services.ComponentInteractions; using NetCord.Rest; using NetCord.Services.ApplicationCommands; -using NetCord.Services.ComponentInteractions; using NetCord.Test.Hosting.AspNetCore; var builder = WebApplication.CreateBuilder(args); builder.Services .AddDiscordRest() - .AddApplicationCommands() - .AddComponentInteractions() + .AddHttpApplicationCommands() + .AddHttpComponentInteractions() .AddHttpInteractionHandler() .AddHttpInteractionHandler((Interaction interaction, ILogger logger) => logger.LogInformation("Id: {}", interaction.Id)); @@ -23,6 +22,7 @@ app .AddSlashCommand("ping", "Ping!", (IServiceProvider provider, HttpApplicationCommandContext context) => "Pong!") .AddSlashCommand("button", "Button!", (string s) => new InteractionMessageProperties().AddComponents(new ActionRowProperties([new ButtonProperties($"button:{s}", "Button!", ButtonStyle.Primary)]))) + .AddSlashCommand("echo", "Echo!", ([SlashCommandParameter(AutocompleteProviderType = typeof(EchoAutocompleteProvider))] string s) => s) .AddComponentInteraction("button", (string s) => $"Button! {s}"); app.UseHttpInteractions("/"); diff --git a/Tests/NetCord.Test.Hosting/Program.cs b/Tests/NetCord.Test.Hosting/Program.cs index f1cf5d15..79d958ef 100644 --- a/Tests/NetCord.Test.Hosting/Program.cs +++ b/Tests/NetCord.Test.Hosting/Program.cs @@ -41,13 +41,13 @@ .ConfigureApplicationCommands(o => o.DefaultParameterDescriptionFormat = "AA") .ConfigureApplicationCommands(o => o.DefaultParameterDescriptionFormat = "XD") .AddDiscordGateway(o => o.Intents = GatewayIntents.All) - .AddApplicationCommands(options => + .AddApplicationCommands(options => { options.ResultHandler = new CustomApplicationCommandResultHandler(); }) .AddComponentInteractions() .AddComponentInteractions() - .AddCommands() + .AddCommands() .AddGatewayEventHandler(nameof(GatewayClient.MessageCreate), (Message message, ILogger logger) => logger.LogInformation("Content: {}", message.Content)) .AddGatewayEventHandler() .AddGatewayEventHandler() diff --git a/Tests/NetCord.Test.Sharded.Hosting/Program.cs b/Tests/NetCord.Test.Sharded.Hosting/Program.cs index 933af1c0..f1eebceb 100644 --- a/Tests/NetCord.Test.Sharded.Hosting/Program.cs +++ b/Tests/NetCord.Test.Sharded.Hosting/Program.cs @@ -7,20 +7,19 @@ using NetCord.Hosting.Services.ApplicationCommands; using NetCord.Hosting.Services.Commands; using NetCord.Services.ApplicationCommands; -using NetCord.Services.Commands; var builder = Host.CreateApplicationBuilder(args); builder.Services - .AddApplicationCommands() - .AddCommands() + .AddApplicationCommands() + .AddCommands() .ConfigureDiscordShardedGateway(o => o.Presence = new(UserStatusType.Idle)) .AddDiscordShardedGateway() .AddShardedGatewayEventHandler(nameof(GatewayClient.MessageCreate), (Message message, GatewayClient client, ILogger logger) => logger.LogInformation(new EventId(client.Shard.GetValueOrDefault().Id), "Content: {}", message.Content)); var host = builder.Build(); -host.AddSlashCommand("ping", "Ping!", (SlashCommandContext context) => "Pong!") +host.AddSlashCommand("ping", "Ping!", (ApplicationCommandContext context) => "Pong!") .AddCommand(["ping"], () => "Pong!") .UseShardedGatewayEventHandlers();