Fixed plugin refresh command and added new method for executing tasks without return type in ConsoleUtilities

This commit is contained in:
2023-09-25 22:06:42 +03:00
parent c577f625c2
commit 89c4932cd7
8 changed files with 78 additions and 57 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Others;

View File

@@ -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<string> { "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);
}
}

View File

@@ -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);

View File

@@ -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<string>("Please enter the bot token :");
var prefix = AnsiConsole.Ask<string>("Please enter the bot prefix :");
var serverId = AnsiConsole.Ask<string>("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<string>("Please enter the bot [yellow]token[/]:");
var prefix = AnsiConsole.Ask<string>("Please enter the bot [yellow]prefix[/]:");
var serverId = AnsiConsole.Ask<string>("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);

View File

@@ -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);
}
}
}

View File

@@ -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<char, ConsoleColor> Colors = new()
{