Updated Logger and Created Command to change settings variables

This commit is contained in:
2023-10-01 14:11:34 +03:00
parent f58a57c6cd
commit 6279c5c3a9
7 changed files with 139 additions and 5 deletions

View File

@@ -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();
}
}

View File

@@ -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] <setting?> <value?>";
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 <settingName> <newValue>: Set a setting");
Console.WriteLine("-r <settingName>: Remove a setting");
Console.WriteLine("-a <settingName> <newValue>: Add a setting");
Console.WriteLine("-h: Show this help message");
break;
default:
Console.WriteLine("Invalid option");
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}

View File

@@ -19,7 +19,6 @@ public class Program
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
[STAThread]
public static void Startup(string[] args) public static void Startup(string[] args)
{ {
PreLoadComponents(args).Wait(); PreLoadComponents(args).Wait();
@@ -128,6 +127,13 @@ public class Program
_ => "[white]" _ => "[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} [/]"); AnsiConsole.MarkupLine($"{messageColor}{logMessage.ThrowTime} {logMessage.Message} [/]");
}; };

View File

@@ -139,6 +139,12 @@ internal class CommandHandler
DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean); 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) if (context.Channel is SocketDMChannel)
plugin.ExecuteDM(cmd); plugin.ExecuteDM(cmd);
else plugin.ExecuteServer(cmd); else plugin.ExecuteServer(cmd);

View File

@@ -37,7 +37,7 @@ public class Config
_isLoaded = true; _isLoaded = true;
await Logger.Log(message: "Config initialized", source: typeof(Config)); Logger.Log(message: "Config initialized", source: typeof(Config));
} }
} }

View File

@@ -11,7 +11,7 @@ internal interface ILogger
bool OutputToFile { get; init; } bool OutputToFile { get; init; }
event EventHandler<Log> OnLog; event EventHandler<Log> OnLog;
Task Log( void Log(
string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO, string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO,
DateTime throwTime = default); DateTime throwTime = default);
} }

View File

@@ -38,7 +38,7 @@ public sealed class Logger : ILogger
(UseShortVersion ? logMessage : logMessage.AsLongString()) + "\n"); (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; if (!IsEnabled) return;
@@ -53,5 +53,17 @@ public sealed class Logger : ILogger
if (source == default) source = typeof(Log); if (source == default) source = typeof(Log);
await Log(new Log(message, outputFile, source, type, throwTime)); 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));
} }
} }