diff --git a/DiscordBot/Discord/Commands/Help.cs b/DiscordBot/Discord/Commands/Help.cs index 32d69ad..23fa7ca 100644 --- a/DiscordBot/Discord/Commands/Help.cs +++ b/DiscordBot/Discord/Commands/Help.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Discord; -using Discord.Commands; using PluginManager; using PluginManager.Interfaces; using PluginManager.Loaders; @@ -60,7 +58,7 @@ internal class Help : DBCommand var adminCommands = ""; var normalCommands = ""; - foreach (var cmd in PluginLoader.Commands!) + foreach (var cmd in PluginLoader.Commands) if (cmd.requireAdmin) adminCommands += cmd.Command + " "; else @@ -77,7 +75,7 @@ internal class Help : DBCommand private EmbedBuilder GenerateHelpCommand(string command) { var embedBuilder = new EmbedBuilder(); - var cmd = PluginLoader.Commands!.Find(p => p.Command == command || + var cmd = PluginLoader.Commands.Find(p => p.Command == command || (p.Aliases is not null && p.Aliases.Contains(command))); if (cmd == null) return null; diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index 52f5bfa..15bd7f9 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -1,48 +1,39 @@ - - - - Exe - net6.0 - disable - - - False - True - 1.0.2.0 - - - - none - - - - none - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + Exe + net6.0 + disable + + + False + True + 1.0.2.1 + + + none + + + none + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 358a155..dd0c8bf 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -6,10 +6,8 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; -using DiscordBot.Discord.Core; - using PluginManager; -using PluginManager.Database; +using PluginManager.Bot; using PluginManager.Items; using PluginManager.Online; using PluginManager.Online.Helpers; @@ -105,6 +103,9 @@ public class Program Utilities.WriteColorText( "Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)"); + + Logger.WriteLine(); + Logger.WriteLine("Running on " + Functions.GetOperatingSystem().ToString()); Logger.WriteLine("============================ LOG ============================"); try @@ -174,9 +175,7 @@ public class Program private static async Task PreLoadComponents(string[] args) { - Directory.CreateDirectory("./Data/Resources"); - Directory.CreateDirectory("./Data/Plugins"); - Directory.CreateDirectory("./Data/PAKS"); + if (!File.Exists(Functions.dataFolder + "loader.json")) { @@ -185,17 +184,8 @@ public class Program } else Entry.startupArguments = await Functions.ConvertFromJson(Functions.dataFolder + "loader.json"); - - Settings.sqlDatabase = new SqlDatabase("SetDB.dat"); - - await Settings.sqlDatabase.Open(); - await Config.Initialize(); - Logger.Initialize(true); - ArchiveManager.Initialize(); - - - Logger.LogEvent += (message) => { Console.Write(message); }; + await Config.Initialize("SetDB.dat", true); Logger.WriteLine("Loading resources ..."); var main = new Utilities.ProgressBar(ProgressBarType.NO_END); diff --git a/DiscordBot/Discord/Core/Boot.cs b/PluginManager/Bot/Boot.cs similarity index 79% rename from DiscordBot/Discord/Core/Boot.cs rename to PluginManager/Bot/Boot.cs index 1c9fc4e..9c3a96a 100644 --- a/DiscordBot/Discord/Core/Boot.cs +++ b/PluginManager/Bot/Boot.cs @@ -1,15 +1,14 @@ -using System; +using System.Net.Mime; +using System; using System.Threading.Tasks; using Discord; using Discord.Commands; using Discord.WebSocket; -using PluginManager; +namespace PluginManager.Bot; -namespace DiscordBot.Discord.Core; - -internal class Boot +public class Boot { /// /// The bot prefix @@ -57,10 +56,12 @@ internal class Boot /// /// The start method for the bot. This method is used to load the bot /// + /// The discord socket config. If null then the default one will be applied (AlwaysDownloadUsers=true, UseInteractionSnowflakeDate=false, GatewayIntents=GatewayIntents.All) /// Task - public async Task Awake() + public async Task Awake(DiscordSocketConfig? config = null) { - var config = new DiscordSocketConfig + if(config is null) + config = new DiscordSocketConfig { AlwaysDownloadUsers = true, @@ -100,16 +101,17 @@ internal class Boot client.Disconnected += Client_Disconnected; } - private Task Client_Disconnected(Exception arg) + private async Task Client_Disconnected(Exception arg) { if (arg.Message.Contains("401")) { Config.Variables.RemoveKey("token"); - Program.GenerateStartUI("The token is invalid"); + Logger.LogError("The token is invalid. Please restart the bot and enter a valid token."); + await Task.Delay(4000); + Environment.Exit(0); } Logger.WriteErrFile(arg); - return Task.CompletedTask; } private async Task Client_LoggedOut() @@ -128,9 +130,6 @@ internal class Boot private Task LoggedIn() { Console.Title = "CONNECTED"; - Logger.WriteLine("The bot has been logged in at " + DateTime.Now.ToShortDateString() + " (" + - DateTime.Now.ToShortTimeString() + ")" - ); return Task.CompletedTask; } @@ -141,20 +140,14 @@ internal class Boot case LogSeverity.Error: case LogSeverity.Critical: Logger.WriteErrFile(message.Message); - - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("[ERROR] " + message.Message); - Console.ForegroundColor = ConsoleColor.White; + Logger.WriteColored(message.Message + "\n", ConsoleColor.Red); break; case LogSeverity.Info: case LogSeverity.Debug: Logger.WriteLogFile(message.Message); - - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("[INFO] " + message.Message); - Console.ForegroundColor = ConsoleColor.White; + Logger.WriteColored(message.Message + "\n", ConsoleColor.White); break; diff --git a/DiscordBot/Discord/Core/CommandHandler.cs b/PluginManager/Bot/CommandHandler.cs similarity index 98% rename from DiscordBot/Discord/Core/CommandHandler.cs rename to PluginManager/Bot/CommandHandler.cs index f030628..4eae29d 100644 --- a/DiscordBot/Discord/Core/CommandHandler.cs +++ b/PluginManager/Bot/CommandHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Reflection; using System.Threading.Tasks; @@ -10,9 +10,7 @@ using PluginManager.Loaders; using PluginManager.Others; using PluginManager.Others.Permissions; -using static PluginManager.Logger; - -namespace DiscordBot.Discord.Core; +namespace PluginManager.Bot; internal class CommandHandler { diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 34fed01..0ec0e1d 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -1,36 +1,52 @@ using System; using System.Threading.Tasks; +using System.IO; using PluginManager.Online.Helpers; +using PluginManager.Database; namespace PluginManager; public static class Config { private static bool IsLoaded = false; - public static async Task Initialize() + public static async Task Initialize(string DatabaseName, bool isConsole) { if (IsLoaded) return; + Directory.CreateDirectory("./Data/Resources"); + Directory.CreateDirectory("./Data/Plugins"); + Directory.CreateDirectory("./Data/PAKS"); + + Settings.sqlDatabase = new SqlDatabase(DatabaseName); + await Settings.sqlDatabase.Open(); + + if (!await Settings.sqlDatabase.TableExistsAsync("Plugins")) await Settings.sqlDatabase.CreateTableAsync("Plugins", "PluginName", "Version"); if (!await Settings.sqlDatabase.TableExistsAsync("Variables")) await Settings.sqlDatabase.CreateTableAsync("Variables", "VarName", "Value", "ReadOnly"); IsLoaded = true; + + Logger.Initialize(isConsole); + PluginManager.Others.ArchiveManager.Initialize(); + + if(isConsole) + Logger.LogEvent += (message) => { Console.Write(message); }; } public static class Variables { - public static async Task GetValueAsync(string VarName) + public static async Task GetValueAsync(string VarName) { if (!IsLoaded) throw new Exception("Config is not loaded"); return await Settings.sqlDatabase.GetValueAsync("Variables", "VarName", VarName, "Value"); } - public static string GetValue(string VarName) + public static string? GetValue(string VarName) { if (!IsLoaded) throw new Exception("Config is not loaded"); diff --git a/PluginManager/Logger.cs b/PluginManager/Logger.cs index 4131299..9aa257d 100644 --- a/PluginManager/Logger.cs +++ b/PluginManager/Logger.cs @@ -96,6 +96,25 @@ namespace PluginManager LogEvent?.Invoke($"{c}"); } + public static void Write(T c, params object[] Args) + { + if (!isInitialized) throw new Exception("Logger is not initialized"); + LogEvent?.Invoke(string.Format($"{c}", Args)); + } + + public static void WriteColored(string message, ConsoleColor color) + { + if (!isInitialized) throw new Exception("Logger is not initialized"); + if(!isConsole) { + LogEvent?.Invoke(message); + return; + } + var oldColor = Console.ForegroundColor; + Console.ForegroundColor = color; + LogEvent?.Invoke(message); + Console.ForegroundColor = oldColor; + } + /// /// Write logs to file /// diff --git a/PluginManager/Others/ArchiveManager.cs b/PluginManager/Others/ArchiveManager.cs index f6d092d..1af60dc 100644 --- a/PluginManager/Others/ArchiveManager.cs +++ b/PluginManager/Others/ArchiveManager.cs @@ -9,7 +9,7 @@ namespace PluginManager.Others public static class ArchiveManager { public static bool isInitialized { get; private set; } - private static string archiveFolder; + private static string? archiveFolder; public static void Initialize() { @@ -28,7 +28,7 @@ namespace PluginManager.Others /// The file name that is inside the archive or its full path /// The archive location from the PAKs folder /// A string that represents the content of the file or null if the file does not exists or it has no content - public static async Task ReadFromPakAsync(string FileName, string archFile) + public static async Task ReadFromPakAsync(string FileName, string archFile) { if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); archFile = archiveFolder + archFile; @@ -37,7 +37,7 @@ namespace PluginManager.Others try { - string textValue = null; + string? textValue = null; using (var fs = new FileStream(archFile, FileMode.Open)) using (var zip = new ZipArchive(fs, ZipArchiveMode.Read)) {