From 6279c5c3a9646db4939cf93cd9850f39782af088 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Sun, 1 Oct 2023 14:11:34 +0300 Subject: [PATCH] Updated Logger and Created Command to change settings variables --- .../Bot/Actions/Extra/SettingsConfigExtra.cs | 43 ++++++++++++ DiscordBot/Bot/Actions/SettingsConfig.cs | 67 +++++++++++++++++++ DiscordBot/Program.cs | 8 ++- PluginManager/Bot/CommandHandler.cs | 8 ++- PluginManager/Config.cs | 2 +- PluginManager/Interfaces/Logger/ILogger.cs | 2 +- PluginManager/Others/Logger/Logger.cs | 14 +++- 7 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs create mode 100644 DiscordBot/Bot/Actions/SettingsConfig.cs diff --git a/DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs b/DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs new file mode 100644 index 0000000..b038e39 --- /dev/null +++ b/DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs @@ -0,0 +1,43 @@ +using System.Linq; +using PluginManager; + +namespace DiscordBot.Bot.Actions.Extra; + +internal static class SettingsConfigExtra +{ + internal static void SetSettings(string key, params string[] value) + { + if (key is null) return; + + if (value is null) return; + + if (!Config.AppSettings.ContainsKey(key)) + return; + + Config.AppSettings[key] = string.Join(' ', value); + // Config.AppSettings.SaveToFile().Wait(); + } + + internal static void RemoveSettings(string key) + { + if (key is null) return; + + if(!Config.AppSettings.ContainsKey(key)) + return; + + Config.AppSettings.Remove(key); + } + + internal static void AddSettings(string key, params string[] value) + { + if (key is null) return; + + if (value is null) return; + + if (Config.AppSettings.ContainsKey(key)) + return; + + Config.AppSettings.Add(key, string.Join(' ', value)); + // Config.AppSettings.SaveToFile().Wait(); + } +} \ No newline at end of file diff --git a/DiscordBot/Bot/Actions/SettingsConfig.cs b/DiscordBot/Bot/Actions/SettingsConfig.cs new file mode 100644 index 0000000..332ed75 --- /dev/null +++ b/DiscordBot/Bot/Actions/SettingsConfig.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using DiscordBot.Bot.Actions.Extra; +using PluginManager; +using PluginManager.Interfaces; +using PluginManager.Others; + +namespace DiscordBot.Bot.Actions; + +public class SettingsConfig : ICommandAction +{ + public string ActionName => "config"; + public string Description => "Change the settings of the bot"; + public string Usage => "config [options] "; + public InternalActionRunType RunType => InternalActionRunType.ON_CALL; + public Task Execute(string[] args) + { + if (args is null) + { + foreach (var settings in Config.AppSettings) + Console.WriteLine(settings.Key + ": " + settings.Value); + + return Task.CompletedTask; + } + + switch (args[0]) + { + case "-s": + case "set": + if(args.Length < 3) + return Task.CompletedTask; + SettingsConfigExtra.SetSettings(args[1],args[2..]); + break; + + case "-r": + case "remove": + if(args.Length < 2) + return Task.CompletedTask; + SettingsConfigExtra.RemoveSettings(args[1]); + break; + + case "-a": + case "add": + if(args.Length < 3) + return Task.CompletedTask; + SettingsConfigExtra.AddSettings(args[1], args[2..]); + break; + + case "-h": + case "-help": + Console.WriteLine("Options:"); + Console.WriteLine("-s : Set a setting"); + Console.WriteLine("-r : Remove a setting"); + Console.WriteLine("-a : Add a setting"); + Console.WriteLine("-h: Show this help message"); + break; + + default: + Console.WriteLine("Invalid option"); + return Task.CompletedTask; + } + + + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index a766b73..818b4b7 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -19,7 +19,6 @@ public class Program /// /// The main entry point for the application. /// - [STAThread] public static void Startup(string[] args) { PreLoadComponents(args).Wait(); @@ -127,6 +126,13 @@ public class Program LogType.CRITICAL => "[red]", _ => "[white]" }; + + if (logMessage.Message.Contains('[')) + { + // If the message contains a tag, just print it as it is. No need to format it + Console.WriteLine(logMessage.Message); + return; + } AnsiConsole.MarkupLine($"{messageColor}{logMessage.ThrowTime} {logMessage.Message} [/]"); }; diff --git a/PluginManager/Bot/CommandHandler.cs b/PluginManager/Bot/CommandHandler.cs index bd3e68d..1a4e9fa 100644 --- a/PluginManager/Bot/CommandHandler.cs +++ b/PluginManager/Bot/CommandHandler.cs @@ -138,7 +138,13 @@ internal class CommandHandler argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' '); DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean); - + + Config.Logger.Log( + message: $"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"", + source: typeof(CommandHandler), + type: LogType.INFO + ); + if (context.Channel is SocketDMChannel) plugin.ExecuteDM(cmd); else plugin.ExecuteServer(cmd); diff --git a/PluginManager/Config.cs b/PluginManager/Config.cs index 3ae4505..021966b 100644 --- a/PluginManager/Config.cs +++ b/PluginManager/Config.cs @@ -37,7 +37,7 @@ public class Config _isLoaded = true; - await Logger.Log(message: "Config initialized", source: typeof(Config)); + Logger.Log(message: "Config initialized", source: typeof(Config)); } } diff --git a/PluginManager/Interfaces/Logger/ILogger.cs b/PluginManager/Interfaces/Logger/ILogger.cs index 85c0977..c513cc6 100644 --- a/PluginManager/Interfaces/Logger/ILogger.cs +++ b/PluginManager/Interfaces/Logger/ILogger.cs @@ -11,7 +11,7 @@ internal interface ILogger bool OutputToFile { get; init; } event EventHandler OnLog; - Task Log( + void Log( string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO, DateTime throwTime = default); } diff --git a/PluginManager/Others/Logger/Logger.cs b/PluginManager/Others/Logger/Logger.cs index 99efbe2..2a9e2c7 100644 --- a/PluginManager/Others/Logger/Logger.cs +++ b/PluginManager/Others/Logger/Logger.cs @@ -38,7 +38,7 @@ public sealed class Logger : ILogger (UseShortVersion ? logMessage : logMessage.AsLongString()) + "\n"); } - public async Task Log(string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO, DateTime throwTime = default) + public async void Log(string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO, DateTime throwTime = default) { if (!IsEnabled) return; @@ -53,5 +53,17 @@ public sealed class Logger : ILogger if (source == default) source = typeof(Log); await Log(new Log(message, outputFile, source, type, throwTime)); + + } + + public async void Log(Exception exception, LogType logType = LogType.ERROR, Type? source = null) + { + if (!IsEnabled) return; + + if (logType < LowestLogLevel) return; + + await Log(new Log(exception.Message, + Config.AppSettings["LogFolder"] + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log", + source, logType, DateTime.Now)); } }