Esempio n. 1
0
        public async Task LoadServicesAsync(MikiAppBuilder app)
        {
            var cache = new StackExchangeCacheClient(
                new ProtobufSerializer(),
                await ConnectionMultiplexer.ConnectAsync(Global.Config.RedisConnectionString)
                );

            app.AddSingletonService <ICacheClient>(cache);
            app.AddSingletonService <IExtendedCacheClient>(cache);
            app.Services.AddDbContext <DbContext, MikiContext>(x => x.UseNpgsql(Global.Config.ConnString));

            app.AddSingletonService(new LoggingService());

            if (!string.IsNullOrWhiteSpace(Global.Config.MikiApiBaseUrl) && !string.IsNullOrWhiteSpace(Global.Config.MikiApiKey))
            {
                app.AddSingletonService(new MikiApiClient(Global.Config.MikiApiKey));
            }
            else
            {
                Log.Warning("No Miki API parameters were supplied, ignoring Miki API.");
            }

            app.AddSingletonService <IApiClient>(new DiscordApiClient(Global.Config.Token, cache));

            if (Global.Config.SelfHosted)
            {
                var gatewayConfig = GatewayConfiguration.Default();
                gatewayConfig.ShardCount      = 1;
                gatewayConfig.ShardId         = 0;
                gatewayConfig.Token           = Global.Config.Token;
                gatewayConfig.WebSocketClient = new BasicWebSocketClient();
                app.AddSingletonService <IGateway>(new CentralizedGatewayShard(gatewayConfig));
            }
            else
            {
                app.AddSingletonService <IGateway>(new DistributedGateway(new MessageClientConfiguration
                {
                    ConnectionString = new Uri(Global.Config.RabbitUrl.ToString()),
                    QueueName        = "gateway",
                    ExchangeName     = "consumer",
                    ConsumerAutoAck  = false,
                    PrefetchCount    = 25
                }));
            }

            app.AddSingletonService(new BunnyCDNClient(Global.Config.BunnyCdnKey));
            app.AddSingletonService(new ConfigurationManager());
            app.AddSingletonService(new EventSystem(new EventSystemConfig()
            {
                Developers = Global.Config.DeveloperIds,
            }));

            app.AddSingletonService(new BackgroundStore());

            if (!string.IsNullOrWhiteSpace(Global.Config.SharpRavenKey))
            {
                app.AddSingletonService(new RavenClient(Global.Config.SharpRavenKey));
            }
            else
            {
                Log.Warning("Sentry.io key not provided, ignoring distributed error logging...");
            }
        }
Esempio n. 2
0
        public async Task LoadServicesAsync(MikiAppBuilder app)
        {
            new LogBuilder()
            .AddLogEvent((msg, lvl) =>
            {
                if (lvl >= Global.Config.LogLevel)
                {
                    Console.WriteLine(msg);
                }
            })
            .SetLogHeader((msg) => $"[{msg}]: ")
            .SetTheme(new LogTheme())
            .Apply();

            var cache = new StackExchangeCacheClient(
                new ProtobufSerializer(),
                await ConnectionMultiplexer.ConnectAsync(Global.Config.RedisConnectionString)
                );

            // Setup Redis
            {
                app.AddSingletonService <ICacheClient>(cache);
                app.AddSingletonService <IExtendedCacheClient>(cache);
            }

            // Setup Entity Framework
            {
                app.Services.AddDbContext <MikiDbContext>(x
                                                          => x.UseNpgsql(Global.Config.ConnString, b => b.MigrationsAssembly("Miki.Bot.Models")));
                app.Services.AddDbContext <DbContext, MikiDbContext>(x
                                                                     => x.UseNpgsql(Global.Config.ConnString, b => b.MigrationsAssembly("Miki.Bot.Models")));
            }

            // Setup Miki API
            {
                if (!string.IsNullOrWhiteSpace(Global.Config.MikiApiBaseUrl) && !string.IsNullOrWhiteSpace(Global.Config.MikiApiKey))
                {
                    app.AddSingletonService(new MikiApiClient(Global.Config.MikiApiKey));
                }
                else
                {
                    Log.Warning("No Miki API parameters were supplied, ignoring Miki API.");
                }
            }

            // Setup Discord
            {
                app.AddSingletonService <IApiClient>(new DiscordApiClient(Global.Config.Token, cache));
                if (Global.Config.SelfHosted)
                {
                    var gatewayConfig = new GatewayProperties();
                    gatewayConfig.ShardCount             = 1;
                    gatewayConfig.ShardId                = 0;
                    gatewayConfig.Token                  = Global.Config.Token;
                    gatewayConfig.Compressed             = true;
                    gatewayConfig.AllowNonDispatchEvents = true;
                    app.AddSingletonService <IGateway>(new GatewayCluster(gatewayConfig));
                }
                else
                {
                    app.AddSingletonService <IGateway>(new DistributedGateway(new MessageClientConfiguration
                    {
                        ConnectionString = new Uri(Global.Config.RabbitUrl.ToString()),
                        QueueName        = "gateway",
                        ExchangeName     = "consumer",
                        ConsumerAutoAck  = false,
                        PrefetchCount    = 25,
                    }));
                }
            }

            // Setup web services
            {
                app.AddSingletonService(new UrbanDictionaryAPI());
                app.AddSingletonService(new BunnyCDNClient(Global.Config.BunnyCdnKey));
            }

            // Setup miscellanious services
            {
                app.AddSingletonService(new ConfigurationManager());
                app.AddSingletonService(new EventSystem());
                app.AddSingletonService(new BackgroundStore());

                if (!string.IsNullOrWhiteSpace(Global.Config.SharpRavenKey))
                {
                    app.AddSingletonService(new RavenClient(Global.Config.SharpRavenKey));
                }
                else
                {
                    Log.Warning("Sentry.io key not provided, ignoring distributed error logging...");
                }
            }
        }