From 21f1975fbc76866ca69ab6b10819dceeac84bbb6 Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Sun, 9 Oct 2022 20:59:16 +0300 Subject: [PATCH] --- DiscordBot/Program.cs | 161 ++++-------------- PluginManager/Config.cs | 26 ++- PluginManager/Online/Helpers/VersionString.cs | 7 +- 3 files changed, 59 insertions(+), 135 deletions(-) diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 6e40c0d..6b0d43a 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -1,20 +1,19 @@ -using System; +using DiscordBot.Discord.Core; + +using PluginManager; +using PluginManager.Items; +using PluginManager.Online; +using PluginManager.Online.Helpers; +using PluginManager.Others; + +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; -using DiscordBot.Discord.Core; - -using PluginManager; - -using PluginManager.Items; -using PluginManager.Online; -using PluginManager.Others; - using Terminal.Gui; namespace DiscordBot; @@ -36,14 +35,10 @@ public class Program Console.WriteLine("Loading resources ..."); PreLoadComponents().Wait(); - - if (!Config.ContainsKey("ServerID") || (!Config.ContainsKey("token") || Config.GetValue("token") == null || (Config.GetValue("token")?.Length != 70 && Config.GetValue("token")?.Length != 59)) || (!Config.ContainsKey("prefix") || Config.GetValue("prefix") == null || Config.GetValue("prefix")?.Length != 1) || (args.Length > 0 && args[0] == "/newconfig")) { Application.Init(); var top = Application.Top; - - //Application.IsMouseDisabled = true; var win = new Window("Discord Bot Config - " + Assembly.GetExecutingAssembly().GetName().Version) { X = 0, @@ -282,36 +277,8 @@ public class Program /// The arguments private static async Task HandleInput(string[] args) { - var len = args.Length; - if (len == 3 && args[0] == "/download") - { - var url = args[1]; - var location = args[2]; - - await ServerCom.DownloadFileAsync(url, location); - - return; - } - - if (len > 0 && (args.Contains("--cmd") || args.Contains("--args") || args.Contains("--nomessage"))) - { - if (args.Contains("lp") || args.Contains("loadplugins")) - loadPluginsOnStartup = true; - if (args.Contains("listplugs")) - listPluginsAtStartup = true; - - len = 0; - } - - if (len == 2 && args[0] == "/procKill") - { - Process.GetProcessById(int.Parse(args[1])).Kill(); - len = 0; - } - - var b = await StartNoGUI(); consoleCommandsHandler = new ConsoleCommandsHandler(b.client); @@ -322,91 +289,28 @@ public class Program Console.WriteLine("Starting to remove " + plugName); await ConsoleCommandsHandler.ExecuteCommad("remplug " + plugName); loadPluginsOnStartup = true; - len = 0; } - if (len > 0 && args[0] == "/updateplug") + Thread mainThread = new Thread(() => { - string plugName = args.MergeStrings(1); - Console.WriteLine("Updating " + plugName); - await ConsoleCommandsHandler.ExecuteCommad("dwplug" + plugName); - return; - } - - if (len == 0 || (args[0] != "--exec" && args[0] != "--execute")) - { - - Thread mainThread = new Thread(() => + try { - try - { - NoGUI(b); - } - catch (IOException ex) - { - if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9) - { - if (!Config.ContainsKey("LaunchMessage")) - Config.AddValueToVariables("LaunchMessage", "An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", false); - Functions.WriteErrFile(ex.ToString()); - } - } - - - - }); - mainThread.Start(); - return; - } - - - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("Execute command interface noGUI\n\n"); - Console.WriteLine( - "\tCommand name\t\t\t\tDescription\n" + - "-- help | -help\t\t ------ \tDisplay the help message\n" + - "--reset-full\t\t ------ \tReset all files (clear files)\n" + - "--reset-logs\t\t ------ \tClear up the output folder\n" + - "--start\t\t ------ \tStart the bot\n" + - "exit\t\t\t ------ \tClose the application" - ); - while (true) - { - Console.ForegroundColor = ConsoleColor.White; - Console.Write("> "); - var message = Console.ReadLine().Split(' '); - - switch (message[0]) - { - case "--help": - case "-help": - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("\tCommand name\t\t\t\tDescription\n" + "-- help | -help\t\t ------ \tDisplay the help message\n" + "--reset-full\t\t ------ \tReset all files (clear files)\n" + "--reset-settings\t ------ \tReset only bot settings\n" + "--reset-logs\t\t ------ \tClear up the output folder\n" + "--start\t\t ------ \tStart the bot\n" + "exit\t\t\t ------ \tClose the application"); - break; - case "--reset-full": - await ClearFolder("./Data/Resources/"); - await ClearFolder("./Output/Logs/"); - await ClearFolder("./Output/Errors"); - await ClearFolder("./Data/Languages/"); - await ClearFolder("./Data/Plugins/Commands"); - await ClearFolder("./Data/Plugins/Events"); - Console.WriteLine("Successfully cleared all folders"); - break; - case "--reset-logs": - await ClearFolder("./Output/Logs"); - await ClearFolder("./Output/Errors"); - Console.WriteLine("Successfully clear logs folder"); - break; - case "--exit": - case "exit": - Environment.Exit(0); - break; - - default: - Console.WriteLine("Failed to execute command " + message[0]); - break; + NoGUI(b); } - } + catch (IOException ex) + { + if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9) + { + if (!Config.ContainsKey("LaunchMessage")) + Config.AddValueToVariables("LaunchMessage", "An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", false); + Functions.WriteErrFile(ex.ToString()); + } + } + + + + }); + mainThread.Start(); } private static async Task PreLoadComponents() @@ -462,12 +366,19 @@ public class Program string newVersion = s[1]; if (!newVersion.Equals(Config.GetValue("Version"))) { + + VersionString nVer = new VersionString(newVersion.Substring(2)); + VersionString cVer = new VersionString(Config.GetValue("Version").Substring(2)); + if (cVer > nVer) + { + Config.SetValue("Version", "1." + cVer.ToShortString() + " (Beta)"); + break; + } + if (Functions.GetOperatingSystem() == PluginManager.Others.OperatingSystem.WINDOWS) { string url = $"https://github.com/Wizzy69/SethDiscordBot/releases/download/v{newVersion}/net6.0.zip"; - //string url2 = $"https://github.com/Wizzy69/SethDiscordBot/releases/download/v{newVersion}-preview/net6.0.zip"; - Process.Start(".\\Updater\\Updater.exe", $"{newVersion} {url} {Process.GetCurrentProcess().ProcessName}"); } @@ -486,7 +397,7 @@ public class Program break; case "UpdaterVersion": string updaternewversion = s[1]; - if (Config.UpdaterVersion != updaternewversion && Functions.GetOperatingSystem() == PluginManager.Others.OperatingSystem.WINDOWS) + if ((Config.UpdaterVersion != updaternewversion && Functions.GetOperatingSystem() == PluginManager.Others.OperatingSystem.WINDOWS) || !Directory.Exists("./Updater") || !File.Exists("./Updater/Updater.exe")) { Console.Clear(); Console.WriteLine("Installing updater ...\nDo NOT close the bot during update !"); diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 60ddd20..eef58ea 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -1,10 +1,11 @@ -using System; -using PluginManager.Others; +using PluginManager.Others; + +using System; +using System.Collections.Generic; using System.IO; using System.Text.Json; -using System.Threading.Tasks; -using System.Collections.Generic; using System.Threading; +using System.Threading.Tasks; namespace PluginManager { @@ -227,6 +228,23 @@ namespace PluginManager Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables!.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords!.Count} readonly variables."); return; } + else if (File.Exists(Functions.dataFolder + "config.json.bak")) + { + try + { + + + Console.WriteLine("An error occured while loading the settings. Importing from backup file..."); + path = Functions.dataFolder + "config.json.bak"; + appConfig = await Functions.ConvertFromJson(path); + + return; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } appConfig = new() { ApplicationVariables = new Dictionary(), ProtectedKeyWords = new List(), PluginVersions = new Dictionary(), UpdaterVersion = "-1" }; } diff --git a/PluginManager/Online/Helpers/VersionString.cs b/PluginManager/Online/Helpers/VersionString.cs index 526528d..31c3993 100644 --- a/PluginManager/Online/Helpers/VersionString.cs +++ b/PluginManager/Online/Helpers/VersionString.cs @@ -1,9 +1,4 @@ -using PluginManager.Others; - -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Linq; +using System; namespace PluginManager.Online.Helpers {