Added fileName properties/fields to all classes that have an accompanying JSON file

This commit is contained in:
Chris Darnell committed 2021-05-01 00:52:05 -07:00
1 parent cf036c534f
commit c55c8c061e
3 files changed
+20 -18

No files matched your search

+4 -3
View File
@@ -17,6 +17,7 @@ namespace StarsiegeBot
class BotSettings : BaseCommandModule
{
public static Dictionary<string, GuildSettings> GuildSettings;
private readonly string fileName = "guildSettings.json";
public BotSettings()
{
Console.WriteLine("Bot Setting Commands Loaded");
@@ -27,7 +28,7 @@ namespace StarsiegeBot
return;
}
// Check for guild settings file. If it doesnt exist, create it.
if (!File.Exists("guildSettings.json"))
if (!File.Exists(fileName))
{
// Since we need to creat it, we're going to set some default stuff.
Console.Write("Guild Settings Config File Not Found. Creating One...");
@@ -54,7 +55,7 @@ namespace StarsiegeBot
// Now that we have the initial stuff in memory, write it to file.
string output = JsonConvert.SerializeObject(GuildSettings);
File.WriteAllTextAsync("guildSettings.json", output);
File.WriteAllTextAsync(fileName, output);
// in form the console reader that we're done making the initial config.
Console.WriteLine(" Done!");
}
@@ -62,7 +63,7 @@ namespace StarsiegeBot
{
// load the config file to memory.
var json = "";
using (var fs = File.OpenRead("guildSettings.json"))
using (var fs = File.OpenRead(fileName))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = sr.ReadToEnd();
GuildSettings = JsonConvert.DeserializeObject<Dictionary<string, GuildSettings>>(json);
+9 -8
View File
@@ -21,17 +21,18 @@ namespace StarsiegeBot
{
private DeathMessageLines dmLines;
private bool DeathMessagesEnabled;
private static readonly string fileName = "DeathMessages.json";
public DeathMessages()
{
Console.WriteLine("Death Message Commands Loaded.");
// If the DeathMessages file exists, load it. And enable the commands.
// Otherwise disable all the commands.
if (File.Exists("DeathMessages.json"))
if (File.Exists(fileName))
{
// set some JSON text to blank.
var json = "";
using (var fs = File.OpenRead("DeathMessages.json"))
using (var fs = File.OpenRead(fileName))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = sr.ReadToEnd();
// Decode the JSON and set it to dmLines.
@@ -42,7 +43,7 @@ namespace StarsiegeBot
else
{
// Echo out that we're missing a file. Disable the commands due to that.
Console.WriteLine(" --- --- --- --- DeathMessages.json is missing");
Console.WriteLine($" --- --- --- --- {fileName} is missing");
DeathMessagesEnabled = false;
}
}
@@ -52,11 +53,11 @@ namespace StarsiegeBot
public async Task LoadDeathMessages(CommandContext ctx)
{
await ctx.TriggerTypingAsync();
if (File.Exists("DeathMessages.json"))
if (File.Exists(fileName))
{
// set some JSON text to blank.
var json = "";
using (var fs = File.OpenRead("DeathMessages.json"))
using (var fs = File.OpenRead(fileName))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = sr.ReadToEnd();
// Decode the JSON and set it to dmLines.
@@ -86,7 +87,7 @@ namespace StarsiegeBot
string[] turnOn = { "on", "true", "1" };
string[] turnOff = { "off", "false", "0" };
if (File.Exists("DeathMessages.json"))
if (File.Exists(fileName))
{
if (turnOn.Contains(isEnabled))
{
@@ -105,7 +106,7 @@ namespace StarsiegeBot
else
{
DeathMessagesEnabled = false;
output = "DeathMessages.json file is missing, and it can not be enabled.";
output = $"{fileName} file is missing, and it can not be enabled.";
}
await ctx.RespondAsync(output);
}
@@ -269,7 +270,7 @@ namespace StarsiegeBot
private async Task StoreDeathMessages()
{
string output = JsonConvert.SerializeObject(dmLines);
await File.WriteAllTextAsync("DeathMessages.json", output);
await File.WriteAllTextAsync(fileName, output);
return;
}
}
+7 -7
View File
@@ -18,17 +18,17 @@ namespace StarsiegeBot
{
private Dictionary<string, SSFunction> ssFunctions;
private bool FunctionsEnabled;
private static readonly string fileName = "functions.json";
public Functions()
{
Console.WriteLine("Starsiege Function Commands Loaded");
// Check for file, if not there, disable commands.
if (File.Exists("functions.json"))
if (File.Exists(fileName))
{
// Load the Functions JSON file. Has all information regarding Starsiege in-game Functions.
var json = "";
using (var fs = File.OpenRead("functions.json"))
using (var fs = File.OpenRead(fileName))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = sr.ReadToEnd();
ssFunctions = JsonConvert.DeserializeObject<Dictionary<string, SSFunction>>(json);
@@ -135,7 +135,7 @@ namespace StarsiegeBot
string[] turnOn = { "on", "true", "1" };
string[] turnOff = { "off", "false", "0" };
if (File.Exists("functions.json"))
if (File.Exists(fileName))
{
if (turnOn.Contains(isEnabled))
{
@@ -154,7 +154,7 @@ namespace StarsiegeBot
else
{
FunctionsEnabled = false;
output = "Functions.json file is missing, and it can not be enabled.";
output = $"{fileName} file is missing, and it can not be enabled.";
}
await ctx.RespondAsync(output);
}
@@ -164,11 +164,11 @@ namespace StarsiegeBot
await ctx.TriggerTypingAsync();
// Check for file, if not there, disable commands.
if (File.Exists("functions.json"))
if (File.Exists(fileName))
{
// Load the Functions JSON file. Has all information regarding Starsiege in-game Functions.
var json = "";
using (var fs = File.OpenRead("functions.json"))
using (var fs = File.OpenRead(fileName))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = sr.ReadToEnd();
ssFunctions = JsonConvert.DeserializeObject<Dictionary<string, SSFunction>>(json);