Skip to content

Commit 383f3b8

Browse files
committed
prevent building bot at design time & fix event handler injection error
1 parent f7b1830 commit 383f3b8

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

Boolean/EventHandlers.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
using Discord.Interactions;
66
using Discord.WebSocket;
77
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.Extensions.DependencyInjection;
89

910
namespace Boolean;
1011

1112
public class EventHandlers(
12-
DataContext db,
1313
IServiceProvider serviceProvider,
1414
Config config,
1515
DiscordSocketClient client,
@@ -84,6 +84,9 @@ public async Task UserJoined(IGuildUser user)
8484
if (user.IsBot)
8585
return;
8686

87+
// We can't pass in data context to the class because EventHandlers is a singleton and data context is scoped
88+
var db = serviceProvider.GetRequiredService<DataContext>();
89+
8790
var guild = await db.Guilds.FirstOrDefaultAsync(g => g.Snowflake == user.Guild.Id);
8891
if (guild?.JoinRoleSnowflake != null)
8992
await user.AddRoleAsync(guild.JoinRoleSnowflake ?? 0);

Boolean/Modules/UserInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Boolean.Util;
22
using Discord;
33
using Discord.Interactions;
4-
using Discord.WebSocket;
54
using Microsoft.EntityFrameworkCore;
65

76
namespace Boolean;

Boolean/Program.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,44 @@ class Program
1616

1717
public static async Task Main()
1818
{
19-
// Dependency injection & application setup
20-
_client = new DiscordSocketClient(new DiscordSocketConfig()
19+
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
20+
Config config = builder.Configuration.Get<Config>()
21+
?? throw new Exception("Failed to load valid config from appsettings.json, please refer to the README.md for instructions.");
22+
23+
builder.Services
24+
.AddDbContext<DataContext>(options => options.UseNpgsql(config.GetConnectionString()));
25+
26+
// Only start the bot outside of design time (avoids app running during dotnet ef commands)
27+
if (EF.IsDesignTime) {
28+
_serviceProvider = builder.Services.BuildServiceProvider();
29+
goto buildApp;
30+
}
31+
32+
_client = new DiscordSocketClient(new DiscordSocketConfig
2133
{
2234
GatewayIntents = GatewayIntents.All,
2335
UseInteractionSnowflakeDate = false // Prevents a funny from happening when your OS clock is out of sync
2436
});
2537

2638
var interactionService = new InteractionService(_client.Rest);
2739

28-
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
29-
Config config = builder.Configuration.Get<Config>()
30-
?? throw new Exception("Failed to load valid config from appsettings.json, please refer to the README.md for instructions.");
31-
32-
_serviceProvider = builder.Services
33-
.AddDbContext<DataContext>(options => options.UseNpgsql(config.GetConnectionString()))
40+
builder.Services
3441
.AddSingleton(interactionService)
3542
.AddSingleton(_client)
3643
.AddSingleton(config)
37-
.AddSingleton<EventHandlers>()
38-
.BuildServiceProvider();
44+
.AddSingleton<EventHandlers>();
45+
46+
_serviceProvider = builder.Services.BuildServiceProvider();
3947

40-
// Start the bot
4148
await _client.LoginAsync(TokenType.Bot, config.DiscordToken);
4249
await _client.StartAsync();
4350

4451
await interactionService.AddModulesAsync(Assembly.GetEntryAssembly(), _serviceProvider);
4552
AttachEventHandlers();
4653

47-
IHost app = builder.Build();
48-
await app.RunAsync();
54+
buildApp:
55+
IHost app = builder.Build();
56+
await app.RunAsync();
4957
}
5058

5159
private static void AttachEventHandlers()

0 commit comments

Comments
 (0)