From 1292e0f5855ab906ef5da843c272170c828b69fe Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Wed, 6 Jul 2022 13:38:44 +0300 Subject: [PATCH] Fixed some bugs and added version checking system. --- DiscordBot/DiscordBot.csproj | 1 + DiscordBot/Program.cs | 86 ++++++++++++++++++- PluginManager/Config.cs | 27 +++++- PluginManager/Items/ConsoleCommandsHandler.cs | 57 +++--------- PluginManager/Online/PluginsManager.cs | 4 +- 5 files changed, 122 insertions(+), 53 deletions(-) diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index a46dc9d..da11f94 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -8,6 +8,7 @@ False True + 1.0.0.1 diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 1cfcec1..a0e3468 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -69,7 +70,10 @@ public class Program var consoleCommandsHandler = new ConsoleCommandsHandler(discordbooter.client); if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp"); if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs"); + Config.SaveConfig(); + + while (true) { Console.ForegroundColor = ConsoleColor.White; @@ -86,11 +90,16 @@ public class Program { Console.Clear(); Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("Discord BOT for Cross Platform"); - Console.WriteLine("Created by: Wizzy\nDiscord: Wizzy#9181"); + + List startupMessageList = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/StartupMessage"); + + foreach (var message in startupMessageList) Console.WriteLine(message); + + Console.WriteLine($"Running on version: {Config.GetValue("Version") ?? System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}"); + Console.WriteLine($"Git URL: {Config.GetValue("GitURL") ?? " Could not find Git URL"}"); Console.ForegroundColor = ConsoleColor.White; - Console.WriteLine("============================ Discord BOT - Cross Platform ============================"); + Console.WriteLine($"============================ LOG ============================"); try { @@ -227,7 +236,76 @@ public class Program if (Config.GetValue("DeleteLogsAtStartup")) foreach (var file in Directory.GetFiles("./Output/Logs/")) File.Delete(file); + List OnlineDefaultKeys = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/SetupKeys"); - Config.Plugins.Load(); + Config.PluginConfig.Load(); + + if (!Config.ContainsKey("Version")) + Config.AddValueToVariables("Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), false); + else + Config.SetValue("Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); + + foreach (var key in OnlineDefaultKeys) + { + if (key.Length <= 3 || !key.Contains(' ')) continue; + string[] s = key.Split(' '); + try + { + Config.GetAndAddValueToVariable(s[0], s[1], s[2].Equals("true", StringComparison.CurrentCultureIgnoreCase)); + } + catch (Exception ex) + { + Functions.WriteErrFile(ex.Message); + } + } + + List onlineSettingsList = await ServerCom.ReadTextFromFile("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/OnlineData"); + foreach (var key in onlineSettingsList) + { + if (key.Length <= 3 || !key.Contains(' ')) continue; + + string[] s = key.Split(' '); + switch (s[0]) + { + case "CurrentVersion": + string newVersion = s[1]; + if (!newVersion.Equals(Config.GetValue("Version"))) + { + Console.WriteLine("A new version has been released on github page."); + Console.WriteLine("Download the new version using the following link wrote in yellow"); + Console_Utilities.WriteColorText("&y" + Config.GetValue("GitURL") + "&c"); + + Console.WriteLine(); + Console.WriteLine("Your product will work just fine on this outdated version, but an update is recommended.\n" + + "From now on, this version is no longer supported" + ); + Console_Utilities.WriteColorText("&rUse at your own risk&c"); + + Console_Utilities.WriteColorText("&mCurrent Version: " + Config.GetValue("Version") + "&c"); + Console_Utilities.WriteColorText("&gNew Version: " + newVersion + "&c"); + + Console.WriteLine("\n\n"); + await Task.Delay(1000); + + int waitTime = 20; //wait time to proceed + + Console.Write($"The bot will start in {waitTime} seconds"); + while (waitTime > 0) + { + await Task.Delay(1000); + waitTime--; + Console.SetCursorPosition("The bot will start in ".Length, Console.CursorTop); + Console.Write(" "); + Console.SetCursorPosition("The bot will start in ".Length, Console.CursorTop); + Console.Write(waitTime + " seconds"); + } + } + + break; + } + } + + + Config.SaveConfig(); } } diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 252fde1..6d326a6 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -5,6 +5,7 @@ using System.Text.Json; using System.Threading.Tasks; using System.Collections.Generic; using System.Threading; +using Newtonsoft.Json.Linq; namespace PluginManager { @@ -16,7 +17,7 @@ namespace PluginManager public static class Config { - public static class Plugins + public static class PluginConfig { public static List> InstalledPlugins = new(); @@ -81,11 +82,33 @@ namespace PluginManager if (appConfig!.ApplicationVariables!.ContainsKey(key)) return false; if (value == null) return false; appConfig.ApplicationVariables.Add(key, value); - if (isProtected) appConfig.ProtectedKeyWords!.Add(key); + if (isProtected && key != "Version") appConfig.ProtectedKeyWords!.Add(key); + SaveConfig(); return true; } + public static void GetAndAddValueToVariable(string key, string value, bool isReadOnly) + { + if (Config.ContainsKey(key)) return; + if (int.TryParse(value, out var intValue)) + Config.AddValueToVariables(key, intValue, isReadOnly); + else if (bool.TryParse(value, out var boolValue)) + Config.AddValueToVariables(key, boolValue, isReadOnly); + else if (float.TryParse(value, out var floatValue)) + Config.AddValueToVariables(key, floatValue, isReadOnly); + else if (double.TryParse(value, out var doubleValue)) + Config.AddValueToVariables(key, doubleValue, isReadOnly); + else if (uint.TryParse(value, out var uintValue)) + Config.AddValueToVariables(key, uintValue, isReadOnly); + else if (long.TryParse(value, out var longValue)) + Config.AddValueToVariables(key, longValue, isReadOnly); + else if (byte.TryParse(value, out var byteValue)) + Config.AddValueToVariables(key, byteValue, isReadOnly); + else + Config.AddValueToVariables(key, value, isReadOnly); + } + public static T? GetValue(string key) { if (!appConfig!.ApplicationVariables!.ContainsKey(key)) return default; diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index c2b9efd..7d35389 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -32,32 +32,24 @@ public class ConsoleCommandsHandler AddCommand("help", "Show help", "help ", args => { - if (args[1] == "lip") - { - foreach (var tuple in Config.Plugins.InstalledPlugins) - { - Console.WriteLine(tuple.Item1); - } - - return; - } - if (args.Length <= 1) { Console.WriteLine("Available commands:"); List items = new List(); - items.Add(new [] {"-", "-", "-"}); - items.Add(new [] {"Command", "Description", "Usage"}); - items.Add(new[] {" ", " ", "Argument type: [required]"}); - items.Add(new [] {"-", "-", "-"}); + items.Add(new[] { "-", "-", "-" }); + items.Add(new[] { "Command", "Description", "Usage" }); + items.Add(new[] { " ", " ", "Argument type: [required]" }); + items.Add(new[] { "-", "-", "-" }); foreach (var command in commandList) { var pa = from p in command.Action.Method.GetParameters() - where p.Name != null select p.ParameterType.FullName; + where p.Name != null + select p.ParameterType.FullName; items.Add(new[] { command.CommandName, command.Description, command.Usage }); } - items.Add(new [] {"-", "-", "-"}); + + items.Add(new[] { "-", "-", "-" }); Console_Utilities.FormatAndAlignTable(items); } else @@ -75,6 +67,7 @@ public class ConsoleCommandsHandler } ); + AddCommand("lp", "Load plugins", () => { if (pluginsLoaded) return; @@ -141,8 +134,8 @@ public class ConsoleCommandsHandler await ServerCom.DownloadFileAsync(info[1], path); if (info[0] == "Command" || info[0] == "Event") if (info[0] == "Event") - Config.Plugins.InstalledPlugins.Add(new(name, PluginType.Event)); - else if (info[0] == "Command") Config.Plugins.InstalledPlugins.Add(new(name, PluginType.Command)); + Config.PluginConfig.InstalledPlugins.Add(new(name, PluginType.Event)); + else if (info[0] == "Command") Config.PluginConfig.InstalledPlugins.Add(new(name, PluginType.Command)); Console.WriteLine("\n"); @@ -190,16 +183,6 @@ public class ConsoleCommandsHandler Console.WriteLine("\n"); File.Delete("./" + split[1]); } - - if (name == "DBUI") - { - Console.WriteLine("Reload with GUI ?[y/n]"); - if (Console.ReadKey().Key == ConsoleKey.Y) - { - Process.Start("./DiscordBotGUI.exe"); - Environment.Exit(0); - } - } } Console.WriteLine(); @@ -227,23 +210,7 @@ public class ConsoleCommandsHandler try { - if (Config.ContainsKey(key)) return; - if (int.TryParse(value, out var intValue)) - Config.AddValueToVariables(key, intValue, isReadOnly); - else if (bool.TryParse(value, out var boolValue)) - Config.AddValueToVariables(key, boolValue, isReadOnly); - else if (float.TryParse(value, out var floatValue)) - Config.AddValueToVariables(key, floatValue, isReadOnly); - else if (double.TryParse(value, out var doubleValue)) - Config.AddValueToVariables(key, doubleValue, isReadOnly); - else if (uint.TryParse(value, out var uintValue)) - Config.AddValueToVariables(key, uintValue, isReadOnly); - else if (long.TryParse(value, out var longValue)) - Config.AddValueToVariables(key, longValue, isReadOnly); - else if (byte.TryParse(value, out var byteValue)) - Config.AddValueToVariables(key, byteValue, isReadOnly); - else - Config.AddValueToVariables(key, value, isReadOnly); + Config.GetAndAddValueToVariable(key, value, isReadOnly); Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}"); } catch (Exception ex) diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 6870d3b..d3bf569 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -58,7 +58,7 @@ public class PluginsManager else display[3] = "1"; - if (Config.Plugins.Contains(content[0]) || Config.Plugins.Contains(content[0])) + if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0])) display[4] = "✓"; else display[4] = "X"; @@ -73,7 +73,7 @@ public class PluginsManager display[1] = content[1]; display[2] = content[2]; if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) display[3] = ((await ServerCom.ReadTextFromFile(content[5])).Count + 1).ToString(); - if (Config.Plugins.Contains(content[0]) || Config.Plugins.Contains(content[0])) + if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0])) display[4] = "✓"; else display[4] = "X";