Refactor Sentry initialization in Program.cs to conditionally set DSN based on configuration; enhance DiscordService to create a service scope for command module registration.

This commit is contained in:
2025-12-14 01:24:24 +07:00
parent 71aff19eb5
commit eff0c11f26
3 changed files with 31 additions and 22 deletions

BIN
src/.DS_Store vendored

Binary file not shown.

View File

@@ -42,12 +42,16 @@ builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnC
builder.Configuration.AddEnvironmentVariables();
builder.Configuration.AddUserSecrets<Program>();
// Initialize Sentry conditionally based on DSN availability
var sentryDsn = builder.Configuration["Sentry:Dsn"];
if (!string.IsNullOrWhiteSpace(sentryDsn))
{
SentrySdk.Init(options =>
{
// A Sentry Data Source Name (DSN) is required.
// See https://docs.sentry.io/concepts/key-terms/dsn-explainer/
// You can set it in the SENTRY_DSN environment variable, or you can set it in code here.
options.Dsn = builder.Configuration["Sentry:Dsn"];
options.Dsn = sentryDsn;
// When debug is enabled, the Sentry client will emit detailed debugging information to the console.
// This might be helpful, or might interfere with the normal operation of your application.
@@ -69,6 +73,7 @@ SentrySdk.Init(options =>
options.Environment = builder.Environment.EnvironmentName;
});
}
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["api"]);

View File

@@ -1,4 +1,4 @@
using Discord;
using Discord;
using Discord.Commands;
using Discord.Net;
using Discord.WebSocket;
@@ -64,7 +64,11 @@ namespace Managing.Infrastructure.Messengers.Discord
_client.Log += Log;
_commandService.Log += Log;
// look for classes implementing ModuleBase to load commands from
await _commandService.AddModulesAsync(GetType().Assembly, _services);
// Create a service scope to resolve scoped services during command module registration
using (var scope = _services.CreateScope())
{
await _commandService.AddModulesAsync(GetType().Assembly, scope.ServiceProvider);
}
// log in to Discord, using the provided token
await _client.LoginAsync(TokenType.Bot, _settings.Token);
// start bot