Updated Logger and Created Command to change settings variables
This commit is contained in:
43
DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs
Normal file
43
DiscordBot/Bot/Actions/Extra/SettingsConfigExtra.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
67
DiscordBot/Bot/Actions/SettingsConfig.cs
Normal file
67
DiscordBot/Bot/Actions/SettingsConfig.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ public class Program
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[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} [/]");
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ internal interface ILogger
|
||||
bool OutputToFile { get; init; }
|
||||
|
||||
event EventHandler<Log> OnLog;
|
||||
Task Log(
|
||||
void Log(
|
||||
string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO,
|
||||
DateTime throwTime = default);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user