Reformatting code

This commit is contained in:
2023-11-21 22:04:38 +02:00
parent 79ecff971b
commit 944d59d9a3
37 changed files with 509 additions and 486 deletions

View File

@@ -53,7 +53,8 @@ internal static class PluginMethods
new TaskDescriptionColumn(), new TaskDescriptionColumn(),
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn() new PercentageColumn()
}) }
)
.StartAsync(async ctx => .StartAsync(async ctx =>
{ {
var downloadTask = ctx.AddTask("Downloading plugin..."); var downloadTask = ctx.AddTask("Downloading plugin...");
@@ -65,7 +66,8 @@ internal static class PluginMethods
downloadTask.Increment(100); downloadTask.Increment(100);
ctx.Refresh(); ctx.Refresh();
}); }
);
if (pluginRequirements == string.Empty) if (pluginRequirements == string.Empty)
{ {
@@ -82,7 +84,8 @@ internal static class PluginMethods
new TaskDescriptionColumn(), new TaskDescriptionColumn(),
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn() new PercentageColumn()
}) }
)
.StartAsync(async ctx => .StartAsync(async ctx =>
{ {
var gatherInformationTask = ctx.AddTask("Gathering info..."); var gatherInformationTask = ctx.AddTask("Gathering info...");
@@ -90,7 +93,8 @@ internal static class PluginMethods
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements); requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
gatherInformationTask.Increment(100); gatherInformationTask.Increment(100);
}); }
);
List<Tuple<ProgressTask, IProgress<float>, string, string>> downloadTasks = new(); List<Tuple<ProgressTask, IProgress<float>, string, string>> downloadTasks = new();
await AnsiConsole.Progress() await AnsiConsole.Progress()
.Columns(new ProgressColumn[] .Columns(new ProgressColumn[]
@@ -98,7 +102,8 @@ internal static class PluginMethods
new TaskDescriptionColumn(), new TaskDescriptionColumn(),
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn() new PercentageColumn()
}) }
)
.StartAsync(async ctx => .StartAsync(async ctx =>
{ {
@@ -114,7 +119,8 @@ internal static class PluginMethods
IProgress<float> progress = new Progress<float>(p => IProgress<float> progress = new Progress<float>(p =>
{ {
task.Value = p; task.Value = p;
}); }
);
task.IsIndeterminate = true; task.IsIndeterminate = true;
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, string, string>(task, progress, url, fileName)); downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, string, string>(task, progress, url, fileName));
@@ -137,11 +143,13 @@ internal static class PluginMethods
{ {
tuple.Item1.IsIndeterminate = false; tuple.Item1.IsIndeterminate = false;
await ServerCom.DownloadFileAsync(tuple.Item3, $"./{tuple.Item4}", tuple.Item2); await ServerCom.DownloadFileAsync(tuple.Item3, $"./{tuple.Item4}", tuple.Item2);
}); }
);
}); }
);
@@ -166,13 +174,15 @@ internal static class PluginMethods
if (success) if (success)
{ {
Config.Logger.Log("Successfully loaded command : " + name, source: typeof(ICommandAction), Config.Logger.Log("Successfully loaded command : " + name, source: typeof(ICommandAction),
type: LogType.INFO); type: LogType.INFO
);
} }
else else
{ {
Config.Logger.Log("Failed to load command : " + name + " because " + exception?.Message, Config.Logger.Log("Failed to load command : " + name + " because " + exception?.Message,
source: typeof(ICommandAction), type: LogType.ERROR); source: typeof(ICommandAction), type: LogType.ERROR
);
} }
Console.ForegroundColor = cc; Console.ForegroundColor = cc;
@@ -185,12 +195,14 @@ internal static class PluginMethods
if (success) if (success)
{ {
Config.Logger.Log("Successfully loaded event : " + name, source: typeof(ICommandAction), Config.Logger.Log("Successfully loaded event : " + name, source: typeof(ICommandAction),
type: LogType.INFO); type: LogType.INFO
);
} }
else else
{ {
Config.Logger.Log("Failed to load event : " + name + " because " + exception?.Message, Config.Logger.Log("Failed to load event : " + name + " because " + exception?.Message,
source: typeof(ICommandAction), type: LogType.ERROR); source: typeof(ICommandAction), type: LogType.ERROR
);
} }
Console.ForegroundColor = cc; Console.ForegroundColor = cc;
@@ -204,12 +216,14 @@ internal static class PluginMethods
if (success) if (success)
{ {
Config.Logger.Log("Successfully loaded slash command : " + name, source: typeof(ICommandAction), Config.Logger.Log("Successfully loaded slash command : " + name, source: typeof(ICommandAction),
type: LogType.INFO); type: LogType.INFO
);
} }
else else
{ {
Config.Logger.Log("Failed to load slash command : " + name + " because " + exception?.Message, Config.Logger.Log("Failed to load slash command : " + name + " because " + exception?.Message,
source: typeof(ICommandAction), type: LogType.ERROR); source: typeof(ICommandAction), type: LogType.ERROR
);
} }
Console.ForegroundColor = cc; Console.ForegroundColor = cc;

View File

@@ -103,11 +103,13 @@ public class Program
if (AppSettings.ContainsKey("LaunchMessage")) if (AppSettings.ContainsKey("LaunchMessage"))
AppSettings.Add("LaunchMessage", AppSettings.Add("LaunchMessage",
"An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" + "An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" +
"There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !"); "There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !"
);
Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" + Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" +
"There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", "There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
source: typeof(Program), type: LogType.ERROR); source: typeof(Program), type: LogType.ERROR
);
} }
} }
} }

View File

@@ -70,7 +70,8 @@ public static class ConsoleUtilities
new TaskDescriptionColumn(), new TaskDescriptionColumn(),
new ProgressBarColumn(), new ProgressBarColumn(),
new PercentageColumn(), new PercentageColumn(),
}) }
)
.StartAsync(async ctx => .StartAsync(async ctx =>
{ {
var task = ctx.AddTask(message); var task = ctx.AddTask(message);
@@ -78,7 +79,8 @@ public static class ConsoleUtilities
await function; await function;
task.Increment(100); task.Increment(100);
}); }
);
} }
private static readonly Dictionary<char, ConsoleColor> Colors = new() private static readonly Dictionary<char, ConsoleColor> Colors = new()

View File

@@ -107,8 +107,10 @@ internal class CommandHandler
plug.Aliases is not null && plug.Aliases is not null &&
plug.Aliases.Contains(message.CleanContent plug.Aliases.Contains(message.CleanContent
.Substring(mentionPrefix.Length + 1) .Substring(mentionPrefix.Length + 1)
.Split(' ')[0]) .Split(' ')[0]
)); )
)
);
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1); cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
} }
@@ -121,7 +123,9 @@ internal class CommandHandler
(p.Aliases is not null && (p.Aliases is not null &&
p.Aliases.Contains( p.Aliases.Contains(
message.Content.Split(' ')[0] message.Content.Split(' ')[0]
.Substring(botPrefix.Length)))); .Substring(botPrefix.Length)
))
);
cleanMessage = message.Content.Substring(botPrefix.Length); cleanMessage = message.Content.Substring(botPrefix.Length);
} }

View File

@@ -23,15 +23,16 @@ public class Config
Directory.CreateDirectory("./Data/Resources"); Directory.CreateDirectory("./Data/Resources");
Directory.CreateDirectory("./Data/Plugins"); Directory.CreateDirectory("./Data/Plugins");
Directory.CreateDirectory("./Data/PAKS"); Directory.CreateDirectory("./Data/Archives");
Directory.CreateDirectory("./Data/Logs/Logs"); Directory.CreateDirectory("./Data/Logs");
Directory.CreateDirectory("./Data/Logs/Errors");
AppSettings = new SettingsDictionary<string, string>("./Data/Resources/config.json"); AppSettings = new SettingsDictionary<string, string>("./Data/Resources/config.json");
AppSettings["LogFolder"] = "./Data/Logs/Logs"; AppSettings["LogFolder"] = "./Data/Logs";
Logger = new Logger(false, true); Logger = new Logger(false, true,
AppSettings["LogFolder"] + $"/{DateTime.Today.ToShortDateString().Replace("/", "")}.log"
);
ArchiveManager.Initialize(); ArchiveManager.Initialize();

View File

@@ -163,7 +163,8 @@ public class SqlDatabase
throw new Exception($"Table {tableName} does not exist"); throw new Exception($"Table {tableName} does not exist");
await ExecuteAsync( await ExecuteAsync(
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'"); $"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'"
);
} }
/// <summary> /// <summary>

View File

@@ -6,7 +6,6 @@ namespace PluginManager.Interfaces.Logger;
internal interface ILog internal interface ILog
{ {
string Message { get; set; } string Message { get; set; }
string OutputFile { get; set; }
Type? Source { get; set; } Type? Source { get; set; }

View File

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

View File

@@ -122,7 +122,8 @@ internal class Loader
IsLoaded = false, IsLoaded = false,
PluginName = type.FullName, PluginName = type.FullName,
TypeName = nameof(T) TypeName = nameof(T)
}); }
);
} }
return list; return list;

View File

@@ -131,7 +131,8 @@ public class PluginLoader
builder.Options = slash.Options; builder.Options = slash.Options;
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded, onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded,
args.Exception); args.Exception
);
await _client.CreateGlobalApplicationCommandAsync(builder.Build()); await _client.CreateGlobalApplicationCommandAsync(builder.Build());
} }

View File

@@ -136,7 +136,8 @@ public class PluginsManager
{ {
var contents = lines[i].Split(','); var contents = lines[i].Split(',');
if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS && contents[4].Contains("Windows")) if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS && contents[4].Contains("Windows"))
{if (contents[0].ToLowerInvariant() == name.ToLowerInvariant()) {
if (contents[0].ToLowerInvariant() == name.ToLowerInvariant())
{ {
if (contents.Length == 6) if (contents.Length == 6)
return new[] { contents[2], contents[3], contents[5] }; return new[] { contents[2], contents[3], contents[5] };
@@ -144,7 +145,8 @@ public class PluginsManager
return new[] { contents[2], contents[3], string.Empty }; return new[] { contents[2], contents[3], string.Empty };
throw new Exception("Failed to download plugin. Invalid Argument Length"); throw new Exception("Failed to download plugin. Invalid Argument Length");
} }
}else if (Functions.GetOperatingSystem() == OperatingSystem.LINUX && contents[4].Contains("Linux")) }
else if (Functions.GetOperatingSystem() == OperatingSystem.LINUX && contents[4].Contains("Linux"))
{ {
if (contents.Length == 6) if (contents.Length == 6)
return new[] { contents[2], contents[3], contents[5] }; return new[] { contents[2], contents[3], contents[5] };

View File

@@ -7,51 +7,37 @@ namespace PluginManager.Others.Logger;
public class Log: ILog public class Log: ILog
{ {
public string Message { get; set; } public string Message { get; set; }
public string OutputFile { get; set; }
public Type? Source { get; set; } public Type? Source { get; set; }
public LogType Type { get; set; } public LogType Type { get; set; }
public DateTime ThrowTime { get; set; } public DateTime ThrowTime { get; set; }
public Log(string message, string outputFile, Type? source, LogType type, DateTime throwTime) public Log(string message, Type? source, LogType type, DateTime throwTime)
{ {
Message = message; Message = message;
OutputFile = outputFile;
Source = source; Source = source;
Type = type; Type = type;
ThrowTime = throwTime; ThrowTime = throwTime;
} }
public Log(string message, string outputFile, Type? source, LogType type) public Log(string message, Type? source, LogType type)
{ {
Message = message; Message = message;
OutputFile = outputFile;
Source = source; Source = source;
Type = type; Type = type;
ThrowTime = DateTime.Now; ThrowTime = DateTime.Now;
} }
public Log(string message, string outputFile, Type? source) public Log(string message, Type? source)
{ {
Message = message; Message = message;
OutputFile = outputFile;
Source = source; Source = source;
Type = LogType.INFO; Type = LogType.INFO;
ThrowTime = DateTime.Now; ThrowTime = DateTime.Now;
} }
public Log(string message, string outputFile)
{
Message = message;
OutputFile = outputFile;
Source = typeof(Log);
Type = LogType.INFO;
ThrowTime = DateTime.Now;
}
public Log(string message) public Log(string message)
{ {
Message = message; Message = message;
OutputFile = "";
Source = typeof(Log); Source = typeof(Log);
Type = LogType.INFO; Type = LogType.INFO;
ThrowTime = DateTime.Now; ThrowTime = DateTime.Now;

View File

@@ -10,16 +10,27 @@ public sealed class Logger : ILogger
{ {
public bool IsEnabled { get; init; } public bool IsEnabled { get; init; }
public bool OutputToFile { get; init; } public bool OutputToFile { get; init; }
public string? OutputFile { get; init; }
private LogType LowestLogLevel { get; } private LogType LowestLogLevel { get; }
private bool UseShortVersion { get; } private bool UseShortVersion { get; }
public Logger(bool useShortVersion, bool outputToFile, LogType lowestLogLevel = LogType.INFO) public Logger(bool useShortVersion, bool outputToFile, string outputFile, LogType lowestLogLevel = LogType.INFO)
{ {
UseShortVersion = useShortVersion; UseShortVersion = useShortVersion;
OutputToFile = outputToFile; OutputToFile = outputToFile;
IsEnabled = true; IsEnabled = true;
LowestLogLevel = lowestLogLevel; LowestLogLevel = lowestLogLevel;
OutputFile = outputFile;
}
public Logger(bool useShortVersion, LogType lowestLogLevel = LogType.INFO)
{
UseShortVersion = useShortVersion;
OutputToFile = false;
IsEnabled = true;
LowestLogLevel = lowestLogLevel;
OutputFile = null;
} }
public event EventHandler<Log>? OnLog; public event EventHandler<Log>? OnLog;
@@ -34,11 +45,12 @@ public sealed class Logger : ILogger
if (OutputToFile) if (OutputToFile)
await File.AppendAllTextAsync( await File.AppendAllTextAsync(
logMessage.OutputFile, OutputFile!,
(UseShortVersion ? logMessage : logMessage.AsLongString()) + "\n"); (UseShortVersion ? logMessage : logMessage.AsLongString()) + "\n"
);
} }
public async void Log(string message = "", string outputFile = "", Type? source = default, LogType type = LogType.INFO, DateTime throwTime = default) public async void Log(string message = "", Type? source = default, LogType type = LogType.INFO, DateTime throwTime = default)
{ {
if (!IsEnabled) return; if (!IsEnabled) return;
@@ -46,13 +58,11 @@ public sealed class Logger : ILogger
if (string.IsNullOrEmpty(message)) return; if (string.IsNullOrEmpty(message)) return;
if (string.IsNullOrEmpty(outputFile)) outputFile = Config.AppSettings["LogFolder"] + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
if (throwTime == default) throwTime = DateTime.Now; if (throwTime == default) throwTime = DateTime.Now;
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, source, type, throwTime));
} }
@@ -62,8 +72,6 @@ public sealed class Logger : ILogger
if (logType < LowestLogLevel) return; if (logType < LowestLogLevel) return;
await Log(new Log(exception.Message, await Log(new Log(exception.Message, source, logType, DateTime.Now));
Config.AppSettings["LogFolder"] + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log",
source, logType, DateTime.Now));
} }
} }