fixed start error when no config file exists or is null

This commit is contained in:
2023-03-25 11:51:48 +02:00
parent 460a85944a
commit f2418d0395
3 changed files with 25 additions and 29 deletions

View File

@@ -108,15 +108,11 @@ internal class CommandHandler
string mentionPrefix = "<@" + client.CurrentUser.Id + ">";
plugin = PluginLoader.Commands!
.Where
(
plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
(
plug.Aliases is not null &&
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
)
)
.FirstOrDefault();
.FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
(
plug.Aliases is not null &&
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
));
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
}
@@ -124,16 +120,14 @@ internal class CommandHandler
else
{
plugin = PluginLoader.Commands!
.Where(
p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
(p.Aliases is not null &&
p.Aliases.Contains(
message.Content.Split(' ')[0].Substring(botPrefix.Length))))
.FirstOrDefault();
.FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
(p.Aliases is not null &&
p.Aliases.Contains(
message.Content.Split(' ')[0].Substring(botPrefix.Length))));
cleanMessage = message.Content.Substring(botPrefix.Length);
}
if (plugin is null)
throw new Exception($"Failed to run command ! " + message.CleanContent);
throw new Exception($"Failed to run command ! " + message.CleanContent + " (user: " + context.Message.Author.Username + " - " + context.Message.Author.Id + ")");
if (plugin.requireAdmin && !context.Message.Author.isAdmin())
return;

View File

@@ -27,11 +27,12 @@ public static class Config
Data = new Json<string, string>("./Data/Resources/config.json");
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
IsLoaded = true;
Logger.Initialize(isConsole);
ArchiveManager.Initialize();
IsLoaded = true;
if (isConsole)
Logger.LogEvent += (message) => { Console.Write(message); };
}
@@ -103,12 +104,19 @@ public static class Config
return dictionary;
}
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
try
{
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
if (d is null)
throw new Exception("Failed to read config file");
return d;
}catch (Exception ex)
{
File.Delete(file);
return new Dictionary<TKey, TValue>();
}
if (d is null)
throw new Exception("Failed to read config file");
return d;
}
public bool Remove(TKey key)