Actions are now loaded together with all plugins. Called the LoadPlugins at startup

This commit is contained in:
2024-06-08 19:17:15 +03:00
parent 9a8ddb5388
commit d9d5c05313
14 changed files with 148 additions and 236 deletions

View File

@@ -8,31 +8,36 @@ namespace DiscordBotCore.Others.Actions;
public class InternalActionManager
{
public Dictionary<string, ICommandAction> Actions = new();
private readonly ActionsLoader _loader;
public InternalActionManager(string path, string extension)
{
_loader = new ActionsLoader(path, extension);
}
private Dictionary<string, ICommandAction> Actions = new();
public async Task Initialize()
{
var loadedActions = await _loader.Load();
Actions.Clear();
PluginLoader.Actions.ForEach(action =>
{
if (action.RunType == InternalActionRunType.ON_CALL || action.RunType == InternalActionRunType.BOTH)
{
if (this.Actions.ContainsKey(action.ActionName))
return; // ingore duplicates
if (loadedActions == null)
return;
foreach (var action in loadedActions)
Actions.TryAdd(action.ActionName, action);
this.Actions.Add(action.ActionName, action);
}
});
}
public async Task Refresh()
public IReadOnlyCollection<ICommandAction> GetActions()
{
Actions.Clear();
await Initialize();
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)

View File

@@ -40,5 +40,6 @@ public enum PluginType
UNKNOWN,
COMMAND,
EVENT,
SLASH_COMMAND
SLASH_COMMAND,
ACTION
}

View File

@@ -1,83 +0,0 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Discord;
namespace DiscordBotCore.Others;
/// <summary>
/// A special class with functions
/// </summary>
public static class Functions
{
/// <summary>
/// The location for the Resources folder
/// String: ./Data/Resources/
/// </summary>
public static string dataFolder => Application.CurrentApplication.DataFolder;
public static Color RandomColor
{
get
{
var random = new Random();
return new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
}
/// <summary>
/// Copy one Stream to another <see langword="async" />
/// </summary>
/// <param name="stream">The base stream</param>
/// <param name="destination">The destination stream</param>
/// <param name="bufferSize">The buffer to read</param>
/// <param name="progress">The progress</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <exception cref="ArgumentNullException">Triggered if any <see cref="Stream" /> is empty</exception>
/// <exception cref="ArgumentOutOfRangeException">Triggered if <paramref name="bufferSize" /> is less then or equal to 0</exception>
/// <exception cref="InvalidOperationException">Triggered if <paramref name="stream" /> is not readable</exception>
/// <exception cref="ArgumentException">Triggered in <paramref name="destination" /> is not writable</exception>
public static async Task CopyToOtherStreamAsync(
this Stream stream, Stream destination, int bufferSize,
IProgress<long>? progress = null,
CancellationToken cancellationToken = default)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (bufferSize <= 0) throw new ArgumentOutOfRangeException(nameof(bufferSize));
if (!stream.CanRead) throw new InvalidOperationException("The stream is not readable.");
if (!destination.CanWrite)
throw new ArgumentException("Destination stream is not writable", nameof(destination));
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)
.ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
}
public static T SelectRandomValueOf<T>()
{
var enums = Enum.GetValues(typeof(T));
var random = new Random();
return (T)enums.GetValue(random.Next(enums.Length));
}
public static T RandomValue<T>(this T[] values)
{
Random random = new();
return values[random.Next(values.Length)];
}
public static string ToResourcesPath(this string path)
{
return Path.Combine(dataFolder, path);
}
}