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
12 changes: 11 additions & 1 deletion Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

using NetCord.Gateway;

Expand All @@ -19,11 +20,20 @@ public Task StartAsync(CancellationToken cancellationToken)
RegisterClassHandler(client, handler);
}

return client.StartAsync(cancellationToken: cancellationToken).AsTask();
var options = services.GetRequiredService<IOptions<GatewayClientOptions>>().Value;

return options.AutoStartStop.GetValueOrDefault(true)
? client.StartAsync(cancellationToken: cancellationToken).AsTask()
: Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
var options = services.GetRequiredService<IOptions<GatewayClientOptions>>().Value;

if (!options.AutoStartStop.GetValueOrDefault(true))
return Task.CompletedTask;

var client = services.GetRequiredService<GatewayClient>();

return client.CloseAsync(cancellationToken: cancellationToken).AsTask();
Expand Down
2 changes: 2 additions & 0 deletions Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal partial class Validator : IValidateOptions<GatewayClientOptions>

public string? PublicKey { get; set; }

public bool? AutoStartStop { get; set; }

/// <inheritdoc cref="GatewayClientConfiguration.WebSocketConnectionProvider" />
public IWebSocketConnectionProvider? WebSocketConnectionProvider { get; set; }

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

using NetCord.Gateway;

Expand All @@ -19,11 +20,20 @@ public Task StartAsync(CancellationToken cancellationToken)
RegisterClassShardedHandler(client, handler);
}

return client.StartAsync(cancellationToken: cancellationToken).AsTask();
var options = services.GetRequiredService<IOptions<ShardedGatewayClientOptions>>().Value;

return options.AutoStartStop.GetValueOrDefault(true)
? client.StartAsync(cancellationToken: cancellationToken).AsTask()
: Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
var options = services.GetRequiredService<IOptions<ShardedGatewayClientOptions>>().Value;

if (!options.AutoStartStop.GetValueOrDefault(true))
return Task.CompletedTask;

var client = services.GetRequiredService<ShardedGatewayClient>();

return client.CloseAsync(cancellationToken: cancellationToken).AsTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal partial class Validator : IValidateOptions<ShardedGatewayClientOptions>

public string? PublicKey { get; set; }

public bool? AutoStartStop { get; set; }

/// <inheritdoc cref="ShardedGatewayClientConfiguration.WebSocketConnectionProviderFactory" />
public Func<Shard, IWebSocketConnectionProvider?>? WebSocketConnectionProviderFactory { get; set; }

Expand Down
6 changes: 5 additions & 1 deletion Tests/NetCord.Test.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
.ConfigureApplicationCommands<ApplicationCommandInteraction, ApplicationCommandContext, AutocompleteInteractionContext>(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();
Expand Down
Loading