From f2418d0395549c4bb06cb133ee5b384b62e96146 Mon Sep 17 00:00:00 2001 From: Tudor Andrei Date: Sat, 25 Mar 2023 11:51:48 +0200 Subject: [PATCH] fixed start error when no config file exists or is null --- PluginManager/Bot/CommandHandler.cs | 26 ++++++++++---------------- PluginManager/Config.cs | 22 +++++++++++++++------- SethDiscordBot.sln | 6 ------ 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/PluginManager/Bot/CommandHandler.cs b/PluginManager/Bot/CommandHandler.cs index 4eae29d..7882325 100644 --- a/PluginManager/Bot/CommandHandler.cs +++ b/PluginManager/Bot/CommandHandler.cs @@ -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; diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 462d948..9aeca0c 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -27,11 +27,12 @@ public static class Config Data = new Json("./Data/Resources/config.json"); Plugins = new Json("./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>(file); + try + { + var d = await Functions.ConvertFromJson>(file); + if (d is null) + throw new Exception("Failed to read config file"); + + return d; + }catch (Exception ex) + { + File.Delete(file); + return new Dictionary(); + } - if (d is null) - throw new Exception("Failed to read config file"); - - return d; } public bool Remove(TKey key) diff --git a/SethDiscordBot.sln b/SethDiscordBot.sln index 7f482dd..5a6a5b4 100644 --- a/SethDiscordBot.sln +++ b/SethDiscordBot.sln @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordBotWeb", "DiscordBotWeb\DiscordBotWeb.csproj", "{5500905A-E48F-4191-BB76-8610FA19F251}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE