From 77f1bef8621d163a0cb6afada134bb693b9debd6 Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Sun, 28 May 2023 17:37:19 +0300 Subject: [PATCH] Changed from TextType to LogLevel --- DiscordBot/Installer.cs | 2 +- DiscordBot/Program.cs | 10 +++---- .../Utilities/ConsoleCommandsHandler.cs | 8 +++--- PluginManager/Bot/Boot.cs | 4 +-- PluginManager/Bot/CommandHandler.cs | 4 +-- PluginManager/Config.cs | 2 +- PluginManager/Loaders/Loader.cs | 4 +-- PluginManager/Loaders/PluginLoader.cs | 12 ++++----- PluginManager/Online/PluginsManager.cs | 7 ++--- PluginManager/Others/ArchiveManager.cs | 10 +++---- PluginManager/Others/Enums.cs | 27 +++---------------- PluginManager/Others/Logger/DBLogger.cs | 8 +++--- PluginManager/Others/Logger/LogMessage.cs | 12 ++++----- 13 files changed, 45 insertions(+), 65 deletions(-) diff --git a/DiscordBot/Installer.cs b/DiscordBot/Installer.cs index 35b6fa9..85dd617 100644 --- a/DiscordBot/Installer.cs +++ b/DiscordBot/Installer.cs @@ -32,7 +32,7 @@ namespace DiscordBot Config.Data.Add("prefix", prefix); Config.Data.Add("ServerID", serverId); - Config.Logger.Log("Config Saved", "Installer", TextType.NORMAL); + Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO); Config.Data.Save(); diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 18cff15..73a593d 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -14,7 +14,7 @@ using PluginManager.Online.Helpers; using PluginManager.Others; using DiscordBot.Utilities; - +using Microsoft.VisualBasic.CompilerServices; using OperatingSystem = PluginManager.Others.OperatingSystem; using static PluginManager.Config; @@ -121,7 +121,7 @@ public class Program } catch (Exception ex) { - Config.Logger.Log(ex.ToString(), "Bot", TextType.ERROR); + Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR); return null; } } @@ -154,7 +154,7 @@ public class Program if (Config.Data.ContainsKey("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 !"); - 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; @@ -200,7 +200,7 @@ public class Program } 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) { - Config.Logger.Log(ex.Message, "Updater", TextType.ERROR); + Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR); if (ex.Message.Contains("Access de")) Config.Logger.Log("Please run the bot as root."); } diff --git a/DiscordBot/Utilities/ConsoleCommandsHandler.cs b/DiscordBot/Utilities/ConsoleCommandsHandler.cs index 338445e..a0043af 100644 --- a/DiscordBot/Utilities/ConsoleCommandsHandler.cs +++ b/DiscordBot/Utilities/ConsoleCommandsHandler.cs @@ -243,7 +243,7 @@ public class ConsoleCommandsHandler if (File.Exists("./" + split[1])) File.Delete("./" + split[1]); 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(); if (split[0].EndsWith(".pak")) @@ -254,7 +254,7 @@ public class ConsoleCommandsHandler { Console.WriteLine($"Extracting {split[1]} ..."); await ArchiveManager.ExtractArchive("./" + split[1], "./", null, - UnzipProgressType.PercentageFromTotalSize); + UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE); Console.WriteLine("\n"); File.Delete("./" + split[1]); } @@ -271,7 +271,7 @@ public class ConsoleCommandsHandler isDownloading = false; - Config.Logger.Log("Plugin installed !", this, TextType.SUCCESS); + Config.Logger.Log("Plugin installed !", this, LogLevel.INFO); //await ExecuteCommad("localload " + name); } @@ -373,7 +373,7 @@ public class ConsoleCommandsHandler catch (Exception ex) { Console.WriteLine(ex.Message); - Config.Logger.Log(ex.Message, this, TextType.ERROR); + Config.Logger.Log(ex.Message, this, LogLevel.ERROR); } }); diff --git a/PluginManager/Bot/Boot.cs b/PluginManager/Bot/Boot.cs index 506bcf0..6fc54b2 100644 --- a/PluginManager/Bot/Boot.cs +++ b/PluginManager/Bot/Boot.cs @@ -106,7 +106,7 @@ public class Boot if (arg.Message.Contains("401")) { 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); Environment.Exit(0); } @@ -136,7 +136,7 @@ public class Boot { case LogSeverity.Error: case LogSeverity.Critical: - Config.Logger.Log(message.Message, this, Others.TextType.ERROR); + Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR); break; diff --git a/PluginManager/Bot/CommandHandler.cs b/PluginManager/Bot/CommandHandler.cs index 9493000..a0bf94b 100644 --- a/PluginManager/Bot/CommandHandler.cs +++ b/PluginManager/Bot/CommandHandler.cs @@ -61,7 +61,7 @@ internal class CommandHandler } catch (Exception ex) { - Config.Logger.Log(ex.Message, "CommandHandler", TextType.ERROR); + Config.Logger.Log(ex.Message, "CommandHandler", LogLevel.ERROR); } return Task.CompletedTask; @@ -144,7 +144,7 @@ internal class CommandHandler } catch (Exception ex) { - Config.Logger.Log(ex.Message, this, TextType.ERROR); + Config.Logger.Log(ex.Message, this, LogLevel.ERROR); } } } \ No newline at end of file diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index bfee239..d9b5886 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -41,7 +41,7 @@ public static class Config IsLoaded = true; - Logger.Log("Config initialized", TextType.NORMAL); + Logger.Log("Config initialized", LogLevel.INFO); } public class Json : IDictionary diff --git a/PluginManager/Loaders/Loader.cs b/PluginManager/Loaders/Loader.cs index c1f971e..ecf4582 100644 --- a/PluginManager/Loaders/Loader.cs +++ b/PluginManager/Loaders/Loader.cs @@ -60,7 +60,7 @@ namespace PluginManager.Loaders } 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; } if (FileLoaded != null) @@ -131,7 +131,7 @@ namespace PluginManager.Loaders } catch (Exception ex) { - Config.Logger.Log(ex.Message, this, Others.TextType.ERROR); + Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR); return null; } diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index e4b3b95..f9f326c 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -87,10 +87,10 @@ public class PluginLoader Events = new List(); SlashCommands = new List(); - 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"); - 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; var res = loader.Load(); Events = res.Item1; @@ -116,7 +116,7 @@ public class PluginLoader } catch (Exception ex) { - Config.Logger.Log(ex.Message, this, Others.TextType.ERROR); + Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR); } break; case "DBSlashCommand": @@ -146,13 +146,13 @@ public class PluginLoader var instance = (DBEvent)Activator.CreateInstance(type); instance.Start(client); 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)) { var instance = (DBCommand)Activator.CreateInstance(type); 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)) { @@ -165,7 +165,7 @@ public class PluginLoader await client.CreateGlobalApplicationCommandAsync(builder.Build()); 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); } } diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 9b9edf3..5d0c850 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -14,7 +14,8 @@ public class PluginsManager /// /// The Plugin Manager constructor /// - /// The link to the file where all plugins are stored + /// The link to the file where all plugins are stored + /// The link to the file where all plugin versions are stored public PluginsManager(string plink, string vlink) { PluginsLink = plink; @@ -82,7 +83,7 @@ public class PluginsManager } 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; @@ -134,7 +135,7 @@ public class PluginsManager } 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! }; diff --git a/PluginManager/Others/ArchiveManager.cs b/PluginManager/Others/ArchiveManager.cs index 6f32cf3..6197f6b 100644 --- a/PluginManager/Others/ArchiveManager.cs +++ b/PluginManager/Others/ArchiveManager.cs @@ -58,7 +58,7 @@ namespace PluginManager.Others } 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); return await ReadFromPakAsync(FileName, archFile); } @@ -79,7 +79,7 @@ namespace PluginManager.Others Directory.CreateDirectory(folder); using (var archive = ZipFile.OpenRead(zip)) { - if (type == UnzipProgressType.PercentageFromNumberOfFiles) + if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES) { var totalZIPFiles = archive.Entries.Count(); var currentZIPFile = 0; @@ -95,7 +95,7 @@ namespace PluginManager.Others } 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++; @@ -104,7 +104,7 @@ namespace PluginManager.Others progress.Report((float)currentZIPFile / totalZIPFiles * 100); } } - else if (type == UnzipProgressType.PercentageFromTotalSize) + else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE) { ulong zipSize = 0; @@ -127,7 +127,7 @@ namespace PluginManager.Others } 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); diff --git a/PluginManager/Others/Enums.cs b/PluginManager/Others/Enums.cs index 6f5a85d..cee40be 100644 --- a/PluginManager/Others/Enums.cs +++ b/PluginManager/Others/Enums.cs @@ -11,23 +11,10 @@ public enum OperatingSystem UNKNOWN } -/// -/// A list with all errors -/// -public enum Error -{ - UNKNOWN_ERROR, - GUILD_NOT_FOUND, - STREAM_NOT_FOUND, - INVALID_USER, - INVALID_CHANNEL, - INVALID_PERMISSIONS -} - /// /// The output log type /// -public enum OutputLogLevel +public enum LogLevel { NONE, INFO, @@ -38,8 +25,8 @@ public enum OutputLogLevel public enum UnzipProgressType { - PercentageFromNumberOfFiles, - PercentageFromTotalSize + PERCENTAGE_FROM_NUMBER_OF_FILES, + PERCENTAGE_FROM_TOTAL_SIZE } public enum SaveType @@ -48,11 +35,3 @@ public enum SaveType BACKUP } -public enum TextType -{ - NORMAL, - ERROR, - WARNING, - SUCCESS -} - diff --git a/PluginManager/Others/Logger/DBLogger.cs b/PluginManager/Others/Logger/DBLogger.cs index 10ca6f6..1d12b9c 100644 --- a/PluginManager/Others/Logger/DBLogger.cs +++ b/PluginManager/Others/Logger/DBLogger.cs @@ -16,7 +16,7 @@ namespace PluginManager.Others.Logger public IReadOnlyList Logs => LogHistory; public IReadOnlyList Errors => ErrorHistory; - public delegate void LogHandler(string message, TextType logType); + public delegate void LogHandler(string message, LogLevel logType); public event LogHandler LogEvent; private string _logFolder; @@ -28,20 +28,20 @@ namespace PluginManager.Others.Logger _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) { if(LogEvent is not null) LogEvent?.Invoke(message.Message, message.Type); - if (message.Type != TextType.ERROR) + if (message.Type != LogLevel.NONE) LogHistory.Add(message); else 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() { diff --git a/PluginManager/Others/Logger/LogMessage.cs b/PluginManager/Others/Logger/LogMessage.cs index 6976506..bc9b0a7 100644 --- a/PluginManager/Others/Logger/LogMessage.cs +++ b/PluginManager/Others/Logger/LogMessage.cs @@ -9,17 +9,17 @@ namespace PluginManager.Others.Logger public class LogMessage { public string Message { get; set; } - public TextType Type { get; set; } + public LogLevel Type { get; set; } public string Time { get; set; } public string Sender { get; set; } - public LogMessage(string message, TextType type) + public LogMessage(string message, LogLevel type) { Message = message; Type = type; 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; } @@ -31,15 +31,15 @@ namespace PluginManager.Others.Logger 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); } - 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); }