Switch to file-based configuration

This commit is contained in:
Noikoio 2019-06-20 20:52:11 -07:00
parent c1e8f6699a
commit 2319c91fc7

View file

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.WebSocket; using Discord.WebSocket;
using Kerobot.Common; using Kerobot.Common;
@ -22,6 +24,7 @@ namespace Kerobot.Services.GuildState
public GuildStateService(Kerobot kb) : base(kb) public GuildStateService(Kerobot kb) : base(kb)
{ {
_moderators = new Dictionary<ulong, EntityList>();
_states = new Dictionary<ulong, Dictionary<Type, StateInfo>>(); _states = new Dictionary<ulong, Dictionary<Type, StateInfo>>();
CreateDatabaseTablesAsync().Wait(); CreateDatabaseTablesAsync().Wait();
@ -110,7 +113,6 @@ namespace Kerobot.Services.GuildState
{ {
var jstr = await RetrieveConfiguration(guildId); var jstr = await RetrieveConfiguration(guildId);
if (jstr == null) jstr = await RetrieveDefaultConfiguration();
int jstrHash = jstr.GetHashCode(); int jstrHash = jstr.GetHashCode();
JObject guildConf; JObject guildConf;
try try
@ -208,7 +210,7 @@ namespace Kerobot.Services.GuildState
c.CommandText = $"insert into {DBTableName} (rev_id, guild_id, author, config_json) " c.CommandText = $"insert into {DBTableName} (rev_id, guild_id, author, config_json) "
+ "values (0, 0, 0, @Json) " + "values (0, 0, 0, @Json) "
+ "on conflict (rev_id) do nothing"; + "on conflict (rev_id) do nothing";
c.Parameters.Add("@Json", NpgsqlTypes.NpgsqlDbType.Text).Value = PreloadDefaultGuildJson(); c.Parameters.Add("@Json", NpgsqlTypes.NpgsqlDbType.Text).Value = GetDefaultConfiguration();
c.Prepare(); c.Prepare();
await c.ExecuteNonQueryAsync(); await c.ExecuteNonQueryAsync();
} }
@ -217,43 +219,28 @@ namespace Kerobot.Services.GuildState
private async Task<string> RetrieveConfiguration(ulong guildId) private async Task<string> RetrieveConfiguration(ulong guildId)
{ {
using (var db = await Kerobot.GetOpenNpgsqlConnectionAsync()) // Offline option: Per-guild configuration exists under `config/(guild ID).json`
var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) +
Path.DirectorySeparatorChar + "config" + Path.DirectorySeparatorChar;
if (!Directory.Exists(basePath)) Directory.CreateDirectory(basePath);
var path = basePath + guildId + ".json";
if (File.Exists(path))
{ {
using (var c = db.CreateCommand()) return await File.ReadAllTextAsync(path);
}
else
{ {
c.CommandText = $"select config_json from {DBTableName} where guild_id = {guildId} " await File.WriteAllTextAsync(path, GetDefaultConfiguration());
+ "order by rev_id desc limit 1"; await Log($"Created initial configuration file in config{Path.DirectorySeparatorChar}{guildId}.json");
using (var r = await c.ExecuteReaderAsync()) return await RetrieveConfiguration(guildId);
{
if (await r.ReadAsync())
{
return r.GetString(0);
} }
return null;
}
}
}
}
private async Task<string> RetrieveDefaultConfiguration()
{
using (var db = await Kerobot.GetOpenNpgsqlConnectionAsync())
{
using (var c = db.CreateCommand())
{
c.CommandText = $"select config_json from {DBTableName} where rev_id = 0";
using (var r = await c.ExecuteReaderAsync())
{
if (await r.ReadAsync()) return r.GetString(0);
}
}
}
throw new Exception("Unable to retrieve fallback configuration.");
} }
#endregion #endregion
// Default guild configuration JSON is embedded in assembly. /// <summary>
private string PreloadDefaultGuildJson() /// Retrieves the default configuration loaded within the assembly.
/// </summary>
private string GetDefaultConfiguration()
{ {
const string ResourceName = "Kerobot.DefaultGuildConfig.json"; const string ResourceName = "Kerobot.DefaultGuildConfig.json";