@@ -16,36 +16,44 @@ class Program
16
16
17
17
public static async Task Main ( )
18
18
{
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
21
33
{
22
34
GatewayIntents = GatewayIntents . All ,
23
35
UseInteractionSnowflakeDate = false // Prevents a funny from happening when your OS clock is out of sync
24
36
} ) ;
25
37
26
38
var interactionService = new InteractionService ( _client . Rest ) ;
27
39
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
34
41
. AddSingleton ( interactionService )
35
42
. AddSingleton ( _client )
36
43
. AddSingleton ( config )
37
- . AddSingleton < EventHandlers > ( )
38
- . BuildServiceProvider ( ) ;
44
+ . AddSingleton < EventHandlers > ( ) ;
45
+
46
+ _serviceProvider = builder . Services . BuildServiceProvider ( ) ;
39
47
40
- // Start the bot
41
48
await _client . LoginAsync ( TokenType . Bot , config . DiscordToken ) ;
42
49
await _client . StartAsync ( ) ;
43
50
44
51
await interactionService . AddModulesAsync ( Assembly . GetEntryAssembly ( ) , _serviceProvider ) ;
45
52
AttachEventHandlers ( ) ;
46
53
47
- IHost app = builder . Build ( ) ;
48
- await app . RunAsync ( ) ;
54
+ buildApp :
55
+ IHost app = builder . Build ( ) ;
56
+ await app . RunAsync ( ) ;
49
57
}
50
58
51
59
private static void AttachEventHandlers ( )
0 commit comments