bot: enable .NET configuration management
This commit is contained in:
		| @@ -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<IDbConnection>() as NpgsqlConnection; | ||||
|                 connection.ConnectionString = Environment.GetEnvironmentVariable("PK_DATABASE_URI"); | ||||
|                 connection.ConnectionString = services.GetRequiredService<CoreConfig>().Database; | ||||
|                 await connection.OpenAsync(); | ||||
|                 await Schema.CreateTables(connection); | ||||
|  | ||||
|                 Console.WriteLine("- Connecting to Discord..."); | ||||
|                 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(); | ||||
|  | ||||
|                 Console.WriteLine("- Initializing bot..."); | ||||
| @@ -54,6 +55,9 @@ namespace PluralKit.Bot | ||||
|         } | ||||
|  | ||||
|         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<IDbConnection, NpgsqlConnection>() | ||||
|                 .AddSingleton<Bot>() | ||||
|   | ||||
							
								
								
									
										7
									
								
								PluralKit.Bot/BotConfig.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								PluralKit.Bot/BotConfig.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| namespace PluralKit.Bot | ||||
| { | ||||
|     public class BotConfig | ||||
|     { | ||||
|         public string Token { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										7
									
								
								PluralKit.Core/CoreConfig.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								PluralKit.Core/CoreConfig.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| namespace PluralKit | ||||
| { | ||||
|     public class CoreConfig | ||||
|     { | ||||
|         public string Database { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -7,6 +7,10 @@ | ||||
|     <ItemGroup> | ||||
|       <PackageReference Include="Dapper" Version="1.60.6" /> | ||||
|       <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" /> | ||||
|     </ItemGroup> | ||||
|  | ||||
|   | ||||
| @@ -25,15 +25,15 @@ namespace PluralKit { | ||||
|         } | ||||
|  | ||||
|         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) { | ||||
|             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) { | ||||
|             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) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user