diff --git a/Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs b/Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs index c9e94893..1ef7ec9c 100644 --- a/Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs +++ b/Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; using NetCord.Gateway; @@ -19,11 +20,20 @@ public Task StartAsync(CancellationToken cancellationToken) RegisterClassHandler(client, handler); } - return client.StartAsync(cancellationToken: cancellationToken).AsTask(); + var options = services.GetRequiredService>().Value; + + return options.AutoStartStop.GetValueOrDefault(true) + ? client.StartAsync(cancellationToken: cancellationToken).AsTask() + : Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { + var options = services.GetRequiredService>().Value; + + if (!options.AutoStartStop.GetValueOrDefault(true)) + return Task.CompletedTask; + var client = services.GetRequiredService(); return client.CloseAsync(cancellationToken: cancellationToken).AsTask(); diff --git a/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs b/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs index 28838e98..f8cdd2bb 100644 --- a/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs +++ b/Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs @@ -23,6 +23,8 @@ internal partial class Validator : IValidateOptions public string? PublicKey { get; set; } + public bool? AutoStartStop { get; set; } + /// public IWebSocketConnectionProvider? WebSocketConnectionProvider { get; set; } diff --git a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs index 2d86bacb..6b6994e6 100644 --- a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs +++ b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; using NetCord.Gateway; @@ -19,11 +20,20 @@ public Task StartAsync(CancellationToken cancellationToken) RegisterClassShardedHandler(client, handler); } - return client.StartAsync(cancellationToken: cancellationToken).AsTask(); + var options = services.GetRequiredService>().Value; + + return options.AutoStartStop.GetValueOrDefault(true) + ? client.StartAsync(cancellationToken: cancellationToken).AsTask() + : Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { + var options = services.GetRequiredService>().Value; + + if (!options.AutoStartStop.GetValueOrDefault(true)) + return Task.CompletedTask; + var client = services.GetRequiredService(); return client.CloseAsync(cancellationToken: cancellationToken).AsTask(); diff --git a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs index 9770c671..6373f555 100644 --- a/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs +++ b/Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs @@ -26,6 +26,8 @@ internal partial class Validator : IValidateOptions public string? PublicKey { get; set; } + public bool? AutoStartStop { get; set; } + /// public Func? WebSocketConnectionProviderFactory { get; set; } diff --git a/Tests/NetCord.Test.Hosting/Program.cs b/Tests/NetCord.Test.Hosting/Program.cs index 1fd6dd75..5989c9a5 100644 --- a/Tests/NetCord.Test.Hosting/Program.cs +++ b/Tests/NetCord.Test.Hosting/Program.cs @@ -48,7 +48,11 @@ .ConfigureApplicationCommands(o => o.DefaultParameterDescriptionFormat = "AA") .ConfigureApplicationCommands(o => o.DefaultParameterDescriptionFormat = "XD") .ConfigureApplicationCommands(o => o.AutoRegisterCommands = true) - .AddDiscordGateway(o => o.Intents = GatewayIntents.All) + .AddDiscordGateway(o => + { + o.Intents = GatewayIntents.All; + o.AutoStartStop = true; + }) .AddApplicationCommands(options => { options.ResultHandler = new CustomApplicationCommandResultHandler();