示例#1
0
        private void AddServices()
        {
            var startingGuildIdList = Client.Guilds.Select(x => (long)x.Id).ToList();

            //this unit of work will be used for initialization of all modules too, to prevent multiple queries from running
            using (var uow = _db.UnitOfWork)
            {
                AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();

                IBotConfigProvider botConfigProvider = new BotConfigProvider(_db, _botConfig, Cache);

                //initialize Services
                Services = new NServiceProvider()
                           .AddManual <IBotCredentials>(Credentials)
                           .AddManual(_db)
                           .AddManual(Client)
                           .AddManual(CommandService)
                           .AddManual(botConfigProvider)
                           .AddManual <NadekoBot>(this)
                           .AddManual <IUnitOfWork>(uow)
                           .AddManual <IDataCache>(Cache);

                Services.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler)));

                var commandHandler = Services.GetService <CommandHandler>();
                commandHandler.AddServices(Services);

                LoadTypeReaders(typeof(NadekoBot).Assembly);
            }
            Services.Unload(typeof(IUnitOfWork)); // unload it after the startup
        }
示例#2
0
        private void AddServices()
        {
            var startingGuildIdList = GetCurrentGuildIds();

            //this unit of work will be used for initialization of all modules too, to prevent multiple queries from running
            using (var uow = _db.GetDbContext())
            {
                var sw = Stopwatch.StartNew();

                var _bot = Client.CurrentUser;

                uow.DiscordUsers.EnsureCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);

                AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();

                IBotConfigProvider botConfigProvider = new BotConfigProvider(_db, _botConfig, Cache);

                var s = new ServiceCollection()
                        .AddSingleton <IBotCredentials>(Credentials)
                        .AddSingleton(_db)
                        .AddSingleton(Client)
                        .AddSingleton(CommandService)
                        .AddSingleton(botConfigProvider)
                        .AddSingleton(this)
                        .AddSingleton(uow)
                        .AddSingleton(Cache)
                        .AddMemoryCache();

                s.AddHttpClient();
                s.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
                {
                    AllowAutoRedirect = false
                });

                s.LoadFrom(Assembly.GetAssembly(typeof(CommandHandler)));

                //initialize Services
                Services = s.BuildServiceProvider();
                var commandHandler = Services.GetService <CommandHandler>();
                //what the fluff
                commandHandler.AddServices(s);
                LoadTypeReaders(typeof(NadekoBot).Assembly);

                sw.Stop();
                _log.Info($"All services loaded in {sw.Elapsed.TotalSeconds:F2}s");
            }
        }
        private void AddServices()
        {
            var startingGuildIdList = Client.Guilds.Select(x => (long)x.Id).ToList();

            //this unit of work will be used for initialization of all modules too, to prevent multiple queries from running
            using (var uow = _db.UnitOfWork)
            {
                AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();

                IBotConfigProvider botConfigProvider = new BotConfigProvider(_db, _botConfig);

                //var localization = new Localization(_botConfig.Locale, AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale), Db);

                //initialize Services
                Services = new NServiceProvider.ServiceProviderBuilder()
                           .AddManual <IBotCredentials>(Credentials)
                           .AddManual(_db)
                           .AddManual(Client)
                           .AddManual(CommandService)
                           .AddManual(botConfigProvider)
                                                                                    //.AddManual<ILocalization>(localization)
                           .AddManual <IEnumerable <GuildConfig> >(AllGuildConfigs) //todo wrap this
                           .AddManual <NadekoBot>(this)
                           .AddManual <IUnitOfWork>(uow)
                           .AddManual <IDataCache>(new RedisCache(Client.CurrentUser.Id))
                           .LoadFrom(Assembly.GetEntryAssembly())
                           .Build();

                var commandHandler = Services.GetService <CommandHandler>();
                commandHandler.AddServices(Services);

                //setup typereaders
                CommandService.AddTypeReader <PermissionAction>(new PermissionActionTypeReader());
                CommandService.AddTypeReader <CommandInfo>(new CommandTypeReader());
                CommandService.AddTypeReader <CommandOrCrInfo>(new CommandOrCrTypeReader());
                CommandService.AddTypeReader <ModuleInfo>(new ModuleTypeReader(CommandService));
                CommandService.AddTypeReader <ModuleOrCrInfo>(new ModuleOrCrTypeReader(CommandService));
                CommandService.AddTypeReader <IGuild>(new GuildTypeReader(Client));
                CommandService.AddTypeReader <GuildDateTime>(new GuildDateTimeTypeReader());
            }
        }