From 92a18e3495bf84d784261c5eb21598efad1fcf81 Mon Sep 17 00:00:00 2001 From: Tudor Andrei Date: Thu, 7 Sep 2023 12:50:36 +0300 Subject: [PATCH] Removed URLs file from bot config --- DiscordBot/Bot/Actions/Help.cs | 4 +- DiscordBot/Bot/Actions/Plugin.cs | 14 +-- DiscordBot/Installer.cs | 20 ---- DiscordBot/Program.cs | 137 +++------------------- DiscordBot/Utilities/Console Utilities.cs | 5 +- PluginManager/Online/PluginsManager.cs | 9 ++ 6 files changed, 35 insertions(+), 154 deletions(-) diff --git a/DiscordBot/Bot/Actions/Help.cs b/DiscordBot/Bot/Actions/Help.cs index 849d2e3..f6e2ea5 100644 --- a/DiscordBot/Bot/Actions/Help.cs +++ b/DiscordBot/Bot/Actions/Help.cs @@ -33,7 +33,7 @@ public class Help : ICommandAction items.Add(new[] { "-", "-", "-" }); - Utilities.Utilities.FormatAndAlignTable(items, + ConsoleUtilities.FormatAndAlignTable(items, TableFormat.CENTER_EACH_COLUMN_BASED ); return; @@ -55,7 +55,7 @@ public class Help : ICommandAction new[] { "-", "-", "-" } }; - Utilities.Utilities.FormatAndAlignTable(actionData, + ConsoleUtilities.FormatAndAlignTable(actionData, TableFormat.CENTER_EACH_COLUMN_BASED ); } diff --git a/DiscordBot/Bot/Actions/Plugin.cs b/DiscordBot/Bot/Actions/Plugin.cs index 06d99fb..ad51b87 100644 --- a/DiscordBot/Bot/Actions/Plugin.cs +++ b/DiscordBot/Bot/Actions/Plugin.cs @@ -30,12 +30,12 @@ public class Plugin : ICommandAction return; } + + var manager = new PluginsManager(); switch ( args[0] ) { case "list": - var manager = - new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]); var data = await manager.GetAvailablePlugins(); var items = new List @@ -49,7 +49,7 @@ public class Plugin : ICommandAction items.Add(new[] { "-", "-", "-", "-" }); - Utilities.Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT); + ConsoleUtilities.FormatAndAlignTable(items, TableFormat.DEFAULT); break; @@ -139,10 +139,8 @@ public class Plugin : ICommandAction break; } } - - var pluginManager = - new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]); - var pluginData = await pluginManager.GetPluginLinkByName(pluginName); + + var pluginData = await manager.GetPluginLinkByName(pluginName); if (pluginData == null || pluginData.Length == 0) { Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again."); @@ -156,7 +154,7 @@ public class Plugin : ICommandAction Console.WriteLine("Downloading plugin..."); //download plugin progress bar for linux and windows terminals - var spinner = new Utilities.Utilities.Spinner(); + var spinner = new ConsoleUtilities.Spinner(); spinner.Start(); IProgress progress = new Progress(p => { spinner.Message = $"Downloading {pluginName}... {Math.Round(p, 2)}% "; }); await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress); diff --git a/DiscordBot/Installer.cs b/DiscordBot/Installer.cs index 2d8eeb8..02bb91c 100644 --- a/DiscordBot/Installer.cs +++ b/DiscordBot/Installer.cs @@ -42,24 +42,4 @@ public static class Installer Console.WriteLine("Config saved !"); } - - public static async Task SetupPluginDatabase() - { - Console.WriteLine("The plugin database is required to run the bot but there is nothing configured yet."); - Console.WriteLine("Downloading the default database..."); - await DownloadPluginDatabase(); - - } - - private static async Task DownloadPluginDatabase( - string url = "https://raw.githubusercontent.com/andreitdr/SethDiscordBot/gh-pages/defaultURLs.json") - { - var path = "./Data/Resources/URLs.json"; - - Directory.CreateDirectory("./Data/Resources"); - var spinner = new Utilities.Utilities.Spinner(); - spinner.Start(); - await ServerCom.DownloadFileAsync(url, path, null); - spinner.Stop(); - } } diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index ee062dc..32fae9c 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; +using DiscordBot.Utilities; using PluginManager.Bot; using PluginManager.Online; using PluginManager.Online.Helpers; @@ -15,8 +16,7 @@ namespace DiscordBot; public class Program { - public static SettingsDictionary URLs; - public static InternalActionManager internalActionManager; + public static InternalActionManager internalActionManager; /// /// The main entry point for the application. @@ -34,7 +34,7 @@ public class Program args.Length == 1 && args[0] == "/reset") Installer.GenerateStartupConfig(); - HandleInput(args.ToList()).Wait(); + HandleInput().Wait(); } /// @@ -42,13 +42,11 @@ public class Program /// private static void NoGUI() { -#if DEBUG - Console.WriteLine("Debug mode enabled"); internalActionManager.Initialize().Wait(); - internalActionManager.Execute("plugin", "load").Wait(); // Load plugins at startup + internalActionManager.Execute("plugin", "load").Wait(); internalActionManager.Refresh().Wait(); -#endif - + + while (true) { var cmd = Console.ReadLine(); @@ -66,56 +64,30 @@ public class Program /// Start the bot without user interface /// /// Returns the boot loader for the Discord Bot - private static async Task StartNoGui() + private static async Task StartNoGui() { Console.Clear(); Console.ForegroundColor = ConsoleColor.DarkYellow; - var startupMessageList = - await ServerCom.ReadTextFromURL(URLs["StartupMessage"]); + Console.WriteLine($"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}"); + Console.WriteLine("Git URL: https://github.com/andreitdr/SethDiscordBot"); - foreach (var message in startupMessageList) - Console.WriteLine(message); + ConsoleUtilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&yexit&r) or some settings won't be saved"); - Console.WriteLine( - $"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}" - ); - Console.WriteLine($"Git URL: {AppSettings["GitURL"]}"); - - Utilities.Utilities.WriteColorText( - "&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n" - ); - Console.ForegroundColor = ConsoleColor.White; - - if (AppSettings.ContainsKey("LaunchMessage")) - Utilities.Utilities.WriteColorText(AppSettings["LaunchMessage"]); - - - Utilities.Utilities.WriteColorText( - "Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)" - ); - - Console.WriteLine("Running on " + Functions.GetOperatingSystem()); + ConsoleUtilities.WriteColorText($"Running on &m{Functions.GetOperatingSystem()}"); Console.WriteLine("============================ LOG ============================"); - + + Console.ForegroundColor = ConsoleColor.White; try { - var token = ""; -#if DEBUG - if (File.Exists("./Data/Resources/token.txt")) token = File.ReadAllText("./Data/Resources/token.txt"); - else token = AppSettings["token"]; -#else - token = AppSettings["token"]; -#endif + var token = AppSettings["token"]; var prefix = AppSettings["prefix"]; var discordbooter = new Boot(token, prefix); await discordbooter.Awake(); - return discordbooter; } catch ( Exception ex ) { Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR); - return null; } } @@ -123,22 +95,9 @@ public class Program /// Handle user input arguments from the startup of the application /// /// The arguments - private static async Task HandleInput(List args) + private static async Task HandleInput() { - Console.WriteLine("Loading Core ..."); - - //Handle arguments here: - - if (args.Contains("--gui")) - { - // GUI not implemented yet - Console.WriteLine("GUI not implemented yet"); - return; - } - - // Starting bot after all arguments are handled - - var b = await StartNoGui(); + await StartNoGui(); try { internalActionManager = new InternalActionManager("./Data/Plugins", "*.dll"); @@ -178,70 +137,6 @@ public class Program Console.WriteLine($"[{type.ToString()}] {message}"); Console.ResetColor(); }; - - if (!Directory.Exists("./Data/Resources") || !File.Exists("./Data/Resources/URLs.json")) - await Installer.SetupPluginDatabase(); - - - URLs = new SettingsDictionary("./Data/Resources/URLs.json"); - - - Console.WriteLine("Loading resources ..."); - - if (AppSettings.ContainsKey("DeleteLogsAtStartup")) - if (AppSettings["DeleteLogsAtStartup"] == "true") - foreach (var file in Directory.GetFiles("./Output/Logs/")) - File.Delete(file); - - var OnlineDefaultKeys = await ServerCom.ReadTextFromURL(URLs["SetupKeys"]); - - AppSettings["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString(); - - foreach (var key in OnlineDefaultKeys) - { - if (key.Length <= 3 || !key.Contains(' ')) continue; - var s = key.Split(' '); - try - { - AppSettings[s[0]] = s[1]; - } - catch ( Exception ex ) - { - Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR); - } - } - - var onlineSettingsList = await ServerCom.ReadTextFromURL(URLs["Versions"]); - foreach (var key in onlineSettingsList) - { - if (key.Length <= 3 || !key.Contains(' ')) continue; - - var s = key.Split(' '); - switch ( s[0] ) - { - case "CurrentVersion": - var currentVersion = AppSettings["Version"]; - var newVersion = s[1]; - if (new VersionString(newVersion) != new VersionString(newVersion)) - { - Console.WriteLine("A new updated was found. Check the changelog for more information."); - var changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]); - foreach (var item in changeLog) - Utilities.Utilities.WriteColorText(item); - Console.WriteLine("Current version: " + currentVersion); - Console.WriteLine("Latest version: " + newVersion); - - Console.WriteLine("Download from here: https://github.com/andreitdr/SethDiscordBot/releases"); - - Console.WriteLine("Press any key to continue ..."); - Console.ReadKey(); - } - - break; - } - } - - Console.Clear(); } } diff --git a/DiscordBot/Utilities/Console Utilities.cs b/DiscordBot/Utilities/Console Utilities.cs index 124a9d4..ce8d074 100644 --- a/DiscordBot/Utilities/Console Utilities.cs +++ b/DiscordBot/Utilities/Console Utilities.cs @@ -4,7 +4,7 @@ using System.Threading; namespace DiscordBot.Utilities; -public static class Utilities +public static class ConsoleUtilities { private static readonly Dictionary Colors = new() { @@ -16,8 +16,7 @@ public static class Utilities }; private static readonly char ColorPrefix = '&'; - - + private static bool CanAproximateTo(this float f, float y) { return MathF.Abs(f - y) < 0.000001; diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index a82595b..7848d1d 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -19,6 +19,15 @@ public class PluginsManager PluginsLink = plink; VersionsLink = vlink; } + + /// + /// The default Plugin Manager constructor. It uses the default links. + /// + public PluginsManager() + { + PluginsLink = "https://raw.githubusercontent.com/andreitdr/installer/discord-bot-files/Plugins.txt"; + VersionsLink = "https://raw.githubusercontent.com/andreitdr/installer/discord-bot-files/Versions"; + } /// /// The URL of the server