11using Discord ;
22using Discord . Interactions ;
3- using Discord . Rest ;
43using Discord . WebSocket ;
4+ using Microsoft . Extensions . DependencyInjection ;
55using Microsoft . Extensions . Logging ;
66using SabaBot . Database ;
7- using Zenject ;
87
98namespace SabaBot ;
109
11- internal class ApplicationInstaller : Installer {
12- public override void InstallBindings ( ) {
13- //adapters
14- Container . BindInterfacesAndSelfTo < ZenjectServiceScopeFactory > ( ) . AsSingle ( ) . Lazy ( ) ;
15- Container . BindInterfacesAndSelfTo < ZenjectServiceProvider > ( ) . AsSingle ( ) . Lazy ( ) ;
16- //base dependencies
10+ internal static class ApplicationInstaller {
11+ public static ServiceProvider Install ( IServiceCollection services ) {
12+ // Base dependencies
1713 var config = new DiscordSocketConfig {
1814 GatewayIntents = GatewayIntents . AllUnprivileged | GatewayIntents . MessageContent ,
1915 MessageCacheSize = 1000
2016 } ;
17+
2118 var socketClient = new DiscordSocketClient ( config ) ;
22- Container . Bind < DiscordSocketClient > ( ) . FromInstance ( socketClient ) . AsSingle ( ) . Lazy ( ) ;
23- Container . Bind < DiscordRestClient > ( ) . FromInstance ( socketClient . Rest ) . AsSingle ( ) . Lazy ( ) ;
24- Container . Bind < InteractionService > ( ) . AsSingle ( ) . Lazy ( ) ;
25- Container . Bind < ApplicationContext > ( ) . AsSingle ( ) . Lazy ( ) ;
26- //logging
27- InstallLogger ( ) ;
28- //services
29- InstallServices ( ) ;
30- //installing apis
31- InstallAPI ( ) ;
32- //starting
33- Container . Bind < ModuleLoader > ( ) . AsSingle ( ) . NonLazy ( ) ;
34- Container . Bind < Bootstrapper > ( ) . AsSingle ( ) . NonLazy ( ) ;
35- //a little workaround to start non-lazy bindings
36- Bootstrap ( ) ;
37- }
19+ services . AddSingleton ( socketClient ) ;
20+ services . AddSingleton ( socketClient . Rest ) ;
3821
39- private void InstallAPI ( ) {
40- Container . Bind < HttpClient > ( ) . AsSingle ( ) ;
41- Container . BindInterfacesTo < BeatLeaderAPI > ( ) . AsSingle ( ) ;
42- }
22+ var interactionService = new InteractionService ( socketClient ) ;
23+ services . AddSingleton ( interactionService ) ;
24+ services . AddDbContext < ApplicationContext > ( ) ;
25+ services . AddSingleton < ILocalization , Localization > ( ) ;
26+
27+ // Logging
28+ services . AddLoggingEnhanced ( ) ;
29+
30+ // Services
31+ services . AddSingleton < InteractionManagementService > ( ) ;
32+ services . AddService < DiscordLoggerService > ( ) ;
33+ services . AddService < MessageService > ( ) ;
34+ services . AddService < ReactionChampService > ( ) ;
35+ services . AddService < LeaveNotifService > ( ) ;
4336
44- private void InstallServices ( ) {
45- Container . Bind ( typeof ( ISystemService ) , typeof ( ILocalization ) ) . To < Localization > ( ) . AsSingle ( ) ;
46- Container . BindInterfacesTo < DiscordLoggerService > ( ) . AsSingle ( ) ;
47- Container . BindInterfacesTo < InteractionManagementService > ( ) . AsSingle ( ) ;
48- //Container.BindInterfacesTo<OpenAIChatBot>().AsSingle();
49- Container . BindInterfacesTo < MessageRewindChatBot > ( ) . AsSingle ( ) ;
50- Container . BindInterfacesTo < MessageService > ( ) . AsSingle ( ) ;
51- Container . BindInterfacesAndSelfTo < ReactionChampService > ( ) . AsSingle ( ) ;
52- Container . BindInterfacesAndSelfTo < LeaveNotifService > ( ) . AsSingle ( ) ;
37+ // Installing apis
38+ services . AddSingleton < HttpClient > ( ) ;
39+ services . AddSingleton < IBeatLeaderAPI , BeatLeaderAPI > ( ) ;
40+ services . AddSingleton < IChatBot , MessageRewindChatBot > ( ) ;
41+
42+ // Starting
43+ services . AddSingleton < ModuleLoader > ( ) ;
44+ services . AddSingleton < Bootstrapper > ( ) ;
45+
46+ // A little workaround to start non-lazy bindings
47+ var provider = services . BuildServiceProvider ( ) ;
48+ provider . GetRequiredService < ModuleLoader > ( ) ;
49+ provider . GetRequiredService < Bootstrapper > ( ) . Start ( ) ;
50+
51+ return provider ;
5352 }
54-
55- private void InstallLogger ( ) {
53+
54+ private static void AddLoggingEnhanced ( this IServiceCollection services ) {
5655 var factory = LoggerFactory . Create ( x => x . AddConsole ( ) . SetMinimumLevel ( LogLevel . Information ) ) ;
5756 var logger = factory . CreateLogger ( "Bot" ) ;
58- Container . Bind < ILoggerFactory > ( ) . FromInstance ( factory ) . AsSingle ( ) ;
59- Container . Bind < ILogger > ( ) . FromInstance ( logger ) . AsTransient ( ) ;
57+
58+ services . AddSingleton ( factory ) ;
59+ services . AddSingleton ( logger ) ;
60+ services . AddSingleton ( typeof ( ILogger < > ) , typeof ( Logger < > ) ) ;
6061 }
6162
62- private void Bootstrap ( ) {
63- Container . Resolve < ModuleLoader > ( ) ;
64- Container . Resolve < Bootstrapper > ( ) ;
63+ private static void AddService < T > ( this IServiceCollection services ) where T : class , IService {
64+ services . AddSingleton < T > ( ) ;
65+ services . AddSingleton < IService , T > ( ) ;
6566 }
6667}
0 commit comments