Esempio n. 1
0
        private static async Task Main()
        {
            Program p = new Program();

            var appBuilder = new MikiAppBuilder();

            await p.LoadServicesAsync(appBuilder);

            MikiApp app = appBuilder.Build();

            await p.LoadDiscord(app);

            p.LoadLocales();

            for (int i = 0; i < Global.Config.MessageWorkerCount; i++)
            {
                MessageBucket.AddWorker();
            }

            using (var c = new MikiContext())
            {
                List <User> bannedUsers = await c.Users.Where(x => x.Banned).ToListAsync();

                foreach (var u in bannedUsers)
                {
                    app.GetService <EventSystem>().MessageFilter
                    .Get <UserFilter>().Users.Add(u.Id.FromDbLong());
                }
            }

            await Task.Delay(-1);
        }
Esempio n. 2
0
        private static async Task Main(string[] args)
        {
            Program p = new Program();

            if (args.Length > 0)
            {
                if (args.Any(x => x.ToLowerInvariant() == "--migrate" && x.ToLowerInvariant() == "-m"))
                {
                    await new MikiDbContextFactory().CreateDbContext(new string[] { }).Database.MigrateAsync();
                    return;
                }
            }

            var appBuilder = new MikiAppBuilder();

            await p.LoadServicesAsync(appBuilder);

            MikiApp app = appBuilder.Build();

            await p.LoadDiscord(app);

            p.LoadLocales();

            for (int i = 0; i < Global.Config.MessageWorkerCount; i++)
            {
                MessageBucket.AddWorker();
            }

            using (var scope = app.Services.CreateScope())
            {
                var context = scope.ServiceProvider
                              .GetService <MikiDbContext>();

                List <IsBanned> bannedUsers = await context.IsBanned
                                              .Where(x => x.ExpirationDate > DateTime.UtcNow)
                                              .ToListAsync();

                foreach (var u in bannedUsers)
                {
                    app.GetService <EventSystem>().MessageFilter
                    .Get <UserFilter>().Users.Add(u.UserId.FromDbLong());
                }
            }

            await Task.Delay(-1);
        }
Esempio n. 3
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. 4
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...");
                }
            }
        }