Adds presistence to log replaces
This commit is contained in:
parent
c1ad97d26f
commit
e0cc46260b
|
@ -70,7 +70,7 @@ namespace BiblioTech
|
|||
{
|
||||
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
|
||||
log.Error(json);
|
||||
throw exception;
|
||||
throw;
|
||||
}
|
||||
log.Log("Initialized.");
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace BiblioTech
|
|||
public class Program
|
||||
{
|
||||
private DiscordSocketClient client = null!;
|
||||
private readonly CustomReplacement replacement = new CustomReplacement();
|
||||
private CustomReplacement replacement = null!;
|
||||
|
||||
public static Configuration Config { get; private set; } = null!;
|
||||
public static UserRepo UserRepo { get; } = new UserRepo();
|
||||
|
@ -42,6 +42,17 @@ namespace BiblioTech
|
|||
public async Task MainAsync(string[] args)
|
||||
{
|
||||
Log.Log("Starting Codex Discord Bot...");
|
||||
try
|
||||
{
|
||||
replacement = new CustomReplacement(Config);
|
||||
replacement.Load();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("Failed to load logReplacements: " + ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (Config.DebugNoDiscord)
|
||||
{
|
||||
Log.Log("Debug option is set. Discord connection disabled!");
|
||||
|
|
|
@ -1,8 +1,30 @@
|
|||
namespace BiblioTech.Rewards
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BiblioTech.Rewards
|
||||
{
|
||||
public class CustomReplacement
|
||||
{
|
||||
private readonly Dictionary<string, string> replacements = new Dictionary<string, string>();
|
||||
private readonly string file;
|
||||
|
||||
public CustomReplacement(Configuration config)
|
||||
{
|
||||
file = Path.Combine(config.DataPath, "logreplacements.json");
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
replacements.Clear();
|
||||
if (!File.Exists(file)) return;
|
||||
|
||||
var replaces = JsonConvert.DeserializeObject<ReplaceJson[]>(File.ReadAllText(file));
|
||||
if (replaces == null) return;
|
||||
|
||||
foreach (var replace in replaces)
|
||||
{
|
||||
replacements.Add(replace.From, replace.To);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(string from, string to)
|
||||
{
|
||||
|
@ -14,11 +36,13 @@
|
|||
{
|
||||
replacements.Add(from, to);
|
||||
}
|
||||
Save();
|
||||
}
|
||||
|
||||
public void Remove(string from)
|
||||
{
|
||||
replacements.Remove(from);
|
||||
Save();
|
||||
}
|
||||
|
||||
public string Apply(string msg)
|
||||
|
@ -30,5 +54,25 @@
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void Save()
|
||||
{
|
||||
ReplaceJson[] replaces = replacements.Select(pair =>
|
||||
{
|
||||
return new ReplaceJson
|
||||
{
|
||||
From = pair.Key,
|
||||
To = pair.Value
|
||||
};
|
||||
}).ToArray();
|
||||
|
||||
File.WriteAllText(file, JsonConvert.SerializeObject(replaces));
|
||||
}
|
||||
|
||||
private class ReplaceJson
|
||||
{
|
||||
public string From { get; set; } = string.Empty;
|
||||
public string To { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue