Improved logging.
This commit is contained in:
@@ -17,9 +17,8 @@ public class Exit : ICommandAction
|
||||
{
|
||||
if (args is null || args.Length == 0)
|
||||
{
|
||||
Config.Logger.Log("Exiting...", "Exit", isInternal: false);
|
||||
Config.Logger.Log("Exiting...", source: typeof(ICommandAction), type: LogType.WARNING);
|
||||
await Config.AppSettings.SaveToFile();
|
||||
await Config.Logger.SaveToFile();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else
|
||||
@@ -34,7 +33,7 @@ public class Exit : ICommandAction
|
||||
|
||||
case "-f":
|
||||
case "force":
|
||||
Config.Logger.Log("Exiting (FORCE)...", "Exit", LogLevel.WARNING, false);
|
||||
Config.Logger.Log("Exiting (FORCE)...", source: typeof(ICommandAction), type: LogType.WARNING);
|
||||
Environment.Exit(0);
|
||||
break;
|
||||
|
||||
|
||||
204
DiscordBot/Bot/Actions/Extra/PluginMethods.cs
Normal file
204
DiscordBot/Bot/Actions/Extra/PluginMethods.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBot.Utilities;
|
||||
using PluginManager;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Loaders;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Others;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace DiscordBot.Bot.Actions.Extra;
|
||||
|
||||
internal static class PluginMethods
|
||||
{
|
||||
private static readonly PluginsManager PluginsManager = new();
|
||||
|
||||
internal static async Task List()
|
||||
{
|
||||
var data = await ConsoleUtilities.ExecuteWithProgressBar(PluginsManager.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();
|
||||
}
|
||||
|
||||
internal static async Task RefreshPlugins(bool quiet)
|
||||
{
|
||||
await Program.internalActionManager.Execute("plugin", "load", quiet ? "-q" : string.Empty);
|
||||
await Program.internalActionManager.Refresh();
|
||||
}
|
||||
|
||||
internal static async Task DownloadPlugin(PluginsManager manager, string pluginName)
|
||||
{
|
||||
var pluginData = await manager.GetPluginLinkByName(pluginName);
|
||||
if (pluginData.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
var pluginType = pluginData[0];
|
||||
var pluginLink = pluginData[1];
|
||||
var pluginRequirements = pluginData[2];
|
||||
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var downloadTask = ctx.AddTask("Downloading plugin...");
|
||||
|
||||
IProgress<float> progress = new Progress<float>(p => { downloadTask.Value = p; });
|
||||
|
||||
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress);
|
||||
|
||||
downloadTask.Increment(100);
|
||||
|
||||
ctx.Refresh();
|
||||
});
|
||||
|
||||
if (pluginRequirements == string.Empty)
|
||||
{
|
||||
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||
await RefreshPlugins(false);
|
||||
return;
|
||||
}
|
||||
|
||||
List<string> requirementsUrLs = new();
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
||||
gatherInformationTask.IsIndeterminate = true;
|
||||
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||
await Task.Delay(2000);
|
||||
gatherInformationTask.Increment(100);
|
||||
});
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
List<Tuple<ProgressTask, IProgress<float>, Task>> downloadTasks = new();
|
||||
|
||||
foreach (var info in requirementsUrLs)
|
||||
{
|
||||
if (info.Length < 2) continue;
|
||||
string[] data = info.Split(',');
|
||||
string url = data[0];
|
||||
string fileName = data[1];
|
||||
|
||||
var task = ctx.AddTask($"Downloading {fileName}...");
|
||||
IProgress<float> progress = new Progress<float>(p =>
|
||||
{
|
||||
task.Value = p;
|
||||
});
|
||||
|
||||
var downloadTask = ServerCom.DownloadFileAsync(url, $"./{fileName}", progress);
|
||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, Task>(task, progress, downloadTask));
|
||||
}
|
||||
|
||||
foreach (var task in downloadTasks)
|
||||
{
|
||||
await task.Item3;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
await RefreshPlugins(false);
|
||||
}
|
||||
|
||||
internal static async Task<bool> LoadPlugins(string[] args)
|
||||
{
|
||||
var loader = new PluginLoader(Config.DiscordBot.client);
|
||||
if (args.Length == 2 && args[1] == "-q")
|
||||
{
|
||||
loader.LoadPlugins();
|
||||
return true;
|
||||
}
|
||||
|
||||
var cc = Console.ForegroundColor;
|
||||
loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
if (success)
|
||||
{
|
||||
Config.Logger.Log("Successfully loaded command : " + name, source: typeof(ICommandAction),
|
||||
type: LogType.INFO);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Config.Logger.Log("Failed to load command : " + name + " because " + exception?.Message,
|
||||
source: typeof(ICommandAction), type: LogType.ERROR);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
loader.onEVELoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
|
||||
if (success)
|
||||
{
|
||||
Config.Logger.Log("Successfully loaded event : " + name, source: typeof(ICommandAction),
|
||||
type: LogType.INFO);
|
||||
}
|
||||
else
|
||||
{
|
||||
Config.Logger.Log("Failed to load event : " + name + " because " + exception?.Message,
|
||||
source: typeof(ICommandAction), type: LogType.ERROR);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
|
||||
loader.onSLSHLoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
|
||||
if (success)
|
||||
{
|
||||
Config.Logger.Log("Successfully loaded slash command : " + name, source: typeof(ICommandAction),
|
||||
type: LogType.INFO);
|
||||
}
|
||||
else
|
||||
{
|
||||
Config.Logger.Log("Failed to load slash command : " + name + " because " + exception?.Message,
|
||||
source: typeof(ICommandAction), type: LogType.ERROR);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
|
||||
loader.LoadPlugins();
|
||||
Console.ForegroundColor = cc;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBot.Utilities;
|
||||
using System.Windows.Input;
|
||||
using DiscordBot.Bot.Actions.Extra;
|
||||
using PluginManager;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Loaders;
|
||||
@@ -40,105 +39,26 @@ public class Plugin : ICommandAction
|
||||
switch (args[0])
|
||||
{
|
||||
case "refresh":
|
||||
await RefreshPlugins(true);
|
||||
await PluginMethods.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();
|
||||
await PluginMethods.List();
|
||||
break;
|
||||
|
||||
|
||||
case "load":
|
||||
if (pluginsLoaded)
|
||||
break;
|
||||
if (Config.DiscordBot is null)
|
||||
{
|
||||
AnsiConsole.MarkupLine("[red]Discord bot not initialized[/]");
|
||||
Config.Logger.Log("Plugins already loaded", source: typeof(ICommandAction), type: LogType.WARNING);
|
||||
break;
|
||||
}
|
||||
|
||||
var loader = new PluginLoader(Config.DiscordBot.client);
|
||||
if (args.Length == 2 && args[1] == "-q")
|
||||
if (Config.DiscordBot is null)
|
||||
{
|
||||
Console.WriteLine("Loading plugins in quiet mode...");
|
||||
loader.LoadPlugins();
|
||||
pluginsLoaded = true;
|
||||
Config.Logger.Log("DiscordBot is null", source: typeof(ICommandAction), type: LogType.WARNING);
|
||||
break;
|
||||
}
|
||||
|
||||
var cc = Console.ForegroundColor;
|
||||
loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("[CMD] Successfully loaded command : " + name);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
if (exception is null)
|
||||
Console.WriteLine("An error occured while loading: " + name);
|
||||
else
|
||||
Console.WriteLine("[CMD] Failed to load command : " + name + " because " +
|
||||
exception!.Message
|
||||
);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
loader.onEVELoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("[EVENT] Successfully loaded event : " + name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
|
||||
loader.onSLSHLoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
name = typeName;
|
||||
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("[SLASH] Successfully loaded command : " + name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("[SLASH] Failed to load command : " + name + " because " +
|
||||
exception!.Message
|
||||
);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
};
|
||||
|
||||
loader.LoadPlugins();
|
||||
Console.ForegroundColor = cc;
|
||||
pluginsLoaded = true;
|
||||
pluginsLoaded = await PluginMethods.LoadPlugins(args);
|
||||
break;
|
||||
|
||||
case "install":
|
||||
@@ -155,112 +75,8 @@ public class Plugin : ICommandAction
|
||||
}
|
||||
}
|
||||
|
||||
await DownloadPlugin(manager, pluginName);
|
||||
await PluginMethods.DownloadPlugin(manager, pluginName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RefreshPlugins(bool quiet)
|
||||
{
|
||||
await Program.internalActionManager.Execute("plugin", "load", quiet ? "-q" : string.Empty);
|
||||
await Program.internalActionManager.Refresh();
|
||||
}
|
||||
|
||||
|
||||
public async Task DownloadPlugin(PluginsManager manager, string pluginName)
|
||||
{
|
||||
var pluginData = await manager.GetPluginLinkByName(pluginName);
|
||||
if (pluginData.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
var pluginType = pluginData[0];
|
||||
var pluginLink = pluginData[1];
|
||||
var pluginRequirements = pluginData[2];
|
||||
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var downloadTask = ctx.AddTask("Downloading plugin...");
|
||||
|
||||
IProgress<float> progress = new Progress<float>(p => { downloadTask.Value = p; });
|
||||
|
||||
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress);
|
||||
|
||||
downloadTask.Increment(100);
|
||||
|
||||
ctx.Refresh();
|
||||
});
|
||||
|
||||
if (pluginRequirements == string.Empty)
|
||||
{
|
||||
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||
await RefreshPlugins(false);
|
||||
return;
|
||||
}
|
||||
|
||||
List<string> requirementsUrLs = new();
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
||||
gatherInformationTask.IsIndeterminate = true;
|
||||
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||
await Task.Delay(2000);
|
||||
gatherInformationTask.Increment(100);
|
||||
});
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
List<Tuple<ProgressTask, IProgress<float>, Task>> downloadTasks = new();
|
||||
|
||||
foreach (var info in requirementsUrLs)
|
||||
{
|
||||
if (info.Length < 2) continue;
|
||||
string[] data = info.Split(',');
|
||||
string url = data[0];
|
||||
string fileName = data[1];
|
||||
|
||||
var task = ctx.AddTask($"Downloading {fileName}...");
|
||||
IProgress<float> progress = new Progress<float>(p =>
|
||||
{
|
||||
task.Value = p;
|
||||
});
|
||||
|
||||
var downloadTask = ServerCom.DownloadFileAsync(url, $"./{fileName}", progress);
|
||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, Task>(task, progress, downloadTask));
|
||||
}
|
||||
|
||||
foreach (var task in downloadTasks)
|
||||
{
|
||||
await task.Item3;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
await RefreshPlugins(false);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,6 @@ public static class Installer
|
||||
|
||||
AnsiConsole.MarkupLine("[bold]Config saved ![/]");
|
||||
|
||||
Config.Logger.Log("Config Saved", "Installer", isInternal: true);
|
||||
Config.Logger.Log("Config Saved", source: typeof(Installer));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using DiscordBot.Utilities;
|
||||
using PluginManager.Bot;
|
||||
using PluginManager.Others;
|
||||
using PluginManager.Others.Actions;
|
||||
using Spectre.Console;
|
||||
using static PluginManager.Config;
|
||||
|
||||
namespace DiscordBot;
|
||||
@@ -80,7 +81,7 @@ public class Program
|
||||
}
|
||||
catch ( Exception ex )
|
||||
{
|
||||
Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
||||
Logger.Log(ex.ToString(), source: typeof(Program), type: LogType.CRITICAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +108,7 @@ public class Program
|
||||
|
||||
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);
|
||||
source: typeof(Program), type: LogType.ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,19 +117,18 @@ public class Program
|
||||
{
|
||||
await Initialize();
|
||||
|
||||
Logger.LogEvent += (message, type, isInternal) =>
|
||||
Logger.OnLog += (sender, logMessage) =>
|
||||
{
|
||||
if (type == LogLevel.INFO)
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
else if (type == LogLevel.WARNING)
|
||||
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
||||
else if (type == LogLevel.ERROR)
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
else if (type == LogLevel.CRITICAL)
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
|
||||
Console.WriteLine($"[{type.ToString()}] {message}");
|
||||
Console.ResetColor();
|
||||
string messageColor = logMessage.Type switch
|
||||
{
|
||||
LogType.INFO => "[green]",
|
||||
LogType.WARNING => "[yellow]",
|
||||
LogType.ERROR => "[red]",
|
||||
LogType.CRITICAL => "[red]",
|
||||
_ => "[white]"
|
||||
};
|
||||
|
||||
AnsiConsole.MarkupLine($"{messageColor}{logMessage.ThrowTime} {logMessage.Message} [/]");
|
||||
};
|
||||
|
||||
AppSettings["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||
|
||||
Reference in New Issue
Block a user