diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 41591916..13f4d545 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -8,19 +8,20 @@ using Dapper; using Discord; using Discord.Commands; using Discord.WebSocket; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Npgsql; -using Npgsql.BackendMessages; -using Npgsql.PostgresTypes; -using Npgsql.TypeHandling; -using Npgsql.TypeMapping; -using NpgsqlTypes; namespace PluralKit.Bot { class Initialize { - static void Main() => new Initialize().MainAsync().GetAwaiter().GetResult(); + private IConfiguration _config; + + static void Main(string[] args) => new Initialize { _config = new ConfigurationBuilder() + .AddEnvironmentVariables() + .AddCommandLine(args) + .Build()}.MainAsync().GetAwaiter().GetResult(); private async Task MainAsync() { @@ -37,13 +38,13 @@ namespace PluralKit.Bot { Console.WriteLine("- Connecting to database..."); var connection = services.GetRequiredService() as NpgsqlConnection; - connection.ConnectionString = Environment.GetEnvironmentVariable("PK_DATABASE_URI"); + connection.ConnectionString = services.GetRequiredService().Database; await connection.OpenAsync(); await Schema.CreateTables(connection); Console.WriteLine("- Connecting to Discord..."); var client = services.GetRequiredService() as DiscordSocketClient; - await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("PK_TOKEN")); + await client.LoginAsync(TokenType.Bot, services.GetRequiredService().Token); await client.StartAsync(); Console.WriteLine("- Initializing bot..."); @@ -54,6 +55,9 @@ namespace PluralKit.Bot } public ServiceProvider BuildServiceProvider() => new ServiceCollection() + .AddSingleton(_config.GetSection("PluralKit").Get() ?? new CoreConfig()) + .AddSingleton(_config.GetSection("PluralKit").GetSection("Bot").Get() ?? new BotConfig()) + .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/PluralKit.Bot/BotConfig.cs b/PluralKit.Bot/BotConfig.cs new file mode 100644 index 00000000..87c2035a --- /dev/null +++ b/PluralKit.Bot/BotConfig.cs @@ -0,0 +1,7 @@ +namespace PluralKit.Bot +{ + public class BotConfig + { + public string Token { get; set; } + } +} \ No newline at end of file diff --git a/PluralKit.Core/CoreConfig.cs b/PluralKit.Core/CoreConfig.cs new file mode 100644 index 00000000..38039b67 --- /dev/null +++ b/PluralKit.Core/CoreConfig.cs @@ -0,0 +1,7 @@ +namespace PluralKit +{ + public class CoreConfig + { + public string Database { get; set; } + } +} \ No newline at end of file diff --git a/PluralKit.Core/PluralKit.Core.csproj b/PluralKit.Core/PluralKit.Core.csproj index 54d15f28..12484212 100644 --- a/PluralKit.Core/PluralKit.Core.csproj +++ b/PluralKit.Core/PluralKit.Core.csproj @@ -7,6 +7,10 @@ + + + + diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index e81032ce..3dba40f6 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -25,15 +25,15 @@ namespace PluralKit { } public async Task GetByAccount(ulong accountId) { - return await conn.QuerySingleAsync("select systems.* from systems, accounts where accounts.system = system.id and accounts.uid = @Id", new { Id = accountId }); + return await conn.QuerySingleOrDefaultAsync("select systems.* from systems, accounts where accounts.system = system.id and accounts.uid = @Id", new { Id = accountId }); } public async Task GetByHid(string hid) { - return await conn.QuerySingleAsync("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() }); + return await conn.QuerySingleOrDefaultAsync("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() }); } public async Task GetByToken(string token) { - return await conn.QuerySingleAsync("select * from systems where token = @Token", new { Token = token }); + return await conn.QuerySingleOrDefaultAsync("select * from systems where token = @Token", new { Token = token }); } public async Task Save(PKSystem system) {