Redesigned the DiscordBotCore by splitting it into multiple projects. Created a WebUI and preparing to remove the DiscordBot application

This commit is contained in:
2025-04-04 22:07:30 +03:00
parent 62ba5ec63d
commit a4afb28f36
2290 changed files with 76694 additions and 17052 deletions

View File

@@ -1,26 +0,0 @@
using System.Collections.Generic;
namespace DiscordBotCore.Others.Actions
{
public class InternalActionOption
{
public string OptionName { get; set; }
public string OptionDescription { get; set; }
public List<InternalActionOption> SubOptions { get; set; }
public InternalActionOption(string optionName, string optionDescription, List<InternalActionOption> subOptions)
{
OptionName = optionName;
OptionDescription = optionDescription;
SubOptions = subOptions;
}
public InternalActionOption(string optionName, string optionDescription)
{
OptionName = optionName;
OptionDescription = optionDescription;
SubOptions = new List<InternalActionOption>();
}
}
}

View File

@@ -1,89 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Loaders;
namespace DiscordBotCore.Others.Actions;
public class InternalActionManager
{
private Dictionary<string, ICommandAction> Actions = new();
public async Task Initialize()
{
Actions.Clear();
PluginLoader.Actions.ForEach(action =>
{
if (action.RunType == InternalActionRunType.OnCall || action.RunType == InternalActionRunType.OnStartupAndCall)
{
if (this.Actions.ContainsKey(action.ActionName))
{
// This should never happen. If it does, log it and return
Application.CurrentApplication.Logger.Log($"Action {action.ActionName} already exists", this, LogType.Error);
return;
}
this.Actions.Add(action.ActionName, action);
}
});
}
public IReadOnlyCollection<ICommandAction> GetActions()
{
return Actions.Values;
}
public bool Exists(string actionName)
{
return Actions.ContainsKey(actionName);
}
public ICommandAction GetAction(string actionName)
{
return Actions[actionName];
}
public async Task<bool> Execute(string actionName, params string[]? args)
{
if (!Actions.ContainsKey(actionName))
{
Application.CurrentApplication.Logger.Log($"Action {actionName} not found", this, LogType.Error);
return false;
}
try
{
if (Actions[actionName].RunType == InternalActionRunType.OnStartup)
{
Application.CurrentApplication.Logger.Log($"Action {actionName} is not executable", this, LogType.Error);
return false;
}
await StartAction(Actions[actionName], args);
return true;
}
catch (Exception e)
{
Application.CurrentApplication.Logger.LogException(e, this);
return false;
}
}
public async Task StartAction(ICommandAction action, params string[]? args)
{
if (action.RequireOtherThread)
{
async void Start() => await action.Execute(args);
Thread thread = new(Start);
thread.Start();
}
else
{
await action.Execute(args);
}
}
}

View File

@@ -1,188 +0,0 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace DiscordBotCore.Others;
public static class ArchiveManager
{
private static readonly string _ArchivesFolder = "./Data/Archives";
public static void CreateFromFile(string file, string folder)
{
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
var archiveName = folder + Path.GetFileNameWithoutExtension(file) + ".zip";
if (File.Exists(archiveName))
File.Delete(archiveName);
using(ZipArchive archive = ZipFile.Open(archiveName, ZipArchiveMode.Create))
{
archive.CreateEntryFromFile(file, Path.GetFileName(file));
}
}
/// <summary>
/// Read a file from a zip archive. The output is a byte array
/// </summary>
/// <param name="fileName">The file name in the archive</param>
/// <param name="archName">The archive location on the disk</param>
/// <returns>An array of bytes that represents the Stream value from the file that was read inside the archive</returns>
public static async Task<byte[]?> ReadAllBytes(string fileName, string archName)
{
string? archiveFolderBasePath = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("ArchiveFolder", _ArchivesFolder);
if(archiveFolderBasePath is null)
throw new Exception("Archive folder not found");
Directory.CreateDirectory(archiveFolderBasePath);
archName = Path.Combine(archiveFolderBasePath, archName);
if (!File.Exists(archName))
throw new Exception("Failed to load file !");
using var zip = ZipFile.OpenRead(archName);
var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName);
if (entry is null) throw new Exception("File not found in archive");
await using var memoryStream = new MemoryStream();
var stream = entry.Open();
await stream.CopyToAsync(memoryStream);
var data = memoryStream.ToArray();
stream.Close();
memoryStream.Close();
Console.WriteLine("Read file from archive: " + fileName);
Console.WriteLine("Size: " + data.Length);
return data;
}
/// <summary>
/// Read data from a file that is inside an archive (ZIP format)
/// </summary>
/// <param name="fileName">The file name that is inside the archive or its full path</param>
/// <param name="archFile">The archive location from the PAKs folder</param>
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
public static async Task<string?> ReadFromPakAsync(string fileName, string archFile)
{
string? archiveFolderBasePath = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("ArchiveFolder", _ArchivesFolder);
if(archiveFolderBasePath is null)
throw new Exception("Archive folder not found");
Directory.CreateDirectory(archiveFolderBasePath);
archFile = Path.Combine(archiveFolderBasePath, archFile);
if (!File.Exists(archFile))
throw new Exception("Failed to load file !");
try
{
string? textValue = null;
using (var fs = new FileStream(archFile, FileMode.Open))
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
{
foreach (var entry in zip.Entries)
if (entry.Name == fileName || entry.FullName == fileName)
using (var s = entry.Open())
using (var reader = new StreamReader(s))
{
textValue = await reader.ReadToEndAsync();
reader.Close();
s.Close();
fs.Close();
}
}
return textValue;
}
catch (Exception ex)
{
Application.CurrentApplication.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.Error); // Write the error to a file
await Task.Delay(100);
return await ReadFromPakAsync(fileName, archFile);
}
}
/// <summary>
/// Extract zip to location
/// </summary>
/// <param name="zip">The zip location</param>
/// <param name="folder">The target location</param>
/// <param name="progress">The progress that is updated as a file is processed</param>
/// <param name="type">The type of progress</param>
/// <returns></returns>
public static async Task ExtractArchive(
string zip, string folder, IProgress<float> progress,
UnzipProgressType type)
{
Directory.CreateDirectory(folder);
using var archive = ZipFile.OpenRead(zip);
var totalZipFiles = archive.Entries.Count();
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
{
var currentZipFile = 0;
foreach (var entry in archive.Entries)
{
if (entry.FullName.EndsWith("/")) // it is a folder
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
else
try
{
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
}
catch (Exception ex)
{
Application.CurrentApplication.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.Error);
}
currentZipFile++;
await Task.Delay(10);
if (progress != null)
progress.Report((float)currentZipFile / totalZipFiles * 100);
}
}
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
{
ulong zipSize = 0;
foreach (var entry in archive.Entries)
zipSize += (ulong)entry.CompressedLength;
ulong currentSize = 0;
foreach (var entry in archive.Entries)
{
if (entry.FullName.EndsWith("/"))
{
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
continue;
}
try
{
string path = Path.Combine(folder, entry.FullName);
Directory.CreateDirectory(Path.GetDirectoryName(path));
entry.ExtractToFile(path, true);
currentSize += (ulong)entry.CompressedLength;
}
catch (Exception ex)
{
Application.CurrentApplication.Logger.Log(ex.Message, typeof(ArchiveManager), LogType.Error);
}
await Task.Delay(10);
if (progress != null)
progress.Report((float)currentSize / zipSize * 100);
}
}
}
}

View File

@@ -1,49 +0,0 @@
using Discord.Commands;
using Discord.WebSocket;
namespace DiscordBotCore.Others;
public class DbCommandExecutingArguments
{
public SocketCommandContext Context { get; init; }
public string CleanContent { get; init; }
public string CommandUsed { get; init; }
public string[]? Arguments { get; init; }
public ISocketMessageChannel Channel => Context.Channel;
public DbCommandExecutingArguments(
SocketCommandContext context, string cleanContent, string commandUsed, string[]? arguments)
{
this.Context = context;
this.CleanContent = cleanContent;
this.CommandUsed = commandUsed;
this.Arguments = arguments;
}
public DbCommandExecutingArguments(SocketUserMessage? message, DiscordSocketClient client)
{
Context = new SocketCommandContext(client, message);
var pos = 0;
if (message.HasMentionPrefix(client.CurrentUser, ref pos))
{
var mentionPrefix = "<@" + client.CurrentUser.Id + ">";
CleanContent = message.Content.Substring(mentionPrefix.Length + 1);
}
else
{
string? prefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("prefix");
CleanContent = message.Content.Substring(prefix?.Length ?? 0);
}
var split = CleanContent.Split(' ');
string[]? argsClean = null;
if (split.Length > 1)
argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' ');
CommandUsed = split[0];
Arguments = argsClean;
}
}

View File

@@ -1,12 +1,9 @@
using System.Linq;
using System.Linq;
using Discord;
using Discord.WebSocket;
namespace DiscordBotCore.Others.Permissions;
namespace DiscordBotCore.Others;
/// <summary>
/// A class whith all discord permissions
/// </summary>
public static class DiscordPermissions
{
/// <summary>
@@ -15,7 +12,7 @@ public static class DiscordPermissions
/// <param name="role">The role</param>
/// <param name="permission">The permission</param>
/// <returns></returns>
public static bool hasPermission(this IRole role, GuildPermission permission)
public static bool HasPermission(this IRole role, GuildPermission permission)
{
return role.Permissions.Has(permission);
}
@@ -30,7 +27,6 @@ public static class DiscordPermissions
{
return user.Roles.Contains(role);
}
/// <summary>
/// Check if user has the specified permission
/// </summary>
@@ -39,7 +35,7 @@ public static class DiscordPermissions
/// <returns></returns>
public static bool HasPermission(this SocketGuildUser user, GuildPermission permission)
{
return user.Roles.Where(role => role.hasPermission(permission)).Any() || user.Guild.Owner == user;
return user.Roles.Any(role => role.HasPermission(permission)) || user.Guild.Owner == user;
}
/// <summary>
@@ -61,4 +57,4 @@ public static class DiscordPermissions
{
return IsAdmin((SocketGuildUser)user);
}
}
}

View File

@@ -1,36 +0,0 @@
using System;
namespace DiscordBotCore.Others;
/// <summary>
/// The output log type. This must be used by other loggers in order to provide logging information
/// </summary>
public enum LogType
{
Info,
Warning,
Error,
Critical
}
public enum UnzipProgressType
{
PERCENTAGE_FROM_NUMBER_OF_FILES,
PERCENTAGE_FROM_TOTAL_SIZE
}
public enum InternalActionRunType
{
OnStartup,
OnCall,
OnStartupAndCall
}
public enum PluginType
{
UNKNOWN,
COMMAND,
EVENT,
SLASH_COMMAND,
ACTION
}

View File

@@ -1,18 +0,0 @@
using System;
namespace DiscordBotCore.Others.Exceptions;
public class DependencyNotFoundException : Exception
{
private string PluginName { get; set; }
public DependencyNotFoundException(string message): base(message)
{
}
public DependencyNotFoundException(string message, string pluginName): base(message)
{
this.PluginName = pluginName;
}
}

View File

@@ -1,12 +0,0 @@
using System;
namespace DiscordBotCore.Others.Exceptions
{
public class PluginNotFoundException : Exception
{
public PluginNotFoundException(string pluginName) : base($"Plugin {pluginName} was not found") { }
public PluginNotFoundException(string pluginName, string url, string branch) :
base ($"Plugin {pluginName} was not found on {url} (branch: {branch}") { }
}
}

View File

@@ -1,107 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace DiscordBotCore.Others;
public static class JsonManager
{
public static async Task<string> ConvertToJson<T>(List<T> data, string[] propertyNamesToUse)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (propertyNamesToUse == null) throw new ArgumentNullException(nameof(propertyNamesToUse));
// Use reflection to filter properties dynamically
var filteredData = data.Select(item =>
{
if (item == null) return null;
var type = typeof(T);
var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Create a dictionary with specified properties and their values
var selectedProperties = propertyInfos
.Where(p => propertyNamesToUse.Contains(p.Name))
.ToDictionary(p => p.Name, p => p.GetValue(item));
return selectedProperties;
}).ToList();
// Serialize the filtered data to JSON
var options = new JsonSerializerOptions
{
WriteIndented = true, // For pretty-print JSON; remove if not needed
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
return await Task.FromResult(JsonSerializer.Serialize(filteredData, options));
}
public static async Task<string> ConvertToJsonString<T>(T Data)
{
var str = new MemoryStream();
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions
{
WriteIndented = false,
});
var result = Encoding.ASCII.GetString(str.ToArray());
await str.FlushAsync();
str.Close();
return result;
}
/// <summary>
/// Save to JSON file
/// </summary>
/// <typeparam name="T">The class type</typeparam>
/// <param name="file">The file path</param>
/// <param name="Data">The values</param>
/// <returns></returns>
public static async Task SaveToJsonFile<T>(string file, T Data)
{
var str = new MemoryStream();
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions
{
WriteIndented = true,
}
);
await File.WriteAllBytesAsync(file, str.ToArray());
await str.FlushAsync();
str.Close();
}
/// <summary>
/// Convert json text or file to some kind of data
/// </summary>
/// <typeparam name="T">The data type</typeparam>
/// <param name="input">The file or json text</param>
/// <returns></returns>
public static async Task<T> ConvertFromJson<T>(string input)
{
Stream text;
if (File.Exists(input))
text = new MemoryStream(await File.ReadAllBytesAsync(input));
else
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
text.Position = 0;
JsonSerializerOptions options = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
};
var obj = await JsonSerializer.DeserializeAsync<T>(text, options);
await text.FlushAsync();
text.Close();
return (obj ?? default)!;
}
}

View File

@@ -1,48 +0,0 @@
using System;
namespace DiscordBotCore.Others;
public class OS
{
public enum OperatingSystem : int
{
Windows = 0,
Linux = 1,
MacOS = 2
}
public static OperatingSystem GetOperatingSystem()
{
if(System.OperatingSystem.IsLinux()) return OperatingSystem.Linux;
if(System.OperatingSystem.IsWindows()) return OperatingSystem.Windows;
if(System.OperatingSystem.IsMacOS()) return OperatingSystem.MacOS;
throw new PlatformNotSupportedException();
}
public static string GetOperatingSystemString(OperatingSystem os)
{
return os switch
{
OperatingSystem.Windows => "Windows",
OperatingSystem.Linux => "Linux",
OperatingSystem.MacOS => "MacOS",
_ => throw new ArgumentOutOfRangeException()
};
}
public static OperatingSystem GetOperatingSystemFromString(string os)
{
return os.ToLower() switch
{
"windows" => OperatingSystem.Windows,
"linux" => OperatingSystem.Linux,
"macos" => OperatingSystem.MacOS,
_ => throw new ArgumentOutOfRangeException()
};
}
public static int GetOperatingSystemInt()
{
return (int) GetOperatingSystem();
}
}

View File

@@ -1,135 +0,0 @@
using System;
namespace DiscordBotCore.Others
{
public class OneOf<T0, T1>
{
public T0 Item0 { get; }
public T1 Item1 { get; }
public object? Value => Item0 != null ? Item0 : Item1;
public OneOf(T0 item0)
{
Item0 = item0;
}
public OneOf(T1 item1)
{
Item1 = item1;
}
public static implicit operator OneOf<T0, T1>(T0 item0) => new OneOf<T0, T1>(item0);
public static implicit operator OneOf<T0, T1>(T1 item1) => new OneOf<T0, T1>(item1);
public void Match(Action<T0> item0, Action<T1> item1)
{
if (Item0 != null)
item0(Item0);
else
item1(Item1);
}
public TResult Match<TResult>(Func<T0, TResult> item0, Func<T1, TResult> item1)
{
return Item0 != null ? item0(Item0) : item1(Item1);
}
public Type GetActualType()
{
return Item0 != null ? Item0.GetType() : Item1.GetType();
}
}
public class OneOf<T0, T1, T2>
{
public T0 Item0 { get; }
public T1 Item1 { get; }
public T2 Item2 { get; }
public OneOf(T0 item0)
{
Item0 = item0;
}
public OneOf(T1 item1)
{
Item1 = item1;
}
public OneOf(T2 item2)
{
Item2 = item2;
}
public static implicit operator OneOf<T0, T1, T2>(T0 item0) => new OneOf<T0, T1, T2>(item0);
public static implicit operator OneOf<T0, T1, T2>(T1 item1) => new OneOf<T0, T1, T2>(item1);
public static implicit operator OneOf<T0, T1, T2>(T2 item2) => new OneOf<T0, T1, T2>(item2);
public void Match(Action<T0> item0, Action<T1> item1, Action<T2> item2)
{
if (Item0 != null)
item0(Item0);
else if (Item1 != null)
item1(Item1);
else
item2(Item2);
}
public TResult Match<TResult>(Func<T0, TResult> item0, Func<T1, TResult> item1, Func<T2, TResult> item2)
{
return Item0 != null ? item0(Item0) : Item1 != null ? item1(Item1) : item2(Item2);
}
}
public class OneOf<T0, T1, T2, T3>
{
public T0 Item0 { get; }
public T1 Item1 { get; }
public T2 Item2 { get; }
public T3 Item3 { get; }
public OneOf(T0 item0)
{
Item0 = item0;
}
public OneOf(T1 item1)
{
Item1 = item1;
}
public OneOf(T2 item2)
{
Item2 = item2;
}
public OneOf(T3 item3)
{
Item3 = item3;
}
public static implicit operator OneOf<T0, T1, T2, T3>(T0 item0) => new OneOf<T0, T1, T2, T3>(item0);
public static implicit operator OneOf<T0, T1, T2, T3>(T1 item1) => new OneOf<T0, T1, T2, T3>(item1);
public static implicit operator OneOf<T0, T1, T2, T3>(T2 item2) => new OneOf<T0, T1, T2, T3>(item2);
public static implicit operator OneOf<T0, T1, T2, T3>(T3 item3) => new OneOf<T0, T1, T2, T3>(item3);
public void Match(Action<T0> item0, Action<T1> item1, Action<T2> item2, Action<T3> item3)
{
if (Item0 != null)
item0(Item0);
else if (Item1 != null)
item1(Item1);
else if (Item2 != null)
item2(Item2);
else
item3(Item3);
}
public TResult Match<TResult>(Func<T0, TResult> item0, Func<T1, TResult> item1, Func<T2, TResult> item2, Func<T3, TResult> item3)
{
return Item0 != null ? item0(Item0) : Item1 != null ? item1(Item1) : Item2 != null ? item2(Item2) : item3(Item3);
}
}
}

View File

@@ -1,179 +0,0 @@
using System;
namespace DiscordBotCore.Others;
public class Option2<T0, T1, TError> where TError : Exception
{
private readonly int _Index;
private T0 Item0 { get; } = default!;
private T1 Item1 { get; } = default!;
private TError Error { get; } = default!;
private Option2(T0 item0)
{
Item0 = item0;
_Index = 0;
}
private Option2(T1 item1)
{
Item1 = item1;
_Index = 1;
}
private Option2(TError error)
{
Error = error;
_Index = 2;
}
public static implicit operator Option2<T0, T1, TError>(T0 item0) => new Option2<T0, T1, TError>(item0);
public static implicit operator Option2<T0, T1, TError>(T1 item1) => new Option2<T0, T1, TError>(item1);
public static implicit operator Option2<T0, T1, TError>(TError error) => new Option2<T0, T1, TError>(error);
public void Match(Action<T0> item0, Action<T1> item1, Action<TError> error)
{
switch (_Index)
{
case 0:
item0(Item0);
break;
case 1:
item1(Item1);
break;
case 2:
error(Error);
break;
default:
throw new InvalidOperationException();
}
}
public TResult Match<TResult>(Func<T0, TResult> item0, Func<T1, TResult> item1, Func<TError, TResult> error)
{
return _Index switch
{
0 => item0(Item0),
1 => item1(Item1),
2 => error(Error),
_ => throw new InvalidOperationException(),
};
}
public override string ToString()
{
return _Index switch
{
0 => $"Option2<{typeof(T0).Name}>: {Item0}",
1 => $"Option2<{typeof(T1).Name}>: {Item1}",
2 => $"Option2<{typeof(TError).Name}>: {Error}",
_ => "Invalid Option2"
};
}
}
public class Option4<T0, T1, T2, T3, TError> where TError : Exception
{
private readonly int _Index;
private T0 Item0 { get; } = default!;
private T1 Item1 { get; } = default!;
private T2 Item2 { get; } = default!;
private T3 Item3 { get; } = default!;
private TError Error { get; } = default!;
internal Option4(T0 item0)
{
Item0 = item0;
_Index = 0;
}
internal Option4(T1 item1)
{
Item1 = item1;
_Index = 1;
}
internal Option4(T2 item2)
{
Item2 = item2;
_Index = 2;
}
internal Option4(T3 item3)
{
Item3 = item3;
_Index = 3;
}
internal Option4(TError error)
{
Error = error;
_Index = 4;
}
public static implicit operator Option4<T0, T1, T2, T3, TError>(T0 item0) => new Option4<T0, T1, T2, T3, TError>(item0);
public static implicit operator Option4<T0, T1, T2, T3, TError>(T1 item1) => new Option4<T0, T1, T2, T3, TError>(item1);
public static implicit operator Option4<T0, T1, T2, T3, TError>(T2 item2) => new Option4<T0, T1, T2, T3, TError>(item2);
public static implicit operator Option4<T0, T1, T2, T3, TError>(T3 item3) => new Option4<T0, T1, T2, T3, TError>(item3);
public static implicit operator Option4<T0, T1, T2, T3, TError>(TError error) => new Option4<T0, T1, T2, T3, TError>(error);
public void Match(Action<T0> item0, Action<T1> item1, Action<T2> item2, Action<T3> item3, Action<TError> error)
{
switch (_Index)
{
case 0:
item0(Item0);
break;
case 1:
item1(Item1);
break;
case 2:
item2(Item2);
break;
case 3:
item3(Item3);
break;
case 4:
error(Error);
break;
default:
throw new InvalidOperationException();
}
}
public TResult Match<TResult>(Func<T0, TResult> item0, Func<T1, TResult> item1, Func<T2, TResult> item2, Func<T3, TResult> item3, Func<TError, TResult> error)
{
return _Index switch
{
0 => item0(Item0),
1 => item1(Item1),
2 => item2(Item2),
3 => item3(Item3),
4 => error(Error),
_ => throw new InvalidOperationException(),
};
}
public override string ToString()
{
return _Index switch
{
0 => $"Option4<{typeof(T0).Name}>: {Item0}",
1 => $"Option4<{typeof(T1).Name}>: {Item1}",
2 => $"Option4<{typeof(T2).Name}>: {Item2}",
3 => $"Option4<{typeof(T3).Name}>: {Item3}",
4 => $"Option4<{typeof(TError).Name}>: {Error}",
_ => "Invalid Option4"
};
}
}

View File

@@ -1,82 +0,0 @@
using System;
namespace DiscordBotCore.Others;
public class Result
{
private bool? _Result;
private Exception? Exception { get; }
private Result(Exception exception)
{
_Result = null;
Exception = exception;
}
private Result(bool result)
{
_Result = result;
Exception = null;
}
public bool IsSuccess => _Result.HasValue && _Result.Value;
public void HandleException(Action<Exception> action)
{
if(IsSuccess)
{
return;
}
action(Exception!);
}
public static Result Success() => new Result(true);
public static Result Failure(Exception ex) => new Result(ex);
public static Result Failure(string message) => new Result(new Exception(message));
public void Match(Action successAction, Action<Exception> exceptionAction)
{
if (_Result.HasValue && _Result.Value)
{
successAction();
}
else
{
exceptionAction(Exception!);
}
}
public TResult Match<TResult>(Func<TResult> successAction, Func<Exception,TResult> errorAction)
{
return IsSuccess ? successAction() : errorAction(Exception!);
}
}
public class Result<T>
{
private readonly OneOf<T, Exception> _Result;
private Result(OneOf<T, Exception> result)
{
_Result = result;
}
public static Result<T> From (T value) => new Result<T>(new OneOf<T, Exception>(value));
public static implicit operator Result<T>(Exception exception) => new Result<T>(new OneOf<T, Exception>(exception));
public void Match(Action<T> valueAction, Action<Exception> exceptionAction)
{
_Result.Match(valueAction, exceptionAction);
}
public TResult Match<TResult>(Func<T, TResult> valueFunc, Func<Exception, TResult> exceptionFunc)
{
return _Result.Match(valueFunc, exceptionFunc);
}
}

View File

@@ -1,118 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DiscordBotCore.Others.Settings;
public class CustomSettingsDictionary : CustomSettingsDictionaryBase<string, object>
{
private bool _EnableAutoAddOnGetWithDefault;
private CustomSettingsDictionary(string diskLocation, bool enableAutoAddOnGetWithDefault): base(diskLocation)
{
_EnableAutoAddOnGetWithDefault = enableAutoAddOnGetWithDefault;
}
public override async Task SaveToFile()
{
var json = JsonConvert.SerializeObject(_InternalDictionary, Formatting.Indented);
await File.WriteAllTextAsync(_DiskLocation, json);
}
public override T Get<T>(string key, T defaultValue)
{
T value = base.Get(key, defaultValue);
if (_EnableAutoAddOnGetWithDefault && value.Equals(defaultValue))
{
Add(key, defaultValue);
}
return value;
}
public override List<T> GetList<T>(string key, List<T> defaultValue)
{
List<T> value = base.GetList(key, defaultValue);
if (_EnableAutoAddOnGetWithDefault && value.All(defaultValue.Contains))
{
Add(key, defaultValue);
}
return value;
}
public override async Task LoadFromFile()
{
if (!File.Exists(_DiskLocation))
{
await SaveToFile();
return;
}
string jsonContent = await File.ReadAllTextAsync(_DiskLocation);
var jObject = JsonConvert.DeserializeObject<JObject>(jsonContent);
if (jObject is null)
{
await SaveToFile();
return;
}
_InternalDictionary.Clear();
foreach (var kvp in jObject)
{
AddPairToDictionary(kvp, _InternalDictionary);
}
}
private void AddPairToDictionary(KeyValuePair<string, JToken> kvp, IDictionary<string, object> dict)
{
if (kvp.Value is JObject nestedJObject)
{
dict[kvp.Key] = nestedJObject.ToObject<Dictionary<string, object>>();
foreach (var nestedKvp in nestedJObject)
{
AddPairToDictionary(nestedKvp, dict[kvp.Key] as Dictionary<string, object>);
}
}
else if (kvp.Value is JArray nestedJArray)
{
dict[kvp.Key] = nestedJArray.ToObject<List<object>>();
}
else
{
if (kvp.Value.Type == JTokenType.Integer)
dict[kvp.Key] = kvp.Value.Value<int>();
else if (kvp.Value.Type == JTokenType.Float)
dict[kvp.Key] = kvp.Value.Value<float>();
else if (kvp.Value.Type == JTokenType.Boolean)
dict[kvp.Key] = kvp.Value.Value<bool>();
else if (kvp.Value.Type == JTokenType.String)
dict[kvp.Key] = kvp.Value.Value<string>();
else if (kvp.Value.Type == JTokenType.Date)
dict[kvp.Key] = kvp.Value.Value<DateTime>();
else
dict[kvp.Key] = kvp.Value;
}
}
/// <summary>
/// Create a new Settings Dictionary from a file
/// </summary>
/// <param name="baseFile">The file location</param>
/// <param name="enableAutoAddOnGetWithDefault">Set this to true if you want to update the dictionary with default values on get</param>
internal static async Task<CustomSettingsDictionary> CreateFromFile(string baseFile, bool enableAutoAddOnGetWithDefault)
{
var settings = new CustomSettingsDictionary(baseFile, enableAutoAddOnGetWithDefault);
await settings.LoadFromFile();
return settings;
}
}

View File

@@ -1,167 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DiscordBotCore.Others.Settings;
public abstract class CustomSettingsDictionaryBase<TKey,TValue> : ICustomSettingsDictionary<TKey,TValue>
{
protected readonly IDictionary<TKey,TValue> _InternalDictionary = new Dictionary<TKey, TValue>();
protected readonly string _DiskLocation;
protected CustomSettingsDictionaryBase(string diskLocation)
{
this._DiskLocation = diskLocation;
}
public virtual void Add(TKey key, TValue value)
{
if (_InternalDictionary.ContainsKey(key))
return;
if (value is null)
return;
_InternalDictionary.Add(key, value);
}
public virtual void Set(TKey key, TValue value)
{
_InternalDictionary[key] = value;
}
public virtual TValue Get(TKey key)
{
return _InternalDictionary[key];
}
public virtual T Get<T>(TKey key, T defaultValue)
{
if (_InternalDictionary.TryGetValue(key, out var value))
{
return (T)Convert.ChangeType(value, typeof(T));
}
return defaultValue;
}
public virtual T? Get<T>(TKey key)
{
if (_InternalDictionary.TryGetValue(key, out var value))
{
return (T)Convert.ChangeType(value, typeof(T));
}
return default;
}
public virtual IDictionary<TSubKey, TSubValue> GetDictionary<TSubKey, TSubValue>(TKey key)
{
if (_InternalDictionary.TryGetValue(key, out var value))
{
if (value is not IDictionary)
{
throw new Exception("The value is not a dictionary");
}
var dictionary = new Dictionary<TSubKey, TSubValue>();
foreach (DictionaryEntry item in (IDictionary)value)
{
dictionary.Add((TSubKey)Convert.ChangeType(item.Key, typeof(TSubKey)), (TSubValue)Convert.ChangeType(item.Value, typeof(TSubValue)));
}
return dictionary;
}
return new Dictionary<TSubKey, TSubValue>();
}
public virtual List<T> GetList<T>(TKey key, List<T> defaultValue)
{
if(_InternalDictionary.TryGetValue(key, out var value))
{
if (value is not IList)
{
throw new Exception("The value is not a list");
}
var list = new List<T>();
foreach (object? item in (IList)value)
{
list.Add((T)Convert.ChangeType(item, typeof(T)));
}
return list;
}
Application.CurrentApplication.Logger.Log($"Key '{key}' not found in settings dictionary. Adding default value.", LogType.Warning);
return defaultValue;
}
public virtual void Remove(TKey key)
{
_InternalDictionary.Remove(key);
}
public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _InternalDictionary.GetEnumerator();
}
public virtual void Clear()
{
_InternalDictionary.Clear();
}
public virtual bool ContainsKey(TKey key)
{
return _InternalDictionary.ContainsKey(key);
}
public virtual IEnumerable<KeyValuePair<TKey, TValue>> Where(Func<KeyValuePair<TKey, TValue>, bool> predicate)
{
return _InternalDictionary.Where(predicate);
}
public virtual IEnumerable<KeyValuePair<TKey, TValue>> Where(Func<KeyValuePair<TKey, TValue>, int, bool> predicate)
{
return _InternalDictionary.Where(predicate);
}
public virtual IEnumerable<TResult> Where<TResult>(Func<KeyValuePair<TKey, TValue>, TResult> selector)
{
return _InternalDictionary.Select(selector);
}
public virtual IEnumerable<TResult> Where<TResult>(Func<KeyValuePair<TKey, TValue>, int, TResult> selector)
{
return _InternalDictionary.Select(selector);
}
public virtual KeyValuePair<TKey, TValue> FirstOrDefault(Func<KeyValuePair<TKey, TValue>, bool> predicate)
{
return _InternalDictionary.FirstOrDefault(predicate);
}
public virtual KeyValuePair<TKey, TValue> FirstOrDefault()
{
return _InternalDictionary.FirstOrDefault();
}
public virtual bool ContainsAllKeys(params TKey[] keys)
{
return keys.All(ContainsKey);
}
public virtual bool TryGetValue(TKey key, out TValue? value)
{
return _InternalDictionary.TryGetValue(key, out value);
}
public abstract Task SaveToFile();
public abstract Task LoadFromFile();
}

View File

@@ -1,136 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DiscordBotCore.Others.Settings;
internal interface ICustomSettingsDictionary<TKey,TValue>
{
/// <summary>
/// Adds an element to the custom settings dictionary
/// </summary>
/// <param name="key">The key</param>
/// <param name="value">The value</param>
void Add(TKey key, TValue value);
/// <summary>
/// Sets the value of a key in the custom settings dictionary
/// </summary>
/// <param name="key">The key</param>
/// <param name="value">The value</param>
void Set(TKey key, TValue value);
/// <summary>
/// Gets the value of a key in the custom settings dictionary. If the T type is different then the TValue type, it will try to convert it.
/// </summary>
/// <param name="key">The key</param>
/// <param name="defaultValue">The default value to be returned if the searched value is not found</param>
/// <typeparam name="T">The type of the returned value</typeparam>
/// <returns></returns>
T Get<T>(TKey key, T defaultValue);
/// <summary>
/// Gets the value of a key in the custom settings dictionary. If the T type is different then the TValue type, it will try to convert it.
/// </summary>
/// <param name="key">The key</param>
/// <typeparam name="T">The type of the returned value</typeparam>
/// <returns></returns>
T? Get<T>(TKey key);
/// <summary>
/// Get a list of values from the custom settings dictionary
/// </summary>
/// <param name="key">The key</param>
/// <param name="defaultValue">The default list to be returned if nothing is found</param>
/// <typeparam name="T">The type of the returned value</typeparam>
/// <returns></returns>
List<T> GetList<T>(TKey key, List<T> defaultValue);
/// <summary>
/// Remove a key from the custom settings dictionary
/// </summary>
/// <param name="key">The key</param>
void Remove(TKey key);
/// <summary>
/// Get the enumerator of the custom settings dictionary
/// </summary>
/// <returns></returns>
IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
/// <summary>
/// Clear the custom settings dictionary
/// </summary>
void Clear();
/// <summary>
/// Check if the custom settings dictionary contains a key
/// </summary>
/// <param name="key">The key</param>
/// <returns></returns>
bool ContainsKey(TKey key);
/// <summary>
/// Filter the custom settings dictionary based on a predicate
/// </summary>
/// <param name="predicate">The predicate</param>
/// <returns></returns>
IEnumerable<KeyValuePair<TKey, TValue>> Where(Func<KeyValuePair<TKey, TValue>, bool> predicate);
/// <summary>
/// Filter the custom settings dictionary based on a predicate
/// </summary>
/// <param name="predicate">The predicate</param>
IEnumerable<KeyValuePair<TKey, TValue>> Where(Func<KeyValuePair<TKey, TValue>, int, bool> predicate);
/// <summary>
/// Filter the custom settings dictionary based on a predicate
/// </summary>
/// <param name="selector">The predicate</param>
IEnumerable<TResult> Where<TResult>(Func<KeyValuePair<TKey, TValue>, TResult> selector);
/// <summary>
/// Filter the custom settings dictionary based on a predicate
/// </summary>
/// <param name="selector">The predicate</param>
IEnumerable<TResult> Where<TResult>(Func<KeyValuePair<TKey, TValue>, int, TResult> selector);
/// <summary>
/// Get the first element of the custom settings dictionary based on a predicate
/// </summary>
/// <param name="predicate">The predicate</param>
KeyValuePair<TKey, TValue> FirstOrDefault(Func<KeyValuePair<TKey, TValue>, bool> predicate);
/// <summary>
/// Get the first element of the custom settings dictionary
/// </summary>
/// <returns></returns>
KeyValuePair<TKey, TValue> FirstOrDefault();
/// <summary>
/// Checks if the custom settings dictionary contains all the keys
/// </summary>
/// <param name="keys">A list of keys</param>
/// <returns></returns>
bool ContainsAllKeys(params TKey[] keys);
/// <summary>
/// Try to get the value of a key in the custom settings dictionary
/// </summary>
/// <param name="key">The key</param>
/// <param name="value">The value</param>
/// <returns></returns>
bool TryGetValue(TKey key, out TValue? value);
/// <summary>
/// Save the custom settings dictionary to a file
/// </summary>
/// <returns></returns>
Task SaveToFile();
/// <summary>
/// Load the custom settings dictionary from a file
/// </summary>
/// <returns></returns>
Task LoadFromFile();
}