Skip to content

Commit

Permalink
Add non-generic Add and Use extension methods (#88)
Browse files Browse the repository at this point in the history
* Add non-generic Add and Use extension methods

* Fix a wrong argument being logged

* Update docs, readmes, tests

* Cleanup

* Remove "the"
  • Loading branch information
KubaZ2 authored Jan 26, 2025
1 parent 69be6aa commit 4bb6547
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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<ApplicationCommandInteraction, HttpApplicationCommandContext>();
.AddHttpApplicationCommands();

var app = builder.Build();

Expand Down
5 changes: 2 additions & 3 deletions Documentation/guides/basic-concepts/http-interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationCommandInteraction, ApplicationCommandContext>();
.AddApplicationCommands();

var host = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Hosting;

using NetCord;
using NetCord.Hosting.Gateway;
using NetCord.Hosting.Services;
using NetCord.Hosting.Services.ApplicationCommands;
Expand All @@ -9,7 +8,7 @@
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext>(options =>
.AddApplicationCommands(options =>
{
options.LocalizationsProvider = new JsonLocalizationsProvider();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandContext>();
.AddCommands();

var host = builder.Build();

Expand Down
4 changes: 2 additions & 2 deletions Documentation/guides/services/text-commands/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,46 @@ public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext>(

// Use

public static IHostBuilder UseApplicationCommands(
this IHostBuilder builder)
{
return builder.UseApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>((_, _) => { });
}

public static IHostBuilder UseApplicationCommands(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>> configureOptions)
{
return builder.UseApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseApplicationCommands(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>, IServiceProvider> configureOptions)
{
return builder.UseApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>(configureOptions);
}

public static IHostBuilder UseHttpApplicationCommands(
this IHostBuilder builder)
{
return builder.UseApplicationCommands<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>((_, _) => { });
}

public static IHostBuilder UseHttpApplicationCommands(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>> configureOptions)
{
return builder.UseApplicationCommands<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseHttpApplicationCommands(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>, IServiceProvider> configureOptions)
{
return builder.UseApplicationCommands(configureOptions);
}

public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IHostBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ public static IServiceCollection ConfigureApplicationCommands(

// Add

public static IServiceCollection AddApplicationCommands(
this IServiceCollection services)
{
return services.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>((_, _) => { });
}

public static IServiceCollection AddApplicationCommands(
this IServiceCollection services,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>> configureOptions)
{
return services.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>((options, _) => configureOptions(options));
}

public static IServiceCollection AddApplicationCommands(
this IServiceCollection services,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>, IServiceProvider> configureOptions)
{
return services.AddApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>(configureOptions);
}

public static IServiceCollection AddHttpApplicationCommands(
this IServiceCollection services)
{
return services.AddApplicationCommands<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>((_, _) => { });
}

public static IServiceCollection AddHttpApplicationCommands(
this IServiceCollection services,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>> configureOptions)
{
return services.AddApplicationCommands<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>((options, _) => configureOptions(options));
}

public static IServiceCollection AddHttpApplicationCommands(
this IServiceCollection services,
Action<ApplicationCommandServiceOptions<ApplicationCommandInteraction, HttpApplicationCommandContext, HttpAutocompleteInteractionContext>, IServiceProvider> configureOptions)
{
return services.AddApplicationCommands(configureOptions);
}

public static IServiceCollection AddApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ public static IHostBuilder ConfigureCommands<TContext>(

// Use

public static IHostBuilder UseCommands(
this IHostBuilder builder)
{
return builder.UseCommands<CommandContext>((_, _) => { });
}

public static IHostBuilder UseCommands(
this IHostBuilder builder,
Action<CommandServiceOptions<CommandContext>> configureOptions)
{
return builder.UseCommands<CommandContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseCommands(
this IHostBuilder builder,
Action<CommandServiceOptions<CommandContext>, IServiceProvider> configureOptions)
{
return builder.UseCommands<CommandContext>(configureOptions);
}

public static IHostBuilder UseCommands<[DAM(DAMT.PublicConstructors)] TContext>(
this IHostBuilder builder)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public static IServiceCollection ConfigureCommands<TContext>(

// Add

public static IServiceCollection AddCommands(
this IServiceCollection services)
{
return services.AddCommands<CommandContext>((_, _) => { });
}

public static IServiceCollection AddCommands(
this IServiceCollection services,
Action<CommandServiceOptions<CommandContext>> configureOptions)
{
return services.AddCommands<CommandContext>((options, _) => configureOptions(options));
}

public static IServiceCollection AddCommands(
this IServiceCollection services,
Action<CommandServiceOptions<CommandContext>, IServiceProvider> configureOptions)
{
return services.AddCommands<CommandContext>(configureOptions);
}

public static IServiceCollection AddCommands<[DAM(DAMT.PublicConstructors)] TContext>(
this IServiceCollection services)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,46 @@ public static IHostBuilder ConfigureComponentInteractions(

// Use

public static IHostBuilder UseComponentInteractions(
this IHostBuilder builder)
{
return builder.UseComponentInteractions<ComponentInteraction, ComponentInteractionContext>((_, _) => { });
}

public static IHostBuilder UseComponentInteractions(
this IHostBuilder builder,
Action<ComponentInteractionServiceOptions<ComponentInteraction, ComponentInteractionContext>> configureOptions)
{
return builder.UseComponentInteractions<ComponentInteraction, ComponentInteractionContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseComponentInteractions(
this IHostBuilder builder,
Action<ComponentInteractionServiceOptions<ComponentInteraction, ComponentInteractionContext>, IServiceProvider> configureOptions)
{
return builder.UseComponentInteractions<ComponentInteraction, ComponentInteractionContext>(configureOptions);
}

public static IHostBuilder UseHttpComponentInteractions(
this IHostBuilder builder)
{
return builder.UseComponentInteractions<ComponentInteraction, HttpComponentInteractionContext>((_, _) => { });
}

public static IHostBuilder UseHttpComponentInteractions(
this IHostBuilder builder,
Action<ComponentInteractionServiceOptions<ComponentInteraction, HttpComponentInteractionContext>> configureOptions)
{
return builder.UseComponentInteractions<ComponentInteraction, HttpComponentInteractionContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseHttpComponentInteractions(
this IHostBuilder builder,
Action<ComponentInteractionServiceOptions<ComponentInteraction, HttpComponentInteractionContext>, IServiceProvider> configureOptions)
{
return builder.UseComponentInteractions(configureOptions);
}

public static IHostBuilder UseComponentInteractions<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IHostBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,46 @@ public static IServiceCollection ConfigureComponentInteractions(

// Add

public static IServiceCollection AddComponentInteractions(
this IServiceCollection services)
{
return services.AddComponentInteractions<ComponentInteraction, ComponentInteractionContext>((_, _) => { });
}

public static IServiceCollection AddComponentInteractions(
this IServiceCollection services,
Action<ComponentInteractionServiceOptions<ComponentInteraction, ComponentInteractionContext>> configureOptions)
{
return services.AddComponentInteractions<ComponentInteraction, ComponentInteractionContext>((options, _) => configureOptions(options));
}

public static IServiceCollection AddComponentInteractions(
this IServiceCollection services,
Action<ComponentInteractionServiceOptions<ComponentInteraction, ComponentInteractionContext>, IServiceProvider> configureOptions)
{
return services.AddComponentInteractions<ComponentInteraction, ComponentInteractionContext>(configureOptions);
}

public static IServiceCollection AddHttpComponentInteractions(
this IServiceCollection services)
{
return services.AddComponentInteractions<ComponentInteraction, HttpComponentInteractionContext>((_, _) => { });
}

public static IServiceCollection AddHttpComponentInteractions(
this IServiceCollection services,
Action<ComponentInteractionServiceOptions<ComponentInteraction, HttpComponentInteractionContext>> configureOptions)
{
return services.AddComponentInteractions<ComponentInteraction, HttpComponentInteractionContext>((options, _) => configureOptions(options));
}

public static IServiceCollection AddHttpComponentInteractions(
this IServiceCollection services,
Action<ComponentInteractionServiceOptions<ComponentInteraction, HttpComponentInteractionContext>, IServiceProvider> configureOptions)
{
return services.AddComponentInteractions(configureOptions);
}

public static IServiceCollection AddComponentInteractions<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IServiceCollection services)
Expand Down
Loading

0 comments on commit 4bb6547

Please sign in to comment.