Changed from TextType to LogLevel

This commit is contained in:
2023-05-28 17:37:19 +03:00
parent f16c139362
commit 77f1bef862
13 changed files with 45 additions and 65 deletions

View File

@@ -32,7 +32,7 @@ namespace DiscordBot
Config.Data.Add("prefix", prefix); Config.Data.Add("prefix", prefix);
Config.Data.Add("ServerID", serverId); Config.Data.Add("ServerID", serverId);
Config.Logger.Log("Config Saved", "Installer", TextType.NORMAL); Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO);
Config.Data.Save(); Config.Data.Save();

View File

@@ -14,7 +14,7 @@ using PluginManager.Online.Helpers;
using PluginManager.Others; using PluginManager.Others;
using DiscordBot.Utilities; using DiscordBot.Utilities;
using Microsoft.VisualBasic.CompilerServices;
using OperatingSystem = PluginManager.Others.OperatingSystem; using OperatingSystem = PluginManager.Others.OperatingSystem;
using static PluginManager.Config; using static PluginManager.Config;
@@ -121,7 +121,7 @@ public class Program
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.ToString(), "Bot", TextType.ERROR); Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
return null; return null;
} }
} }
@@ -154,7 +154,7 @@ public class Program
if (Config.Data.ContainsKey("LaunchMessage")) if (Config.Data.ContainsKey("LaunchMessage"))
Config.Data.Add("LaunchMessage", Config.Data.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 !"); "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 !");
Config.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", TextType.ERROR); Config.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);
} }
} }
return; return;
@@ -200,7 +200,7 @@ public class Program
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.ToString(), "Bot", TextType.ERROR); Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
} }
} }
@@ -284,7 +284,7 @@ public class Program
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, "Updater", TextType.ERROR); Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR);
if (ex.Message.Contains("Access de")) if (ex.Message.Contains("Access de"))
Config.Logger.Log("Please run the bot as root."); Config.Logger.Log("Please run the bot as root.");
} }

View File

@@ -243,7 +243,7 @@ public class ConsoleCommandsHandler
if (File.Exists("./" + split[1])) File.Delete("./" + split[1]); if (File.Exists("./" + split[1])) File.Delete("./" + split[1]);
await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null); await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null);
Console.WriteLine("Item " + split[1] + " downloaded !", this, TextType.SUCCESS); Console.WriteLine("Item " + split[1] + " downloaded !");
Console.WriteLine(); Console.WriteLine();
if (split[0].EndsWith(".pak")) if (split[0].EndsWith(".pak"))
@@ -254,7 +254,7 @@ public class ConsoleCommandsHandler
{ {
Console.WriteLine($"Extracting {split[1]} ..."); Console.WriteLine($"Extracting {split[1]} ...");
await ArchiveManager.ExtractArchive("./" + split[1], "./", null, await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
UnzipProgressType.PercentageFromTotalSize); UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE);
Console.WriteLine("\n"); Console.WriteLine("\n");
File.Delete("./" + split[1]); File.Delete("./" + split[1]);
} }
@@ -271,7 +271,7 @@ public class ConsoleCommandsHandler
isDownloading = false; isDownloading = false;
Config.Logger.Log("Plugin installed !", this, TextType.SUCCESS); Config.Logger.Log("Plugin installed !", this, LogLevel.INFO);
//await ExecuteCommad("localload " + name); //await ExecuteCommad("localload " + name);
} }
@@ -373,7 +373,7 @@ public class ConsoleCommandsHandler
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
Config.Logger.Log(ex.Message, this, TextType.ERROR); Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
} }
}); });

View File

@@ -106,7 +106,7 @@ public class Boot
if (arg.Message.Contains("401")) if (arg.Message.Contains("401"))
{ {
Config.Data.Remove("token"); Config.Data.Remove("token");
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.TextType.ERROR); Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR);
await Task.Delay(4000); await Task.Delay(4000);
Environment.Exit(0); Environment.Exit(0);
} }
@@ -136,7 +136,7 @@ public class Boot
{ {
case LogSeverity.Error: case LogSeverity.Error:
case LogSeverity.Critical: case LogSeverity.Critical:
Config.Logger.Log(message.Message, this, Others.TextType.ERROR); Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR);
break; break;

View File

@@ -61,7 +61,7 @@ internal class CommandHandler
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, "CommandHandler", TextType.ERROR); Config.Logger.Log(ex.Message, "CommandHandler", LogLevel.ERROR);
} }
return Task.CompletedTask; return Task.CompletedTask;
@@ -144,7 +144,7 @@ internal class CommandHandler
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, this, TextType.ERROR); Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
} }
} }
} }

View File

@@ -41,7 +41,7 @@ public static class Config
IsLoaded = true; IsLoaded = true;
Logger.Log("Config initialized", TextType.NORMAL); Logger.Log("Config initialized", LogLevel.INFO);
} }
public class Json<TKey, TValue> : IDictionary<TKey, TValue> public class Json<TKey, TValue> : IDictionary<TKey, TValue>

View File

@@ -60,7 +60,7 @@ namespace PluginManager.Loaders
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.TextType.ERROR); Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.LogLevel.ERROR);
continue; continue;
} }
if (FileLoaded != null) if (FileLoaded != null)
@@ -131,7 +131,7 @@ namespace PluginManager.Loaders
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, this, Others.TextType.ERROR); Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
return null; return null;
} }

View File

@@ -87,10 +87,10 @@ public class PluginLoader
Events = new List<DBEvent>(); Events = new List<DBEvent>();
SlashCommands = new List<DBSlashCommand>(); SlashCommands = new List<DBSlashCommand>();
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.TextType.NORMAL); Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.LogLevel.INFO);
var loader = new Loader("./Data/Plugins", "dll"); var loader = new Loader("./Data/Plugins", "dll");
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.TextType.SUCCESS); loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.LogLevel.INFO);
loader.PluginLoaded += Loader_PluginLoaded; loader.PluginLoaded += Loader_PluginLoaded;
var res = loader.Load(); var res = loader.Load();
Events = res.Item1; Events = res.Item1;
@@ -116,7 +116,7 @@ public class PluginLoader
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, this, Others.TextType.ERROR); Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
} }
break; break;
case "DBSlashCommand": case "DBSlashCommand":
@@ -146,13 +146,13 @@ public class PluginLoader
var instance = (DBEvent)Activator.CreateInstance(type); var instance = (DBEvent)Activator.CreateInstance(type);
instance.Start(client); instance.Start(client);
PluginLoader.Events.Add(instance); PluginLoader.Events.Add(instance);
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.TextType.SUCCESS); Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.LogLevel.INFO);
} }
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type)) else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
{ {
var instance = (DBCommand)Activator.CreateInstance(type); var instance = (DBCommand)Activator.CreateInstance(type);
PluginLoader.Commands.Add(instance); PluginLoader.Commands.Add(instance);
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.TextType.SUCCESS); Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
} }
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type)) else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
{ {
@@ -165,7 +165,7 @@ public class PluginLoader
await client.CreateGlobalApplicationCommandAsync(builder.Build()); await client.CreateGlobalApplicationCommandAsync(builder.Build());
PluginLoader.SlashCommands.Add(instance); PluginLoader.SlashCommands.Add(instance);
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.TextType.SUCCESS); Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
} }
} }

View File

@@ -14,7 +14,8 @@ public class PluginsManager
/// <summary> /// <summary>
/// The Plugin Manager constructor /// The Plugin Manager constructor
/// </summary> /// </summary>
/// <param name="link">The link to the file where all plugins are stored</param> /// <param name="plink">The link to the file where all plugins are stored</param>
/// <param name="vlink">The link to the file where all plugin versions are stored</param>
public PluginsManager(string plink, string vlink) public PluginsManager(string plink, string vlink)
{ {
PluginsLink = plink; PluginsLink = plink;
@@ -82,7 +83,7 @@ public class PluginsManager
} }
catch (Exception exception) catch (Exception exception)
{ {
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, TextType.ERROR); Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
} }
return null; return null;
@@ -134,7 +135,7 @@ public class PluginsManager
} }
catch (Exception exception) catch (Exception exception)
{ {
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, TextType.ERROR); Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
} }
return new string[] { null!, null!, null! }; return new string[] { null!, null!, null! };

View File

@@ -58,7 +58,7 @@ namespace PluginManager.Others
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log(ex.Message, "Archive Manager", TextType.ERROR); // Write the error to a file Config.Logger.Log(ex.Message, "Archive Manager", LogLevel.ERROR); // Write the error to a file
await Task.Delay(100); await Task.Delay(100);
return await ReadFromPakAsync(FileName, archFile); return await ReadFromPakAsync(FileName, archFile);
} }
@@ -79,7 +79,7 @@ namespace PluginManager.Others
Directory.CreateDirectory(folder); Directory.CreateDirectory(folder);
using (var archive = ZipFile.OpenRead(zip)) using (var archive = ZipFile.OpenRead(zip))
{ {
if (type == UnzipProgressType.PercentageFromNumberOfFiles) if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
{ {
var totalZIPFiles = archive.Entries.Count(); var totalZIPFiles = archive.Entries.Count();
var currentZIPFile = 0; var currentZIPFile = 0;
@@ -95,7 +95,7 @@ namespace PluginManager.Others
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", TextType.ERROR); Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
} }
currentZIPFile++; currentZIPFile++;
@@ -104,7 +104,7 @@ namespace PluginManager.Others
progress.Report((float)currentZIPFile / totalZIPFiles * 100); progress.Report((float)currentZIPFile / totalZIPFiles * 100);
} }
} }
else if (type == UnzipProgressType.PercentageFromTotalSize) else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
{ {
ulong zipSize = 0; ulong zipSize = 0;
@@ -127,7 +127,7 @@ namespace PluginManager.Others
} }
catch (Exception ex) catch (Exception ex)
{ {
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", TextType.ERROR); Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
} }
await Task.Delay(10); await Task.Delay(10);

View File

@@ -11,23 +11,10 @@ public enum OperatingSystem
UNKNOWN UNKNOWN
} }
/// <summary>
/// A list with all errors
/// </summary>
public enum Error
{
UNKNOWN_ERROR,
GUILD_NOT_FOUND,
STREAM_NOT_FOUND,
INVALID_USER,
INVALID_CHANNEL,
INVALID_PERMISSIONS
}
/// <summary> /// <summary>
/// The output log type /// The output log type
/// </summary> /// </summary>
public enum OutputLogLevel public enum LogLevel
{ {
NONE, NONE,
INFO, INFO,
@@ -38,8 +25,8 @@ public enum OutputLogLevel
public enum UnzipProgressType public enum UnzipProgressType
{ {
PercentageFromNumberOfFiles, PERCENTAGE_FROM_NUMBER_OF_FILES,
PercentageFromTotalSize PERCENTAGE_FROM_TOTAL_SIZE
} }
public enum SaveType public enum SaveType
@@ -48,11 +35,3 @@ public enum SaveType
BACKUP BACKUP
} }
public enum TextType
{
NORMAL,
ERROR,
WARNING,
SUCCESS
}

View File

@@ -16,7 +16,7 @@ namespace PluginManager.Others.Logger
public IReadOnlyList<LogMessage> Logs => LogHistory; public IReadOnlyList<LogMessage> Logs => LogHistory;
public IReadOnlyList<LogMessage> Errors => ErrorHistory; public IReadOnlyList<LogMessage> Errors => ErrorHistory;
public delegate void LogHandler(string message, TextType logType); public delegate void LogHandler(string message, LogLevel logType);
public event LogHandler LogEvent; public event LogHandler LogEvent;
private string _logFolder; private string _logFolder;
@@ -28,20 +28,20 @@ namespace PluginManager.Others.Logger
_errFolder = Config.Data["ErrorFolder"]; _errFolder = Config.Data["ErrorFolder"];
} }
public void Log(string message, string sender = "unknown", TextType type = TextType.NORMAL) => Log(new LogMessage(message, type, sender)); public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
public void Log(LogMessage message) public void Log(LogMessage message)
{ {
if(LogEvent is not null) if(LogEvent is not null)
LogEvent?.Invoke(message.Message, message.Type); LogEvent?.Invoke(message.Message, message.Type);
if (message.Type != TextType.ERROR) if (message.Type != LogLevel.NONE)
LogHistory.Add(message); LogHistory.Add(message);
else else
ErrorHistory.Add(message); ErrorHistory.Add(message);
} }
public void Log(string message, object sender, TextType type = TextType.NORMAL) => Log(message, sender.GetType().Name, type); public void Log(string message, object sender, LogLevel type = LogLevel.NONE) => Log(message, sender.GetType().Name, type);
public async void SaveToFile() public async void SaveToFile()
{ {

View File

@@ -9,17 +9,17 @@ namespace PluginManager.Others.Logger
public class LogMessage public class LogMessage
{ {
public string Message { get; set; } public string Message { get; set; }
public TextType Type { get; set; } public LogLevel Type { get; set; }
public string Time { get; set; } public string Time { get; set; }
public string Sender { get; set; } public string Sender { get; set; }
public LogMessage(string message, TextType type) public LogMessage(string message, LogLevel type)
{ {
Message = message; Message = message;
Type = type; Type = type;
Time = DateTime.Now.ToString("HH:mm:ss"); Time = DateTime.Now.ToString("HH:mm:ss");
} }
public LogMessage(string message, TextType type, string sender) : this(message, type) public LogMessage(string message, LogLevel type, string sender) : this(message, type)
{ {
Sender = sender; Sender = sender;
} }
@@ -31,15 +31,15 @@ namespace PluginManager.Others.Logger
public static explicit operator LogMessage(string message) public static explicit operator LogMessage(string message)
{ {
return new LogMessage(message, TextType.NORMAL); return new LogMessage(message, LogLevel.INFO);
} }
public static explicit operator LogMessage((string message, TextType type) tuple) public static explicit operator LogMessage((string message, LogLevel type) tuple)
{ {
return new LogMessage(tuple.message, tuple.type); return new LogMessage(tuple.message, tuple.type);
} }
public static explicit operator LogMessage((string message, TextType type, string sender) tuple) public static explicit operator LogMessage((string message, LogLevel type, string sender) tuple)
{ {
return new LogMessage(tuple.message, tuple.type, tuple.sender); return new LogMessage(tuple.message, tuple.type, tuple.sender);
} }