Updated performance in plugin loading
This commit is contained in:
@@ -33,7 +33,8 @@ public class DbCommandExecutingArguments
|
||||
}
|
||||
else
|
||||
{
|
||||
CleanContent = message.Content.Substring(Application.CurrentApplication.DiscordBotClient.BotPrefix.Length);
|
||||
string? prefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("prefix");
|
||||
CleanContent = message.Content.Substring(prefix?.Length ?? 0);
|
||||
}
|
||||
|
||||
var split = CleanContent.Split(' ');
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace DiscordBotCore.Others.Exceptions
|
||||
{
|
||||
internal class PluginNotFoundException : Exception
|
||||
public class PluginNotFoundException : Exception
|
||||
{
|
||||
public PluginNotFoundException(string pluginName) : base($"Plugin {pluginName} was not found") { }
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
namespace DiscordBotCore.Others
|
||||
{
|
||||
@@ -132,4 +132,4 @@ namespace DiscordBotCore.Others
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
179
DiscordBotCore/Others/Option.cs
Normal file
179
DiscordBotCore/Others/Option.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
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"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
64
DiscordBotCore/Others/Result.cs
Normal file
64
DiscordBotCore/Others/Result.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
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 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 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);
|
||||
}
|
||||
}
|
||||
@@ -49,8 +49,21 @@ public class CustomSettingsDictionary : CustomSettingsDictionaryBase<string, obj
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user