diff --git a/.idea/.idea.SethDiscordBot/.idea/projectSettingsUpdater.xml b/.idea/.idea.SethDiscordBot/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/.idea/.idea.SethDiscordBot/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.SethDiscordBot/.idea/workspace.xml b/.idea/.idea.SethDiscordBot/.idea/workspace.xml new file mode 100644 index 0000000..39f0d0c --- /dev/null +++ b/.idea/.idea.SethDiscordBot/.idea/workspace.xml @@ -0,0 +1,107 @@ + + + + DiscordBot/DiscordBot.csproj + DiscordBot/DiscordBot.csproj + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1658854902538 + + + + + + + + + + + + + \ No newline at end of file diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 8dd1586..a94ff80 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -112,18 +112,19 @@ public class Program #if DEBUG Console.WriteLine(); - consoleCommandsHandler.HandleCommand("lp"); + ConsoleCommandsHandler.ExecuteCommad("lp").Wait(); #else if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp"); if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs"); #endif - Config.SaveConfig(SaveType.NORMAL); + Config.SaveConfig(SaveType.NORMAL).Wait(); + while (true) { - // Console_Utilities.WriteColorText("&rSethBot (&yDEBUG&r) &c> ", false); + var cmd = Console.ReadLine(); if (!consoleCommandsHandler.HandleCommand(cmd! -#if DEBUG +#if DEBUG , false #endif @@ -177,7 +178,6 @@ public class Program #endif var prefix = Config.GetValue("prefix"); - var discordbooter = new Boot(token, prefix); await discordbooter.Awake(); return discordbooter; @@ -230,11 +230,11 @@ public class Program int p = 1; bool allowed = true; Console.CancelKeyPress += (sender, e) => allowed = false; - Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(); + Console_Utilities.ProgressBar bar = new(ProgressBarType.NO_END);// { NoColor = false, Color = ConsoleColor.DarkRed }; Console.WriteLine("Press Ctrl + C to stop."); while (p <= int.MaxValue - 1 && allowed) { - bar.Update(ProgressBarType.NO_END, 100 / p); + bar.Update(100 / p); await Task.Delay(100); p++; } diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 881ca8d..9bb7140 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -181,7 +181,7 @@ namespace PluginManager SaveConfig(SaveType.NORMAL); } - public static async void SaveConfig(SaveType type) + public static async Task SaveConfig(SaveType type) { if (type == SaveType.NORMAL) { diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index bc95438..4a31091 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -82,30 +82,48 @@ public class ConsoleCommandsHandler if (pluginsLoaded) return; var loader = new PluginLoader(client!); + ConsoleColor cc = Console.ForegroundColor; loader.onCMDLoad += (name, typeName, success, exception) => { - Console.ForegroundColor = ConsoleColor.Green; + 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; + Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message); - Console.ForegroundColor = ConsoleColor.Red; + } + Console.ForegroundColor = cc; }; loader.onEVELoad += (name, typeName, success, exception) => { if (name == null || name.Length < 2) name = typeName; - Console.ForegroundColor = ConsoleColor.Green; + 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 = ConsoleColor.Red; + } + Console.ForegroundColor = cc; }; + loader.LoadPlugins(); + Console.ForegroundColor = cc; pluginsLoaded = true; + } ); @@ -178,14 +196,14 @@ public class ConsoleCommandsHandler Console.WriteLine($"Extracting {split[1]}"); var proc = 0f; var isExtracting = true; - var bar = new Console_Utilities.ProgressBar { Max = 100f, Color = ConsoleColor.Green }; + var bar = new Console_Utilities.ProgressBar(ProgressBarType.NORMAL) { Max = 100f, Color = ConsoleColor.Green }; IProgress extractProgress = new Progress(value => { proc = value; }); new Thread(new Task(() => { while (isExtracting) { - bar.Update(ProgressBarType.NORMAL, proc); + bar.Update(proc); if (proc >= 99.9f) isExtracting = false; Thread.Sleep(500); @@ -194,10 +212,10 @@ public class ConsoleCommandsHandler ).Start ).Start(); await Functions.ExtractArchive("./" + split[1], "./", extractProgress, UnzipProgressType.PercentageFromTotalSize); - bar.Update(ProgressBarType.NORMAL, 100f); + bar.Update(100f); isExtracting = false; await Task.Delay(1000); - bar.Update(ProgressBarType.NORMAL, 100); + bar.Update(100); Console.WriteLine("\n"); File.Delete("./" + split[1]); } @@ -207,7 +225,7 @@ public class ConsoleCommandsHandler } VersionString? ver = await VersionString.GetVersionOfPackageFromWeb(name); if (ver is null) throw new Exception("Incorrect version"); - Config.SetPluginVersion(name, $"{ver.PackageID}.{ver.PackageMainVersion}.{ver.PackageCheckVersion}"); + Config.SetPluginVersion(name, $"{ver.PackageVersionID}.{ver.PackageMainVersion}.{ver.PackageCheckVersion}"); // Console.WriteLine(); isDownloading = false; @@ -255,17 +273,30 @@ public class ConsoleCommandsHandler } ); - AddCommand("sd", "Shuts down the discord bot", () => + AddCommand("sd", "Shuts down the discord bot", async () => { if (client is null) return; - client.StopAsync(); - client.DisposeAsync(); - Config.SaveConfig(SaveType.NORMAL); - Config.SaveConfig(SaveType.BACKUP); - Console.WriteLine("Bot is closing in 2 seconds ! Please wait to save data !"); - Thread.Sleep(2000); + bool run = true; + var t = new Thread(() => + { + Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END); + while (run) + { + bar.Update(1); + Thread.Sleep(50); + } + }); + t.Start(); + await Config.SaveConfig(SaveType.NORMAL); + await Config.SaveConfig(SaveType.BACKUP); + await Task.Delay(4000); + run = false; + Console.WriteLine(); + await client.StopAsync(); + await client.DisposeAsync(); Environment.Exit(0); + } ); @@ -346,7 +377,7 @@ public class ConsoleCommandsHandler Console.WriteLine("Found: " + tuple.ToString()); Config.PluginConfig.InstalledPlugins.Remove(tuple); Config.RemovePluginVersion(plugName); - Config.SaveConfig(SaveType.NORMAL); + await Config.SaveConfig(SaveType.NORMAL); } Console.WriteLine("Removed the plugin DLL. Checking for other files ..."); diff --git a/PluginManager/Loaders/PluginLoader.cs b/PluginManager/Loaders/PluginLoader.cs index 0f20fb8..6e57705 100644 --- a/PluginManager/Loaders/PluginLoader.cs +++ b/PluginManager/Loaders/PluginLoader.cs @@ -69,7 +69,7 @@ public class PluginLoader { string name = new FileInfo(file).Name.Split('.')[0]; if (!Config.PluginVersionsContainsKey(name)) - Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageID + ".0.0"); + Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0"); if (await PluginUpdater.CheckForUpdates(name)) await PluginUpdater.Download(name); @@ -84,7 +84,7 @@ public class PluginLoader { string name = new FileInfo(file).Name.Split('.')[0]; if (!Config.PluginVersionsContainsKey(name)) - Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageID + ".0.0"); + Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0"); if (await PluginUpdater.CheckForUpdates(name)) await PluginUpdater.Download(name); @@ -94,7 +94,7 @@ public class PluginLoader //Save the new config file (after the updates) - Config.SaveConfig(SaveType.NORMAL); + await Config.SaveConfig(SaveType.NORMAL); //Load all plugins diff --git a/PluginManager/Online/Helpers/VersionString.cs b/PluginManager/Online/Helpers/VersionString.cs index 1cf9732..1067228 100644 --- a/PluginManager/Online/Helpers/VersionString.cs +++ b/PluginManager/Online/Helpers/VersionString.cs @@ -9,7 +9,7 @@ namespace PluginManager.Online.Helpers { public class VersionString { - public int PackageID; + public int PackageVersionID; public int PackageMainVersion; public int PackageCheckVersion; @@ -18,7 +18,7 @@ namespace PluginManager.Online.Helpers string[] data = version.Split('.'); try { - PackageID = int.Parse(data[0]); + PackageVersionID = int.Parse(data[0]); PackageMainVersion = int.Parse(data[1]); PackageCheckVersion = int.Parse(data[2]); } @@ -28,21 +28,25 @@ namespace PluginManager.Online.Helpers } } - public static bool operator >(VersionString s1, VersionString s2) - { - if (s1.PackageID != s2.PackageID) throw new Exception("Can not compare two different paks"); - if (s1.PackageMainVersion > s2.PackageMainVersion) return true; - if (s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion > s2.PackageCheckVersion) return true; - return false; - } #region operators + public static bool operator >(VersionString s1, VersionString s2) + { + if (s1.PackageVersionID > s2.PackageVersionID) return true; + if (s1.PackageVersionID == s2.PackageVersionID) + { + if (s1.PackageMainVersion > s2.PackageMainVersion) return true; + if (s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion > s2.PackageCheckVersion) return true; + + } + return false; + } public static bool operator <(VersionString s1, VersionString s2) => !(s1 > s2) && s1 != s2; public static bool operator ==(VersionString s1, VersionString s2) { - if (s1.PackageID == s2.PackageID && s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion == s2.PackageCheckVersion) return true; + if (s1.PackageVersionID == s2.PackageVersionID && s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion == s2.PackageCheckVersion) return true; return false; } @@ -55,12 +59,14 @@ namespace PluginManager.Online.Helpers public override string ToString() { - return "{PackageID: " + PackageID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}"; + return "{PackageID: " + PackageVersionID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}"; } public string ToShortString() { - return $"{PackageID}.{PackageMainVersion}.{PackageCheckVersion}"; + if (PackageVersionID == 0 && PackageCheckVersion == 0 && PackageMainVersion == 0) + return "Unknown"; + return $"{PackageVersionID}.{PackageMainVersion}.{PackageCheckVersion}"; } public static VersionString? GetVersionOfPackage(string pakName) diff --git a/PluginManager/Online/PluginsManager.cs b/PluginManager/Online/PluginsManager.cs index 2ad44b4..2e06edb 100644 --- a/PluginManager/Online/PluginsManager.cs +++ b/PluginManager/Online/PluginsManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; +using PluginManager.Online.Helpers; using PluginManager.Others; using OperatingSystem = PluginManager.Others.OperatingSystem; @@ -39,7 +40,7 @@ public class PluginsManager var op = Functions.GetOperatingSystem(); var len = lines.Length; - string[] titles = { "Name", "Description", "Plugin Type", "Libraries", "Installed" }; + string[] titles = { "Name", "Description", "Type", "Version", "Installed" }; data.Add(new[] { "-", "-", "-", "-", "-" }); data.Add(titles); data.Add(new[] { "-", "-", "-", "-", "-" }); @@ -56,11 +57,7 @@ public class PluginsManager display[0] = content[0]; display[1] = content[1]; display[2] = content[2]; - if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) - display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString(); - - else - display[3] = "1"; + display[3] = (await VersionString.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0")).ToShortString(); if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0])) display[4] = "✓"; else @@ -75,8 +72,7 @@ public class PluginsManager display[0] = content[0]; display[1] = content[1]; display[2] = content[2]; - if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) - display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString(); + display[3] = (await VersionString.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0")).ToShortString(); if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0])) display[4] = "✓"; else diff --git a/PluginManager/Online/ServerCom.cs b/PluginManager/Online/ServerCom.cs index 267042e..925bbf5 100644 --- a/PluginManager/Online/ServerCom.cs +++ b/PluginManager/Online/ServerCom.cs @@ -54,7 +54,7 @@ namespace PluginManager.Online bool isDownloading = true; float c_progress = 0; - Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100f, NoColor = true }; + Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar(ProgressBarType.NORMAL) { Max = 100f, NoColor = true }; IProgress progress = new Progress(percent => { c_progress = percent; }); @@ -63,7 +63,7 @@ namespace PluginManager.Online { while (isDownloading) { - pbar.Update(ProgressBarType.NORMAL, c_progress); + pbar.Update(c_progress); if (c_progress == 100f) break; Thread.Sleep(500); @@ -76,7 +76,7 @@ namespace PluginManager.Online c_progress = pbar.Max; - pbar.Update(ProgressBarType.NORMAL, 100f); + pbar.Update(100f); isDownloading = false; } } diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index fc40d3a..c1277f0 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -using System.Linq; namespace PluginManager.Others { @@ -33,15 +32,21 @@ namespace PluginManager.Others /// public class ProgressBar { + public ProgressBar(ProgressBarType type) + { + this.type = type; + } + public float Max { get; init; } public ConsoleColor Color { get; init; } public bool NoColor { get; init; } + public ProgressBarType type { get; set; } private int BarLength = 32; private int position = 1; private bool positive = true; - public void Update(ProgressBarType type, float progress) + public void Update(float progress) { switch (type) { @@ -52,6 +57,8 @@ namespace PluginManager.Others if (progress <= 99.9f) UpdateNoEnd(); return; + default: + return; } } @@ -81,6 +88,8 @@ namespace PluginManager.Others Console.CursorLeft = 1; float onechunk = 30.0f / Max; + int position = 1; + for (int i = 0; i < onechunk * progress; i++) { Console.BackgroundColor = NoColor ? ConsoleColor.Black : this.Color; @@ -88,14 +97,14 @@ namespace PluginManager.Others Console.Write("#"); } - for (int i = position; i <= BarLength - 1; i++) + for (int i = position; i < BarLength; i++) { Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray; Console.CursorLeft = position++; Console.Write(" "); } - Console.CursorLeft = 35; + Console.CursorLeft = BarLength + 4; Console.BackgroundColor = ConsoleColor.Black; if (progress.CanAproximateTo(Max)) Console.Write(progress + " % ✓");