/// <summary> /// The main async entry point for the application. /// </summary> /// <returns> /// Task.Completed. /// </returns> public async Task MainAsync(string[] args) { // ------------------------------------------- // Get Environment Variables // ------------------------------------------- string xivapiKey = Environment.GetEnvironmentVariable("XIVAPI_KEY", EnvironmentVariableTarget.Machine); #if DEBUG string discordKey = Environment.GetEnvironmentVariable("BABYSPRIGGANS_API_KEY", EnvironmentVariableTarget.Machine); #else string discordKey = Environment.GetEnvironmentVariable("MASTERSPRIGGANS_API_KEY", EnvironmentVariableTarget.Machine); #endif // ------------------------------------------- // Initialize services for dependency injection // ------------------------------------------- IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext <MasterSpriggansDatabaseContext>((o) => { o.UseSqlite("Data Source=spriggans.db"); }); serviceCollection.AddSingleton <DiscordEventHandler>(); serviceCollection.AddSingleton <DiscordCommandHandler>(); serviceCollection.AddSingleton <DiscordSocketClient>(); serviceCollection.AddSingleton <XIVApi>(new XIVApi(xivapiKey)); _serviceProvider = serviceCollection.BuildServiceProvider(); // ------------------------------------------- // Ensure the database // ------------------------------------------- Logger.Message("Ensuring database is created"); MasterSpriggansDatabaseContext context = _serviceProvider.GetRequiredService <MasterSpriggansDatabaseContext>(); #if DEBUG Console.WriteLine(); while (true) { Console.WriteLine("DEBUG: Delete Database? [y/n]"); string deletedb = Console.ReadLine(); if (string.Equals("y", deletedb, StringComparison.InvariantCultureIgnoreCase)) { await context.Database.EnsureDeletedAsync(); break; } else if (string.Equals("n", deletedb, StringComparison.InvariantCultureIgnoreCase)) { break; } } #endif if (await context.Database.EnsureCreatedAsync()) { await MasterSpriggansDatabaseContext.SeedDatabase(context, _serviceProvider.GetRequiredService <XIVApi>()); } // ------------------------------------------- // Login the client // ------------------------------------------- using (_client = _serviceProvider.GetRequiredService <DiscordSocketClient>()) { _serviceProvider.GetRequiredService <DiscordEventHandler>().InitializeEvents(); await _serviceProvider.GetRequiredService <DiscordCommandHandler>().InitializeAsync(); bool loggedIn = false; try { await _client.LoginAsync(TokenType.Bot, discordKey); loggedIn = true; } catch (Exception ex) { Logger.Error($"The following exception occurred while attempting to log in the discord client. \"{ex.Message}\""); } if (loggedIn) { _client.Ready += OnClientReady; await _client.StartAsync(); await Task.Delay(-1); } } }