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 + ">"; string mentionPrefix = "<@" + client.CurrentUser.Id + ">";
plugin = PluginLoader.Commands! plugin = PluginLoader.Commands!
.Where .FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
( (
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])
plug.Aliases is not null && ));
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
)
)
.FirstOrDefault();
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1); cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
} }
@@ -124,16 +120,14 @@ internal class CommandHandler
else else
{ {
plugin = PluginLoader.Commands! plugin = PluginLoader.Commands!
.Where( .FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) || (p.Aliases is not null &&
(p.Aliases is not null && p.Aliases.Contains(
p.Aliases.Contains( message.Content.Split(' ')[0].Substring(botPrefix.Length))));
message.Content.Split(' ')[0].Substring(botPrefix.Length))))
.FirstOrDefault();
cleanMessage = message.Content.Substring(botPrefix.Length); cleanMessage = message.Content.Substring(botPrefix.Length);
} }
if (plugin is null) 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()) if (plugin.requireAdmin && !context.Message.Author.isAdmin())
return; return;

View File

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

View File

@@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordBotWeb", "DiscordBotWeb\DiscordBotWeb.csproj", "{5500905A-E48F-4191-BB76-8610FA19F251}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -23,10 +21,6 @@ Global
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU {EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU
{5500905A-E48F-4191-BB76-8610FA19F251}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5500905A-E48F-4191-BB76-8610FA19F251}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5500905A-E48F-4191-BB76-8610FA19F251}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5500905A-E48F-4191-BB76-8610FA19F251}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE