From 89c4932cd719342e92ff7a444989bdb5c3bbb85a Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Mon, 25 Sep 2023 22:06:42 +0300 Subject: [PATCH] Fixed plugin refresh command and added new method for executing tasks without return type in ConsoleUtilities --- DiscordBot/Bot/Actions/Clear.cs | 1 - DiscordBot/Bot/Actions/Plugin.cs | 25 +++++---- DiscordBot/Entry.cs | 4 +- DiscordBot/Installer.cs | 14 ++--- DiscordBot/Program.cs | 26 ++++------ DiscordBot/Utilities/Console Utilities.cs | 51 ++++++++++++++----- PluginManager/Config.cs | 12 ++--- .../Others/Actions/InternalActionsManager.cs | 2 +- 8 files changed, 78 insertions(+), 57 deletions(-) diff --git a/DiscordBot/Bot/Actions/Clear.cs b/DiscordBot/Bot/Actions/Clear.cs index 5d81ffb..a4bf0e3 100644 --- a/DiscordBot/Bot/Actions/Clear.cs +++ b/DiscordBot/Bot/Actions/Clear.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using PluginManager; using PluginManager.Interfaces; using PluginManager.Others; diff --git a/DiscordBot/Bot/Actions/Plugin.cs b/DiscordBot/Bot/Actions/Plugin.cs index bbfe0ad..c577711 100644 --- a/DiscordBot/Bot/Actions/Plugin.cs +++ b/DiscordBot/Bot/Actions/Plugin.cs @@ -40,25 +40,33 @@ public class Plugin : ICommandAction switch (args[0]) { case "refresh": - await Program.internalActionManager.Refresh(); + await RefreshPlugins(true); break; + case "list": var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetAvailablePlugins(), "Loading plugins..."); TableData tableData = new(new List { "Name", "Description", "Type", "Version" }); foreach (var plugin in data) tableData.AddRow(plugin); + tableData.HasRoundBorders = false; tableData.PrintAsTable(); - break; case "load": if (pluginsLoaded) break; + if (Config.DiscordBot is null) + { + AnsiConsole.MarkupLine("[red]Discord bot not initialized[/]"); + break; + } + var loader = new PluginLoader(Config.DiscordBot.client); if (args.Length == 2 && args[1] == "-q") { + Console.WriteLine("Loading plugins in quiet mode..."); loader.LoadPlugins(); pluginsLoaded = true; break; @@ -152,13 +160,10 @@ public class Plugin : ICommandAction } } - private async Task RefreshPlugins() + private async Task RefreshPlugins(bool quiet) { - Console.WriteLine("Reloading plugins list..."); - await Program.internalActionManager.Execute("plugin", "load"); + await Program.internalActionManager.Execute("plugin", "load", quiet ? "-q" : string.Empty); await Program.internalActionManager.Refresh(); - - Console.WriteLine("Finished reloading plugins list"); } @@ -199,7 +204,7 @@ public class Plugin : ICommandAction if (pluginRequirements == string.Empty) { Console.WriteLine("Finished installing " + pluginName + " successfully"); - await RefreshPlugins(); + await RefreshPlugins(false); return; } @@ -255,7 +260,7 @@ public class Plugin : ICommandAction } }); - - await RefreshPlugins(); + + await RefreshPlugins(false); } } \ No newline at end of file diff --git a/DiscordBot/Entry.cs b/DiscordBot/Entry.cs index 2834a51..f30a642 100644 --- a/DiscordBot/Entry.cs +++ b/DiscordBot/Entry.cs @@ -25,9 +25,7 @@ public class Entry static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args) { - var folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), - "./Libraries" - ); + var folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "./Libraries"); var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll"); if (!File.Exists(assemblyPath)) return null; var assembly = Assembly.LoadFrom(assemblyPath); diff --git a/DiscordBot/Installer.cs b/DiscordBot/Installer.cs index be92fae..0111a38 100644 --- a/DiscordBot/Installer.cs +++ b/DiscordBot/Installer.cs @@ -1,4 +1,3 @@ -using System; using PluginManager; using Spectre.Console; @@ -8,13 +7,14 @@ public static class Installer { public static void GenerateStartupConfig() { - AnsiConsole.WriteLine("Welcome to the [bold]SethBot[/] installer !"); - AnsiConsole.WriteLine("First, we need to configure the bot. Don't worry, it will be quick !"); - - var token = AnsiConsole.Ask("Please enter the bot token :"); - var prefix = AnsiConsole.Ask("Please enter the bot prefix :"); - var serverId = AnsiConsole.Ask("Please enter the Server ID :"); + AnsiConsole.MarkupLine("Welcome to the [bold]SethBot[/] installer !"); + AnsiConsole.MarkupLine("First, we need to configure the bot. Don't worry, it will be quick !"); + var token = AnsiConsole.Ask("Please enter the bot [yellow]token[/]:"); + var prefix = AnsiConsole.Ask("Please enter the bot [yellow]prefix[/]:"); + var serverId = AnsiConsole.Ask("Please enter the [yellow]Server ID[/]:"); + + if (string.IsNullOrWhiteSpace(serverId)) serverId = "NULL"; Config.AppSettings.Add("token", token); Config.AppSettings.Add("prefix", prefix); Config.AppSettings.Add("ServerID", serverId); diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 3035402..17cc867 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -22,13 +22,8 @@ public class Program public static void Startup(string[] args) { PreLoadComponents(args).Wait(); - - if (!AppSettings.ContainsKey("ServerID") || !AppSettings.ContainsKey("token") || - AppSettings["token"] == null || - AppSettings["token"]?.Length != 70 && AppSettings["token"]?.Length != 59 || - !AppSettings.ContainsKey("prefix") || AppSettings["prefix"] == null || - AppSettings["prefix"]?.Length != 1 || - args.Length == 1 && args[0] == "/reset") + + if (!AppSettings.ContainsKey("ServerID") || !AppSettings.ContainsKey("token") || !AppSettings.ContainsKey("prefix")) Installer.GenerateStartupConfig(); HandleInput().Wait(); @@ -67,8 +62,9 @@ public class Program Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}"); - Console.WriteLine("Git URL: https://github.com/andreitdr/SethDiscordBot"); - + Console.WriteLine("Git SethBot: https://github.com/andreitdr/SethDiscordBot"); + Console.WriteLine("Git Plugins: https://github.com/andreitdr/SethPlugins"); + ConsoleUtilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&yexit&r) or some settings won't be saved"); ConsoleUtilities.WriteColorText($"Running on &m{Functions.GetOperatingSystem()}"); @@ -106,12 +102,12 @@ public class Program { if (AppSettings.ContainsKey("LaunchMessage")) AppSettings.Add("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 !" - ); - Logger - .Log("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 !", - "Bot", LogLevel.ERROR - ); + "An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" + + "There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !"); + + Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" + + "There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", + "Bot", LogLevel.ERROR); } } } diff --git a/DiscordBot/Utilities/Console Utilities.cs b/DiscordBot/Utilities/Console Utilities.cs index 26f9190..5fdbe60 100644 --- a/DiscordBot/Utilities/Console Utilities.cs +++ b/DiscordBot/Utilities/Console Utilities.cs @@ -40,23 +40,46 @@ public static class ConsoleUtilities { T result = default; await AnsiConsole.Progress() - .Columns(new ProgressColumn[] - { - new TaskDescriptionColumn(), - new ProgressBarColumn(), - new PercentageColumn(), - }) - .StartAsync(async ctx => - { - var task = ctx.AddTask(message); - task.IsIndeterminate = true; - result = await function; - task.Increment(100); - - }); + .Columns( + new ProgressColumn[] + { + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + } + ) + .StartAsync( + async ctx => + { + var task = ctx.AddTask(message); + task.IsIndeterminate = true; + result = await function; + task.Increment(100); + + } + ); return result; } + + public static async Task ExecuteWithProgressBar(Task function, string message) + { + await AnsiConsole.Progress() + .Columns(new ProgressColumn[] + { + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + }) + .StartAsync(async ctx => + { + var task = ctx.AddTask(message); + task.IsIndeterminate = true; + await function; + task.Increment(100); + + }); + } private static readonly Dictionary Colors = new() { diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index e2c0de7..3bac0fd 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -9,9 +9,9 @@ namespace PluginManager; public class Config { - private static bool IsLoaded; - public static DBLogger Logger; - public static SettingsDictionary? AppSettings; + private static bool _isLoaded; + public static DBLogger? Logger; + public static SettingsDictionary AppSettings; internal static Boot? _DiscordBotClient; @@ -19,8 +19,8 @@ public class Config public static async Task Initialize() { - if (IsLoaded) return; - + if (_isLoaded) return; + Directory.CreateDirectory("./Data/Resources"); Directory.CreateDirectory("./Data/Plugins"); Directory.CreateDirectory("./Data/PAKS"); @@ -36,7 +36,7 @@ public class Config ArchiveManager.Initialize(); - IsLoaded = true; + _isLoaded = true; Logger.Log("Config initialized", LogLevel.INFO); } diff --git a/PluginManager/Others/Actions/InternalActionsManager.cs b/PluginManager/Others/Actions/InternalActionsManager.cs index 0ee4498..2bd3a6a 100644 --- a/PluginManager/Others/Actions/InternalActionsManager.cs +++ b/PluginManager/Others/Actions/InternalActionsManager.cs @@ -29,7 +29,7 @@ public class InternalActionManager public async Task Refresh() { - Actions.Clear(); + Actions.Clear(); await Initialize(); }