Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Documentation/guides/advanced/sharding.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Sharding is splitting your bot into multiple @"NetCord.Gateway.GatewayClient"s.
To start sharding with the generic host, instead of calling @NetCord.Hosting.Gateway.GatewayClientServiceCollectionExtensions.AddDiscordGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection), you need to call @NetCord.Hosting.Gateway.ShardedGatewayClientServiceCollectionExtensions.AddDiscordShardedGateway(Microsoft.Extensions.DependencyInjection.IServiceCollection). Example:
[!code-cs[Program.cs](ShardingHosting/Program.cs)]

Also note that you need to use @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead of @NetCord.Hosting.Gateway.IGatewayEventHandler or @NetCord.Hosting.Gateway.IGatewayEventHandler`1 for event handlers. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers.
Also note that you need to use @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead of @NetCord.Hosting.Gateway.IGatewayEventHandler or @NetCord.Hosting.Gateway.IGatewayEventHandler`1 for event handlers. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers and @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseShardedGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind them.

## [Bare Bones](#tab/bare-bones)

Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/events/first-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ When you run this code, when someone reacts to a message, the bot will notify ev
Other events work similar to these. You can play with them if you want!

> [!NOTE]
> When using @NetCord.Gateway.ShardedGatewayClient, you need to implement @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers instead.
> When using @NetCord.Gateway.ShardedGatewayClient, you need to implement @NetCord.Hosting.Gateway.IShardedGatewayEventHandler or @NetCord.Hosting.Gateway.IShardedGatewayEventHandler`1 instead. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerServiceCollectionExtensions.AddShardedGatewayEventHandlers(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Reflection.Assembly) to add event handlers and @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseShardedGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind them instead.

## [Bare Bones](#tab/bare-bones)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public static class EndpointRouteBuilderExtensions
{
public static IEndpointConventionBuilder UseHttpInteractions(this IEndpointRouteBuilder endpoints, string pattern)
{
var publicKey = endpoints.ServiceProvider.GetRequiredService<IOptions<IDiscordOptions>>().Value.PublicKey ?? throw new InvalidOperationException($"'{nameof(IDiscordOptions.PublicKey)}' must be set.");
var services = endpoints.ServiceProvider;

var publicKey = services.GetRequiredService<IOptions<IDiscordOptions>>().Value.PublicKey ?? throw new InvalidOperationException($"'{nameof(IDiscordOptions.PublicKey)}' must be set.");
HttpInteractionValidator validator = new(publicKey);

var handlers = endpoints.ServiceProvider.GetServices<IHttpInteractionHandler>().ToArray();
var handlers = services.GetServices<IHttpInteractionHandler>().ToArray();
var tasks = new ValueTask[handlers.Length];

var client = endpoints.ServiceProvider.GetRequiredService<RestClient>();
var client = services.GetRequiredService<RestClient>();

var routePattern = RoutePatternHelper.ParseLiteral(pattern);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<IsAotCompatible>false</IsAotCompatible>
<IsAotCompatible>true</IsAotCompatible>

<RepositoryUrl>https://github.com/NetCordDev/NetCord</RepositoryUrl>
<PackageProjectUrl>https://netcord.dev</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
namespace NetCord.Hosting.Services.ApplicationCommands;

[GatewayEvent(nameof(GatewayClient.InteractionCreate))]
internal unsafe partial class ApplicationCommandInteractionHandler<TInteraction, TContext> : IGatewayEventHandler<Interaction>, IShardedGatewayEventHandler<Interaction>, IHttpInteractionHandler where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext
internal unsafe partial class ApplicationCommandInteractionHandler<TInteraction, [DAM(DAMT.PublicConstructors)] TContext>
: IGatewayEventHandler<Interaction>,
IShardedGatewayEventHandler<Interaction>,
IHttpInteractionHandler
where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext
{
private IServiceProvider Services { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace NetCord.Hosting.Services.ApplicationCommands;

public class ApplicationCommandResultHandler<TContext>(MessageFlags? messageFlags = null) : IApplicationCommandResultHandler<TContext> where TContext : IApplicationCommandContext
public class ApplicationCommandResultHandler<TContext>(MessageFlags? messageFlags = null)
: IApplicationCommandResultHandler<TContext>
where TContext : IApplicationCommandContext
{
public ValueTask HandleResultAsync(IExecutionResult result, TContext context, GatewayClient? client, ILogger logger, IServiceProvider services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,60 @@ public static class ApplicationCommandServiceHostBuilderExtensions
{
// Configure

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

public static IHostBuilder ConfigureApplicationCommands(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions, IServiceProvider> configureOptions)
public static IHostBuilder ConfigureApplicationCommands(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions, IServiceProvider> configureOptions)
{
return builder.ConfigureServices((context, services) => services.ConfigureApplicationCommands(configureOptions));
}

public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>> configureOptions)
public static IHostBuilder ConfigureApplicationCommands<TInteraction,
TContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
return builder.ConfigureApplicationCommands<TInteraction, TContext>((options, _) => configureOptions(options));
}

public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>, IServiceProvider> configureOptions)
public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>, IServiceProvider> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
return builder.ConfigureServices((context, services) => services.ConfigureApplicationCommands(configureOptions));
}

public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext, TAutocompleteContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>> configureOptions)
public static IHostBuilder ConfigureApplicationCommands<TInteraction,
TContext,
TAutocompleteContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
where TAutocompleteContext : IAutocompleteInteractionContext
{
return builder.ConfigureApplicationCommands<TInteraction, TContext, TAutocompleteContext>((options, _) => configureOptions(options));
}

public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext, TAutocompleteContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>, IServiceProvider> configureOptions)
public static IHostBuilder ConfigureApplicationCommands<TInteraction,
TContext,
TAutocompleteContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>, IServiceProvider> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
where TAutocompleteContext : IAutocompleteInteractionContext
Expand All @@ -56,47 +71,68 @@ public static IHostBuilder ConfigureApplicationCommands<TInteraction, TContext,

// Use

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

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
return builder.UseApplicationCommands<TInteraction, TContext>((_, _) => { });
}

public static IHostBuilder UseApplicationCommands<TInteraction, TContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>> configureOptions)
public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
return builder.UseApplicationCommands<TInteraction, TContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseApplicationCommands<TInteraction, TContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>, IServiceProvider> configureOptions)
public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext>, IServiceProvider> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
return builder.ConfigureServices((context, services) => services.AddApplicationCommands(configureOptions));
}

public static IHostBuilder UseApplicationCommands<TInteraction, TContext, TAutocompleteContext>(this IHostBuilder builder)
public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext,
[DAM(DAMT.PublicConstructors)] TAutocompleteContext>(
this IHostBuilder builder)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext where TAutocompleteContext : IAutocompleteInteractionContext
{
return builder.UseApplicationCommands<TInteraction, TContext, TAutocompleteContext>((_, _) => { });
}

public static IHostBuilder UseApplicationCommands<TInteraction, TContext, TAutocompleteContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>> configureOptions)
public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext,
[DAM(DAMT.PublicConstructors)] TAutocompleteContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
where TAutocompleteContext : IAutocompleteInteractionContext
{
return builder.UseApplicationCommands<TInteraction, TContext, TAutocompleteContext>((options, _) => configureOptions(options));
}

public static IHostBuilder UseApplicationCommands<TInteraction, TContext, TAutocompleteContext>(this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>, IServiceProvider> configureOptions)
public static IHostBuilder UseApplicationCommands<TInteraction,
[DAM(DAMT.PublicConstructors)] TContext,
[DAM(DAMT.PublicConstructors)] TAutocompleteContext>(
this IHostBuilder builder,
Action<ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>, IServiceProvider> configureOptions)

where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
where TAutocompleteContext : IAutocompleteInteractionContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ public static IHost AddMessageCommand<TContext>(this IHost host,
return host;
}

public static IHost AddApplicationCommandModule<TContext>(this IHost host, Type type) where TContext : IApplicationCommandContext
public static IHost AddApplicationCommandModule<TContext>(
this IHost host,
[DAM(DAMT.PublicConstructors | DAMT.PublicMethods | DAMT.PublicNestedTypes)] Type type)

where TContext : IApplicationCommandContext
{
var service = host.Services.GetRequiredService<ApplicationCommandService<TContext>>();
service.AddModule(type);
return host;
}

public static IHost AddApplicationCommandModule<TContext, T>(this IHost host) where TContext : IApplicationCommandContext
public static IHost AddApplicationCommandModule<TContext,
[DAM(DAMT.PublicConstructors | DAMT.PublicMethods | DAMT.PublicNestedTypes)] T>(
this IHost host)

where TContext : IApplicationCommandContext
{
var service = host.Services.GetRequiredService<ApplicationCommandService<TContext>>();
service.AddModule<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class ApplicationCommandServiceOptions
public bool? UseScopes { get; set; }
}

public class ApplicationCommandServiceOptions<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext
public class ApplicationCommandServiceOptions<TInteraction, TContext>
where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
{
public Dictionary<Type, SlashCommandTypeReader<TContext>> TypeReaders { get; set; } = ApplicationCommandServiceConfiguration<TContext>.Default.TypeReaders.ToDictionary();

Expand Down Expand Up @@ -106,7 +108,11 @@ internal ApplicationCommandServiceConfiguration<TContext> CreateConfiguration()
}
}

public class ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext> : ApplicationCommandServiceOptions<TInteraction, TContext> where TInteraction : ApplicationCommandInteraction where TContext : IApplicationCommandContext where TAutocompleteContext : IAutocompleteInteractionContext
public class ApplicationCommandServiceOptions<TInteraction, TContext, TAutocompleteContext>
: ApplicationCommandServiceOptions<TInteraction, TContext>
where TInteraction : ApplicationCommandInteraction
where TContext : IApplicationCommandContext
where TAutocompleteContext : IAutocompleteInteractionContext
{
public Func<AutocompleteInteraction, GatewayClient?, IServiceProvider, TAutocompleteContext>? CreateAutocompleteContext { get; set; }

Expand Down
Loading