From 1665d47a2560a276349773cda1c29038e6d20789 Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Thu, 15 Jun 2023 14:46:28 +0300 Subject: [PATCH] Fixed a bug with invalid token bot startup --- DiscordBot/Installer.cs | 31 ++++++++++++++++++++----------- PluginManager/Bot/Boot.cs | 5 +++-- PluginManager/Config.cs | 11 ++++++++++- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/DiscordBot/Installer.cs b/DiscordBot/Installer.cs index 85dd617..e378c35 100644 --- a/DiscordBot/Installer.cs +++ b/DiscordBot/Installer.cs @@ -18,19 +18,28 @@ namespace DiscordBot Console.WriteLine("Welcome to the SethBot installer !"); Console.WriteLine("First, we need to configure the bot. Don't worry, it will be quick !"); Console.WriteLine("The following information will be stored in the config.json file in the ./Data/Resources folder. You can change it later from there."); - Console.WriteLine("The bot tokn is required to run the bot. You can get it from the Discord Developer Portal. (https://discord.com/developers/applications)"); - Console.WriteLine("Please enter the bot token :"); - var token = Console.ReadLine(); + Console.WriteLine("The bot token is required to run the bot. You can get it from the Discord Developer Portal. (https://discord.com/developers/applications)"); - Console.WriteLine("Please enter the bot prefix :"); - var prefix = Console.ReadLine(); + if (!Config.Data.ContainsKey("token")) + { + Console.WriteLine("Please enter the bot token :"); + var token = Console.ReadLine(); + Config.Data.Add("token", token); + } - Console.WriteLine("Please enter the Server ID :"); - var serverId = Console.ReadLine(); + if (!Config.Data.ContainsKey("prefix")) + { + Console.WriteLine("Please enter the bot prefix :"); + var prefix = Console.ReadLine(); + Config.Data.Add("prefix", prefix); + } - Config.Data.Add("token", token); - Config.Data.Add("prefix", prefix); - Config.Data.Add("ServerID", serverId); + if (!Config.Data.ContainsKey("ServerID")) + { + Console.WriteLine("Please enter the Server ID :"); + var serverId = Console.ReadLine(); + Config.Data.Add("ServerID", serverId); + } Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO); @@ -114,4 +123,4 @@ namespace DiscordBot bar.Update(bar.Max); } } -} \ No newline at end of file +} diff --git a/PluginManager/Bot/Boot.cs b/PluginManager/Bot/Boot.cs index 676601c..66cb76e 100644 --- a/PluginManager/Bot/Boot.cs +++ b/PluginManager/Bot/Boot.cs @@ -1,5 +1,6 @@ using System.Net.Mime; using System; +using System.Collections.Generic; using System.Threading.Tasks; using Discord; @@ -68,7 +69,6 @@ public class Boot //Disable system clock checkup (for responses at slash commands) UseInteractionSnowflakeDate = false, - GatewayIntents = GatewayIntents.All }; @@ -110,6 +110,7 @@ public class Boot { Config.Data.Remove("token"); Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR); + Config.Data.Save(); await Task.Delay(4000); Environment.Exit(0); } @@ -153,4 +154,4 @@ public class Boot return Task.CompletedTask; } -} \ No newline at end of file +} diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 6fe794c..1ac8332 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -54,6 +54,14 @@ public class Config protected IDictionary _dictionary; private readonly string _file = ""; + /// + /// Empty constructor + /// + public Json() + { + _dictionary = new Dictionary(); + } + public Json(string file) { _dictionary = PrivateReadConfig(file).GetAwaiter().GetResult(); @@ -62,7 +70,8 @@ public class Config public async void Save() { - await Functions.SaveToJsonFile(_file, _dictionary); + if(!string.IsNullOrEmpty(_file)) + await Functions.SaveToJsonFile(_file, _dictionary); } public virtual void Add(TKey key, TValue value)