luck perms discord initial
This commit is contained in:
27
LuckPerms.Torch.Discord/Utils/SedbGetters.cs
Normal file
27
LuckPerms.Torch.Discord/Utils/SedbGetters.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
using Torch.API.Managers;
|
||||
using Torch.API.Plugins;
|
||||
|
||||
namespace LuckPerms.Torch.Discord.Utils;
|
||||
|
||||
public static class SedbGetters
|
||||
{
|
||||
private static readonly Guid SedbId = new("3cd3ba7f-c47c-4efe-8cf1-bd3f618f5b9c");
|
||||
|
||||
public static bool IsSedbInstalled(this IPluginManager pluginManager) => pluginManager.Plugins.ContainsKey(SedbId);
|
||||
|
||||
public static string? GetSedbToken(this IPluginManager pluginManager)
|
||||
{
|
||||
var instance = GetConfigProp(pluginManager, out var configProp);
|
||||
var tokenProp = configProp.PropertyType.GetProperty("BotToken") ?? throw new InvalidOperationException("BotToken property not found");
|
||||
|
||||
return tokenProp.GetValue(configProp.GetValue(instance)) as string;
|
||||
}
|
||||
|
||||
private static ITorchPlugin GetConfigProp(IPluginManager pluginManager, out PropertyInfo configProp)
|
||||
{
|
||||
var instance = pluginManager.Plugins[SedbId];
|
||||
configProp = instance.GetType().GetProperty("Config") ?? throw new InvalidOperationException("Config property not found");
|
||||
return instance;
|
||||
}
|
||||
}
|
136
LuckPerms.Torch.Discord/Utils/TaskExtensions.cs
Normal file
136
LuckPerms.Torch.Discord/Utils/TaskExtensions.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using java.util.concurrent;
|
||||
|
||||
namespace LuckPerms.Torch.Discord.Utils;
|
||||
|
||||
public static class TaskExtensions
|
||||
{
|
||||
public static Future AsFuture(this Task task)
|
||||
{
|
||||
return new FutureAdapter(task);
|
||||
}
|
||||
|
||||
public static Future AsFuture<T>(this Task<T> task)
|
||||
{
|
||||
return new FutureAdapter<T>(task);
|
||||
}
|
||||
}
|
||||
|
||||
public class FutureAdapter<T>(Task<T> task) : Future
|
||||
{
|
||||
public bool cancel(bool mayInterruptIfRunning)
|
||||
{
|
||||
throw new InvalidOperationException("Task is not cancellable");
|
||||
}
|
||||
|
||||
public bool isCancelled() => task.IsCanceled;
|
||||
|
||||
public bool isDone() => task.Status is TaskStatus.RanToCompletion or TaskStatus.Faulted or TaskStatus.Canceled;
|
||||
|
||||
public object? get()
|
||||
{
|
||||
if (!isDone())
|
||||
try
|
||||
{
|
||||
task.Wait();
|
||||
}
|
||||
catch (AggregateException e) when (e.InnerExceptions is [ TaskCanceledException canceledException ])
|
||||
{
|
||||
throw new CancellationException(canceledException.Message);
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
|
||||
if (task.IsCanceled)
|
||||
throw new CancellationException();
|
||||
if (task.IsFaulted)
|
||||
throw new ExecutionException(task.Exception);
|
||||
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
public object? get(long timeout, TimeUnit unit)
|
||||
{
|
||||
if (!isDone())
|
||||
try
|
||||
{
|
||||
task.Wait(TimeSpan.FromMilliseconds(unit.toMillis(timeout)));
|
||||
} // TODO: InterruptedException and TimeoutException
|
||||
catch (AggregateException e) when (e.InnerExceptions is [ TaskCanceledException canceledException ])
|
||||
{
|
||||
throw new CancellationException(canceledException.Message);
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
|
||||
if (task.IsCanceled)
|
||||
throw new CancellationException();
|
||||
if (task.IsFaulted)
|
||||
throw new ExecutionException(task.Exception);
|
||||
|
||||
return task.Result;
|
||||
}
|
||||
}
|
||||
|
||||
public class FutureAdapter(Task task) : Future
|
||||
{
|
||||
public bool cancel(bool mayInterruptIfRunning)
|
||||
{
|
||||
throw new InvalidOperationException("Task is not cancellable");
|
||||
}
|
||||
|
||||
public bool isCancelled() => task.IsCanceled;
|
||||
|
||||
public bool isDone() => task.Status is TaskStatus.RanToCompletion or TaskStatus.Faulted or TaskStatus.Canceled;
|
||||
|
||||
public object? get()
|
||||
{
|
||||
if (!isDone())
|
||||
try
|
||||
{
|
||||
task.Wait();
|
||||
}
|
||||
catch (AggregateException e) when (e.InnerExceptions is [ TaskCanceledException canceledException ])
|
||||
{
|
||||
throw new CancellationException(canceledException.Message);
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
|
||||
if (task.IsCanceled)
|
||||
throw new CancellationException();
|
||||
if (task.IsFaulted)
|
||||
throw new ExecutionException(task.Exception);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public object? get(long timeout, TimeUnit unit)
|
||||
{
|
||||
if (!isDone())
|
||||
try
|
||||
{
|
||||
task.Wait(TimeSpan.FromMilliseconds(unit.toMillis(timeout)));
|
||||
} // TODO: InterruptedException and TimeoutException
|
||||
catch (AggregateException e) when (e.InnerExceptions is [ TaskCanceledException canceledException ])
|
||||
{
|
||||
throw new CancellationException(canceledException.Message);
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
|
||||
if (task.IsCanceled)
|
||||
throw new CancellationException();
|
||||
if (task.IsFaulted)
|
||||
throw new ExecutionException(task.Exception);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user