コード例 #1
0
ファイル: PingModule.cs プロジェクト: madman2021/MODiX
        public PingModule(DiscordSocketClient discordClient, DiscordRestClient restClient, IHttpClientFactory httpClientFactory)
        {
            _discordClient = discordClient;
            _restClient    = restClient;
            _httpClient    = httpClientFactory.CreateClient();

            //Some APIs require this
            _httpClient.DefaultRequestHeaders.Add("User-Agent", "MODiX");

            _latencyProviders = new Dictionary <string, Func <Task <double> > >
            {
                ["Discord Gateway"] = () => Task.FromResult((double)_discordClient.Latency),
                ["Google"]          = async() => (await new Ping().SendPingAsync("8.8.8.8", PingTimeout)).RoundtripTime,
                ["Cloudflare"]      = async() => (await new Ping().SendPingAsync("1.1.1.1", PingTimeout)).RoundtripTime
            };

            _availabilityProviders = new Dictionary <string, Func <Task <bool> > >
            {
                ["Github REST"]  = () => EndpointAvailable("https://api.github.com"),
                ["Discord REST"] = () => EndpointAvailable(() => _restClient.GetApplicationInfoAsync())
            };
        }
コード例 #2
0
        async Task IService.InitializeAsync()
        {
            var config = Basic;

            DiscordToken             = config.DiscordToken;
            DefaultPrefix            = config.DefaultPrefix;
            PostgresConnectionString = config.PostgresConnectionString;
            OwnerIds      = config.OwnerIds;
            _successColor = config._successColor;
            _warnColor    = config._warnColor;
            _errorColor   = config._errorColor;

            if (OwnerIds.Count == 0)
            {
                await _logging.LogDebugAsync("No owner IDs found. Fetching the bot owner's ID.",
                                             "Configuration");

                var app = await _restClient.GetApplicationInfoAsync();

                await _logging.LogDebugAsync($"Got owner {app.Owner}.", "Configuration");

                OwnerIds = new List <ulong>
                {
                    app.Owner.Id
                };
            }

            using (var ctx = new AdminDatabaseContext())
            {
                await _logging.LogDebugAsync("Attempting database connection.", "Configuration");

                if (!await ctx.Database.CanConnectAsync())
                {
                    await _logging.LogCriticalAsync(
                        "Could not connect to the PostgreSQL database using the following connection string:\n" +
                        PostgresConnectionString, "Configuration");

                    Console.ReadKey();
                    Environment.Exit(-1);
                }
                else
                {
                    await _logging.LogDebugAsync("Connection successful.", "Configuration");
                }

                await _logging.LogDebugAsync("Checking for pending database migrations.", "Configuration");

                var migrations = (await ctx.Database.GetPendingMigrationsAsync()).ToList();
                if (migrations.Count > 0)
                {
                    await _logging.LogDebugAsync($"{migrations.Count} new migration(s) found.\n" +
                                                 "Attempting to apply migrations.", "Configuration");

                    try
                    {
                        await ctx.Database.MigrateAsync();
                    }
                    catch (Exception ex)
                    {
                        await _logging.LogCriticalAsync($"An error occurred migrating the PostgreSQL database:\n{ex}",
                                                        "Configuration");

                        Console.ReadKey();
                        Environment.Exit(-1);
                    }
                }
                else
                {
                    await _logging.LogDebugAsync("No migrations found.", "Configuration");
                }
            }

            await _logging.LogInfoAsync("Initialized.", "Configuration");
        }