Fixed plugin refresh command and added new method for executing tasks without return type in ConsoleUtilities
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Others;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -256,6 +261,6 @@ public class Plugin : ICommandAction
|
||||
|
||||
});
|
||||
|
||||
await RefreshPlugins();
|
||||
await RefreshPlugins(false);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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 !");
|
||||
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 token :");
|
||||
var prefix = AnsiConsole.Ask<string>("Please enter the bot prefix :");
|
||||
var serverId = AnsiConsole.Ask<string>("Please enter the Server ID :");
|
||||
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);
|
||||
|
||||
@@ -23,12 +23,7 @@ public class Program
|
||||
{
|
||||
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,7 +62,8 @@ 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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,24 +40,47 @@ 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()
|
||||
{
|
||||
{ 'g', ConsoleColor.Green },
|
||||
|
||||
@@ -9,9 +9,9 @@ namespace PluginManager;
|
||||
|
||||
public class Config
|
||||
{
|
||||
private static bool IsLoaded;
|
||||
public static DBLogger Logger;
|
||||
public static SettingsDictionary<string, string>? AppSettings;
|
||||
private static bool _isLoaded;
|
||||
public static DBLogger? Logger;
|
||||
public static SettingsDictionary<string, string> AppSettings;
|
||||
|
||||
internal static Boot? _DiscordBotClient;
|
||||
|
||||
@@ -19,7 +19,7 @@ public class Config
|
||||
|
||||
public static async Task Initialize()
|
||||
{
|
||||
if (IsLoaded) return;
|
||||
if (_isLoaded) return;
|
||||
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
Directory.CreateDirectory("./Data/Plugins");
|
||||
@@ -36,7 +36,7 @@ public class Config
|
||||
|
||||
ArchiveManager.Initialize();
|
||||
|
||||
IsLoaded = true;
|
||||
_isLoaded = true;
|
||||
|
||||
Logger.Log("Config initialized", LogLevel.INFO);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class InternalActionManager
|
||||
|
||||
public async Task Refresh()
|
||||
{
|
||||
Actions.Clear();
|
||||
Actions.Clear();
|
||||
await Initialize();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user