Fixed bug where bot crashes if the config is empty

This commit is contained in:
2024-06-03 19:52:39 +03:00
parent 83115d72a4
commit cdd426b03c
2 changed files with 6 additions and 2 deletions

View File

@@ -28,7 +28,7 @@ namespace DiscordBotCore
private static readonly string _MaxParallelDownloads = "3"; private static readonly string _MaxParallelDownloads = "3";
public string ServerID => ApplicationEnvironmentVariables["ServerID"]; public string ServerID => ApplicationEnvironmentVariables["ServerID"];
public string PluginDatabase => ApplicationEnvironmentVariables["PluginDatabase"]; public string PluginDatabase => ApplicationEnvironmentVariables["PluginDatabase"] ?? _PluginsDatabaseFile;
public string LogFile => $"{ApplicationEnvironmentVariables["LogFolder"]}/{DateTime.Now.ToLongDateString().Replace(" / ", "")}.log"; public string LogFile => $"{ApplicationEnvironmentVariables["LogFolder"]}/{DateTime.Now.ToLongDateString().Replace(" / ", "")}.log";
public string DataFolder => _ResourcesFolder; public string DataFolder => _ResourcesFolder;

View File

@@ -36,18 +36,22 @@ public class SettingsDictionary<TKey, TValue>
{ {
_Dictionary = new Dictionary<TKey, TValue>(); _Dictionary = new Dictionary<TKey, TValue>();
await SaveToFile(); await SaveToFile();
return true; return false;
} }
string fileAsText = await File.ReadAllTextAsync(_File); string fileAsText = await File.ReadAllTextAsync(_File);
if(string.IsNullOrEmpty(fileAsText) || string.IsNullOrWhiteSpace(fileAsText)) if(string.IsNullOrEmpty(fileAsText) || string.IsNullOrWhiteSpace(fileAsText))
{ {
_Dictionary = new Dictionary<TKey, TValue>(); _Dictionary = new Dictionary<TKey, TValue>();
await SaveToFile();
return false; return false;
} }
_Dictionary = await JsonManager.ConvertFromJson<IDictionary<TKey,TValue>>(fileAsText); _Dictionary = await JsonManager.ConvertFromJson<IDictionary<TKey,TValue>>(fileAsText);
if (_Dictionary.Keys.Count == 0)
return false;
return true; return true;
} }