bot: enable .NET configuration management

This commit is contained in:
Ske 2019-05-08 20:08:56 +02:00
parent c5d2b7c251
commit 9b49f22048
5 changed files with 33 additions and 11 deletions

View File

@ -8,19 +8,20 @@ using Dapper;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Npgsql; using Npgsql;
using Npgsql.BackendMessages;
using Npgsql.PostgresTypes;
using Npgsql.TypeHandling;
using Npgsql.TypeMapping;
using NpgsqlTypes;
namespace PluralKit.Bot namespace PluralKit.Bot
{ {
class Initialize 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() private async Task MainAsync()
{ {
@ -37,13 +38,13 @@ namespace PluralKit.Bot
{ {
Console.WriteLine("- Connecting to database..."); Console.WriteLine("- Connecting to database...");
var connection = services.GetRequiredService<IDbConnection>() as NpgsqlConnection; var connection = services.GetRequiredService<IDbConnection>() as NpgsqlConnection;
connection.ConnectionString = Environment.GetEnvironmentVariable("PK_DATABASE_URI"); connection.ConnectionString = services.GetRequiredService<CoreConfig>().Database;
await connection.OpenAsync(); await connection.OpenAsync();
await Schema.CreateTables(connection); await Schema.CreateTables(connection);
Console.WriteLine("- Connecting to Discord..."); Console.WriteLine("- Connecting to Discord...");
var client = services.GetRequiredService<IDiscordClient>() as DiscordSocketClient; var client = services.GetRequiredService<IDiscordClient>() as DiscordSocketClient;
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("PK_TOKEN")); await client.LoginAsync(TokenType.Bot, services.GetRequiredService<BotConfig>().Token);
await client.StartAsync(); await client.StartAsync();
Console.WriteLine("- Initializing bot..."); Console.WriteLine("- Initializing bot...");
@ -54,6 +55,9 @@ namespace PluralKit.Bot
} }
public ServiceProvider BuildServiceProvider() => new ServiceCollection() public ServiceProvider BuildServiceProvider() => new ServiceCollection()
.AddSingleton(_config.GetSection("PluralKit").Get<CoreConfig>() ?? new CoreConfig())
.AddSingleton(_config.GetSection("PluralKit").GetSection("Bot").Get<BotConfig>() ?? new BotConfig())
.AddSingleton<IDiscordClient, DiscordSocketClient>() .AddSingleton<IDiscordClient, DiscordSocketClient>()
.AddSingleton<IDbConnection, NpgsqlConnection>() .AddSingleton<IDbConnection, NpgsqlConnection>()
.AddSingleton<Bot>() .AddSingleton<Bot>()

View File

@ -0,0 +1,7 @@
namespace PluralKit.Bot
{
public class BotConfig
{
public string Token { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace PluralKit
{
public class CoreConfig
{
public string Database { get; set; }
}
}

View File

@ -7,6 +7,10 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Dapper" Version="1.60.6" /> <PackageReference Include="Dapper" Version="1.60.6" />
<PackageReference Include="Dapper.Contrib" Version="1.60.1" /> <PackageReference Include="Dapper.Contrib" Version="1.60.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Npgsql" Version="4.0.6" /> <PackageReference Include="Npgsql" Version="4.0.6" />
</ItemGroup> </ItemGroup>

View File

@ -25,15 +25,15 @@ namespace PluralKit {
} }
public async Task<PKSystem> GetByAccount(ulong accountId) { public async Task<PKSystem> GetByAccount(ulong accountId) {
return await conn.QuerySingleAsync<PKSystem>("select systems.* from systems, accounts where accounts.system = system.id and accounts.uid = @Id", new { Id = accountId }); return await conn.QuerySingleOrDefaultAsync<PKSystem>("select systems.* from systems, accounts where accounts.system = system.id and accounts.uid = @Id", new { Id = accountId });
} }
public async Task<PKSystem> GetByHid(string hid) { public async Task<PKSystem> GetByHid(string hid) {
return await conn.QuerySingleAsync<PKSystem>("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() }); return await conn.QuerySingleOrDefaultAsync<PKSystem>("select * from systems where systems.hid = @Hid", new { Hid = hid.ToLower() });
} }
public async Task<PKSystem> GetByToken(string token) { public async Task<PKSystem> GetByToken(string token) {
return await conn.QuerySingleAsync<PKSystem>("select * from systems where token = @Token", new { Token = token }); return await conn.QuerySingleOrDefaultAsync<PKSystem>("select * from systems where token = @Token", new { Token = token });
} }
public async Task Save(PKSystem system) { public async Task Save(PKSystem system) {