Skip to content

Commit e0cc462

Browse files
committed
Adds presistence to log replaces
1 parent c1ad97d commit e0cc462

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

Tools/BiblioTech/CommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private async Task Client_Ready()
7070
{
7171
var json = JsonConvert.SerializeObject(exception.Errors, Formatting.Indented);
7272
log.Error(json);
73-
throw exception;
73+
throw;
7474
}
7575
log.Log("Initialized.");
7676
}

Tools/BiblioTech/Program.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace BiblioTech
1111
public class Program
1212
{
1313
private DiscordSocketClient client = null!;
14-
private readonly CustomReplacement replacement = new CustomReplacement();
14+
private CustomReplacement replacement = null!;
1515

1616
public static Configuration Config { get; private set; } = null!;
1717
public static UserRepo UserRepo { get; } = new UserRepo();
@@ -42,6 +42,17 @@ public static Task Main(string[] args)
4242
public async Task MainAsync(string[] args)
4343
{
4444
Log.Log("Starting Codex Discord Bot...");
45+
try
46+
{
47+
replacement = new CustomReplacement(Config);
48+
replacement.Load();
49+
}
50+
catch (Exception ex)
51+
{
52+
Log.Error("Failed to load logReplacements: " + ex);
53+
throw;
54+
}
55+
4556
if (Config.DebugNoDiscord)
4657
{
4758
Log.Log("Debug option is set. Discord connection disabled!");

Tools/BiblioTech/Rewards/CustomReplacement.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1-
namespace BiblioTech.Rewards
1+
using Newtonsoft.Json;
2+
3+
namespace BiblioTech.Rewards
24
{
35
public class CustomReplacement
46
{
57
private readonly Dictionary<string, string> replacements = new Dictionary<string, string>();
8+
private readonly string file;
9+
10+
public CustomReplacement(Configuration config)
11+
{
12+
file = Path.Combine(config.DataPath, "logreplacements.json");
13+
}
14+
15+
public void Load()
16+
{
17+
replacements.Clear();
18+
if (!File.Exists(file)) return;
19+
20+
var replaces = JsonConvert.DeserializeObject<ReplaceJson[]>(File.ReadAllText(file));
21+
if (replaces == null) return;
22+
23+
foreach (var replace in replaces)
24+
{
25+
replacements.Add(replace.From, replace.To);
26+
}
27+
}
628

729
public void Add(string from, string to)
830
{
@@ -14,11 +36,13 @@ public void Add(string from, string to)
1436
{
1537
replacements.Add(from, to);
1638
}
39+
Save();
1740
}
1841

1942
public void Remove(string from)
2043
{
2144
replacements.Remove(from);
45+
Save();
2246
}
2347

2448
public string Apply(string msg)
@@ -30,5 +54,25 @@ public string Apply(string msg)
3054
}
3155
return result;
3256
}
57+
58+
private void Save()
59+
{
60+
ReplaceJson[] replaces = replacements.Select(pair =>
61+
{
62+
return new ReplaceJson
63+
{
64+
From = pair.Key,
65+
To = pair.Value
66+
};
67+
}).ToArray();
68+
69+
File.WriteAllText(file, JsonConvert.SerializeObject(replaces));
70+
}
71+
72+
private class ReplaceJson
73+
{
74+
public string From { get; set; } = string.Empty;
75+
public string To { get; set; } = string.Empty;
76+
}
3377
}
3478
}

0 commit comments

Comments
 (0)