Reimplemented Command Actions.
This commit is contained in:
62
DiscordBot/Discord/Actions/Help.cs
Normal file
62
DiscordBot/Discord/Actions/Help.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using PluginManager.Others;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
|
||||||
|
namespace DiscordBot.Discord.Actions
|
||||||
|
{
|
||||||
|
public class Help : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "help";
|
||||||
|
|
||||||
|
public string Description => "Shows the list of commands and their usage";
|
||||||
|
|
||||||
|
public string Usage => "help [command]";
|
||||||
|
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
if (args == null || args.Length == 0)
|
||||||
|
{
|
||||||
|
var items = new List<string[]>
|
||||||
|
{
|
||||||
|
new[] { "-", "-", "-" },
|
||||||
|
new[] { "Command", "Usage", "Description" },
|
||||||
|
new[] { "-", "-", "-" }
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var a in Program.internalActionManager.Actions)
|
||||||
|
{
|
||||||
|
items.Add(new[] { a.Key, a.Value.Usage, a.Value.Description });
|
||||||
|
}
|
||||||
|
|
||||||
|
items.Add(new[] { "-", "-", "-" });
|
||||||
|
|
||||||
|
DiscordBot.Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Program.internalActionManager.Actions.ContainsKey(args[0]))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Command not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var action = Program.internalActionManager.Actions[args[0]];
|
||||||
|
var actionData = new List<string[]>
|
||||||
|
{
|
||||||
|
new[] { "-", "-", "-" },
|
||||||
|
new[] { "Command", "Usage", "Description" },
|
||||||
|
new[] { "-", "-", "-"},
|
||||||
|
new[] { action.ActionName, action.Usage, action.Description },
|
||||||
|
new[] { "-", "-", "-" }
|
||||||
|
};
|
||||||
|
|
||||||
|
DiscordBot.Utilities.Utilities.FormatAndAlignTable(actionData, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
DiscordBot/Discord/Actions/ListPlugins.cs
Normal file
49
DiscordBot/Discord/Actions/ListPlugins.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace DiscordBot.Discord.Actions
|
||||||
|
{
|
||||||
|
public class ListPlugins : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "listplugs";
|
||||||
|
|
||||||
|
public string Description => "Lists all plugins";
|
||||||
|
|
||||||
|
public string Usage => "listplugs";
|
||||||
|
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
|
||||||
|
var manager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
if (manager == null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Plugin manager is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var data = await manager.GetAvailablePlugins();
|
||||||
|
var items = new List<string[]>
|
||||||
|
{
|
||||||
|
new[] { "-", "-", "-", "-" },
|
||||||
|
new[] { "Name", "Type", "Description", "Required" },
|
||||||
|
new[] { "-", "-", "-", "-" }
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var plugin in data)
|
||||||
|
{
|
||||||
|
items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
||||||
|
}
|
||||||
|
|
||||||
|
items.Add(new[] { "-", "-", "-", "-" });
|
||||||
|
|
||||||
|
DiscordBot.Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.DEFAULT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
94
DiscordBot/Discord/Actions/LoadPlugins.cs
Normal file
94
DiscordBot/Discord/Actions/LoadPlugins.cs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Loaders;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace DiscordBot.Discord.Actions
|
||||||
|
{
|
||||||
|
public class LoadPlugins : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "loadplugs";
|
||||||
|
|
||||||
|
public string Description => "Loads all plugins";
|
||||||
|
|
||||||
|
public string Usage => "loadplugs";
|
||||||
|
|
||||||
|
private bool pluginsLoaded = false;
|
||||||
|
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_STARTUP;
|
||||||
|
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
if (pluginsLoaded)
|
||||||
|
return;
|
||||||
|
var loader = new PluginLoader(PluginManager.Config.DiscordBot.client);
|
||||||
|
var cc = Console.ForegroundColor;
|
||||||
|
loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||||
|
{
|
||||||
|
if (name == null || name.Length < 2)
|
||||||
|
name = typeName;
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine("[CMD] Successfully loaded command : " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
if (exception is null)
|
||||||
|
Console.WriteLine("An error occured while loading: " + name);
|
||||||
|
else
|
||||||
|
Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.ForegroundColor = cc;
|
||||||
|
};
|
||||||
|
loader.onEVELoad += (name, typeName, success, exception) =>
|
||||||
|
{
|
||||||
|
if (name == null || name.Length < 2)
|
||||||
|
name = typeName;
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine("[EVENT] Successfully loaded event : " + name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.ForegroundColor = cc;
|
||||||
|
};
|
||||||
|
|
||||||
|
loader.onSLSHLoad += (name, typeName, success, exception) =>
|
||||||
|
{
|
||||||
|
if (name == null || name.Length < 2)
|
||||||
|
name = typeName;
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine("[SLASH] Successfully loaded command : " + name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.ForegroundColor = cc;
|
||||||
|
};
|
||||||
|
|
||||||
|
loader.LoadPlugins();
|
||||||
|
Console.ForegroundColor = cc;
|
||||||
|
pluginsLoaded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
@@ -14,17 +12,15 @@ using PluginManager.Online.Helpers;
|
|||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
using DiscordBot.Utilities;
|
using DiscordBot.Utilities;
|
||||||
using Microsoft.VisualBasic.CompilerServices;
|
|
||||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
|
||||||
using static PluginManager.Config;
|
using static PluginManager.Config;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
|
||||||
namespace DiscordBot;
|
namespace DiscordBot;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static Json<string, string> URLs;
|
public static Json<string, string> URLs;
|
||||||
private static bool loadPluginsOnStartup = false;
|
public static PluginManager.Others.Actions.InternalActionManager internalActionManager;
|
||||||
private static ConsoleCommandsHandler consoleCommandsHandler;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main entry point for the application.
|
/// The main entry point for the application.
|
||||||
@@ -56,19 +52,17 @@ public class Program
|
|||||||
Console.WriteLine("Debug mode enabled");
|
Console.WriteLine("Debug mode enabled");
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
if (loadPluginsOnStartup)
|
|
||||||
consoleCommandsHandler.HandleCommand("lp");
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var cmd = Console.ReadLine();
|
var cmd = Console.ReadLine();
|
||||||
if (!consoleCommandsHandler.HandleCommand(cmd!
|
string[] args = cmd.Split(' ');
|
||||||
#if DEBUG
|
string command = args[0];
|
||||||
, false
|
args = args.Skip(1).ToArray();
|
||||||
#endif
|
if(args.Length == 0)
|
||||||
|
args = null;
|
||||||
|
|
||||||
) && cmd.Length > 0)
|
internalActionManager.Execute(command, args).Wait(); // Execute the command
|
||||||
Console.WriteLine("Failed to run command " + cmd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +130,6 @@ public class Program
|
|||||||
Console.WriteLine("Loading Core ...");
|
Console.WriteLine("Loading Core ...");
|
||||||
|
|
||||||
var b = await StartNoGui();
|
var b = await StartNoGui();
|
||||||
consoleCommandsHandler = new ConsoleCommandsHandler(b.client);
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if(args.Contains("--gui"))
|
if(args.Contains("--gui"))
|
||||||
@@ -145,6 +138,10 @@ public class Program
|
|||||||
Console.WriteLine("GUI not implemented yet");
|
Console.WriteLine("GUI not implemented yet");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internalActionManager = new PluginManager.Others.Actions.InternalActionManager("./Data/Actions", "*.dll");
|
||||||
|
await internalActionManager.Initialize();
|
||||||
|
|
||||||
NoGUI();
|
NoGUI();
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
@@ -217,7 +214,7 @@ public class Program
|
|||||||
case "CurrentVersion":
|
case "CurrentVersion":
|
||||||
var currentVersion = Config.Data["Version"];
|
var currentVersion = Config.Data["Version"];
|
||||||
var newVersion = s[1];
|
var newVersion = s[1];
|
||||||
if(newVersion != currentVersion)
|
if(new VersionString(newVersion) != new VersionString(newVersion))
|
||||||
{
|
{
|
||||||
Console.WriteLine("A new updated was found. Check the changelog for more information.");
|
Console.WriteLine("A new updated was found. Check the changelog for more information.");
|
||||||
List<string> changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
|
List<string> changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
|
||||||
@@ -232,122 +229,10 @@ public class Program
|
|||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
// case "CurrentVersion":
|
|
||||||
// var newVersion = s[1];
|
|
||||||
// var currentVersion = Config.Data["Version"];
|
|
||||||
|
|
||||||
// if (!newVersion.Equals(currentVersion))
|
|
||||||
// {
|
|
||||||
|
|
||||||
// var nVer = new VersionString(newVersion.Substring(2));
|
|
||||||
// var cVer = new VersionString((Config.Data["Version"]).Substring(2));
|
|
||||||
// if (cVer > nVer)
|
|
||||||
// {
|
|
||||||
// Config.Data["Version"] = "1." + cVer.ToShortString() + " (Beta)";
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (OperatingSystem.WINDOWS == Functions.GetOperatingSystem())
|
|
||||||
// {
|
|
||||||
// Console.Clear();
|
|
||||||
// Console.WriteLine("A new update was found !");
|
|
||||||
// Console.WriteLine("Run the launcher to update");
|
|
||||||
// Console.WriteLine("Current version: " + currentVersion);
|
|
||||||
// Console.WriteLine("Latest version: " + s[1]);
|
|
||||||
|
|
||||||
// File.WriteAllText("version.txt", currentVersion);
|
|
||||||
|
|
||||||
// await Task.Delay(3000);
|
|
||||||
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if(OperatingSystem.LINUX == Functions.GetOperatingSystem())
|
|
||||||
// {
|
|
||||||
// Console.WriteLine("A new update was found !");
|
|
||||||
// Console.WriteLine("Run the launcher to update");
|
|
||||||
// }
|
|
||||||
// Console.Clear();
|
|
||||||
// Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
// Console.WriteLine("A new version of the bot is available !");
|
|
||||||
// Console.ForegroundColor = ConsoleColor.Yellow;
|
|
||||||
// Console.WriteLine("Current version : " +
|
|
||||||
// Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
|
||||||
// Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
// Console.WriteLine("New version : " + newVersion);
|
|
||||||
// Console.ForegroundColor = ConsoleColor.White;
|
|
||||||
|
|
||||||
// File.WriteAllText("version.txt", newVersion);
|
|
||||||
|
|
||||||
// Console.WriteLine("Changelog :");
|
|
||||||
|
|
||||||
// List<string> changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
|
|
||||||
// foreach (var item in changeLog)
|
|
||||||
// Utilities.Utilities.WriteColorText(item);
|
|
||||||
// Console.WriteLine("Do you want to update the bot ? (y/n)");
|
|
||||||
// if (Console.ReadKey().Key == ConsoleKey.Y)
|
|
||||||
// {
|
|
||||||
// var url = URLs["LinuxBot"].Replace("{0}", newVersion);
|
|
||||||
// Config.Logger.Log($"executing: download_file {url}");
|
|
||||||
|
|
||||||
// await ServerCom.DownloadFileAsync(url, "./update.zip", new Progress<float>(percent => { Console.WriteLine($"\rProgress: {percent}% "); }));
|
|
||||||
// await File.WriteAllTextAsync("Install.sh",
|
|
||||||
// "#!/bin/bash\nunzip -qq -o update.zip \nrm update.zip\nchmod a+x DiscordBot");
|
|
||||||
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// Console.WriteLine("executing: chmod a+x Install.sh");
|
|
||||||
// Process.Start("chmod", "a+x Install.sh").WaitForExit();
|
|
||||||
// Process.Start("Install.sh").WaitForExit();
|
|
||||||
|
|
||||||
// Console.WriteLine("executing: rm Install.sh");
|
|
||||||
// Process.Start("rm", "Install.sh").WaitForExit();
|
|
||||||
|
|
||||||
// Config.Logger.Log("The new version of the bot has been installed.");
|
|
||||||
// Console.WriteLine("Please restart the bot.");
|
|
||||||
// Environment.Exit(0);
|
|
||||||
// }
|
|
||||||
// catch (Exception ex)
|
|
||||||
// {
|
|
||||||
// Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR);
|
|
||||||
// if (ex.Message.Contains("Access de"))
|
|
||||||
// Config.Logger.Log("Please run the bot as sudo.");
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// break;
|
|
||||||
// case "LauncherVersion":
|
|
||||||
// var updaternewversion = s[1];
|
|
||||||
// //File.WriteAllText(updaternewversion + ".txt", updaternewversion);
|
|
||||||
// if (Functions.GetOperatingSystem() == OperatingSystem.LINUX)
|
|
||||||
// break;
|
|
||||||
|
|
||||||
// Directory.CreateDirectory(Functions.dataFolder + "Applications");
|
|
||||||
// if (!Config.Data.ContainsKey("LauncherVersion"))
|
|
||||||
// Config.Data["LauncherVersion"] = "0.0.0.0";
|
|
||||||
// if (Config.Data["LauncherVersion"] != updaternewversion ||
|
|
||||||
// !File.Exists("./Launcher.exe"))
|
|
||||||
// {
|
|
||||||
// Console.Clear();
|
|
||||||
// Console.WriteLine("Installing a new Launcher ...\nDo NOT close the bot during update !");
|
|
||||||
// var bar = new Utilities.Utilities.ProgressBar(ProgressBarType.NO_END);
|
|
||||||
// bar.Start();
|
|
||||||
// await ServerCom.DownloadFileAsync(URLs["WindowsLauncher"], $"./Launcher.exe", null);
|
|
||||||
// //await ArchiveManager.ExtractArchive("./Updater.zip", "./", null,
|
|
||||||
// // UnzipProgressType.PercentageFromTotalSize);
|
|
||||||
// Config.Data["LauncherVersion"] = updaternewversion;
|
|
||||||
// // File.Delete("Updater.zip");
|
|
||||||
// bar.Stop("The launcher has been updated !");
|
|
||||||
// Console.Clear();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Discord.Commands;
|
|
||||||
using Discord.WebSocket;
|
|
||||||
|
|
||||||
namespace DiscordBot.Utilities;
|
|
||||||
|
|
||||||
public class ConsoleCommand
|
|
||||||
{
|
|
||||||
public string? CommandName { get; init; }
|
|
||||||
public string? Description { get; init; }
|
|
||||||
public string? Usage { get; init; }
|
|
||||||
public Action<string[]>? Action { get; init; }
|
|
||||||
}
|
|
||||||
@@ -1,439 +0,0 @@
|
|||||||
using System.Threading;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
using Discord.WebSocket;
|
|
||||||
|
|
||||||
using PluginManager;
|
|
||||||
using PluginManager.Loaders;
|
|
||||||
using PluginManager.Online;
|
|
||||||
using PluginManager.Others;
|
|
||||||
|
|
||||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
|
||||||
|
|
||||||
namespace DiscordBot.Utilities;
|
|
||||||
|
|
||||||
public class ConsoleCommandsHandler
|
|
||||||
{
|
|
||||||
|
|
||||||
public static ConsoleCommandsHandler handler;
|
|
||||||
private readonly PluginsManager manager;
|
|
||||||
|
|
||||||
private readonly List<ConsoleCommand> commandList = new();
|
|
||||||
|
|
||||||
|
|
||||||
private bool isDownloading;
|
|
||||||
private bool pluginsLoaded;
|
|
||||||
private DiscordSocketClient client;
|
|
||||||
|
|
||||||
public ConsoleCommandsHandler(DiscordSocketClient client)
|
|
||||||
{
|
|
||||||
this.client = client;
|
|
||||||
|
|
||||||
manager = new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
|
||||||
InitializeBasicCommands();
|
|
||||||
Console.WriteLine("Done");
|
|
||||||
|
|
||||||
if (handler == null)
|
|
||||||
handler = this;
|
|
||||||
else
|
|
||||||
throw new Exception("ConsoleCommandsHandler already initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeBasicCommands()
|
|
||||||
{
|
|
||||||
commandList.Clear();
|
|
||||||
|
|
||||||
AddCommand("help", "Show help", "help <command>", args =>
|
|
||||||
{
|
|
||||||
if (args.Length <= 1)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Available commands:");
|
|
||||||
var items = new List<string[]>
|
|
||||||
{
|
|
||||||
new[] { "-", "-", "-" },
|
|
||||||
new[] { "Command", "Description", "Usage" },
|
|
||||||
new[] { " ", " ", "Argument type: <optional> [required]" },
|
|
||||||
new[] { "-", "-", "-" }
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach (var command in commandList)
|
|
||||||
{
|
|
||||||
if (!command.CommandName.StartsWith("_"))
|
|
||||||
items.Add(new[] { command.CommandName, command.Description, command.Usage });
|
|
||||||
}
|
|
||||||
|
|
||||||
items.Add(new[] { "-", "-", "-" });
|
|
||||||
Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (var command in commandList)
|
|
||||||
if (command.CommandName == args[1])
|
|
||||||
{
|
|
||||||
Console.WriteLine("Command description: " + command.Description);
|
|
||||||
Console.WriteLine("Command execution format:" + command.Usage);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine("Command not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("lp", "Load plugins", () =>
|
|
||||||
{
|
|
||||||
if (pluginsLoaded)
|
|
||||||
return;
|
|
||||||
var loader = new PluginLoader(client!);
|
|
||||||
var cc = Console.ForegroundColor;
|
|
||||||
loader.onCMDLoad += (name, typeName, success, exception) =>
|
|
||||||
{
|
|
||||||
if (name == null || name.Length < 2)
|
|
||||||
name = typeName;
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.WriteLine("[CMD] Successfully loaded command : " + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
if (exception is null)
|
|
||||||
Console.WriteLine("An error occured while loading: " + name);
|
|
||||||
else
|
|
||||||
Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.ForegroundColor = cc;
|
|
||||||
};
|
|
||||||
loader.onEVELoad += (name, typeName, success, exception) =>
|
|
||||||
{
|
|
||||||
if (name == null || name.Length < 2)
|
|
||||||
name = typeName;
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.WriteLine("[EVENT] Successfully loaded event : " + name);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.ForegroundColor = cc;
|
|
||||||
};
|
|
||||||
|
|
||||||
loader.onSLSHLoad += (name, typeName, success, exception) =>
|
|
||||||
{
|
|
||||||
if (name == null || name.Length < 2)
|
|
||||||
name = typeName;
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
|
||||||
Console.WriteLine("[SLASH] Successfully loaded command : " + name);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.ForegroundColor = cc;
|
|
||||||
};
|
|
||||||
|
|
||||||
loader.LoadPlugins();
|
|
||||||
Console.ForegroundColor = cc;
|
|
||||||
pluginsLoaded = true;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("listplugs", "list available plugins", async () => {
|
|
||||||
if(manager == null)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Plugin manager is null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var data = await manager.GetAvailablePlugins();
|
|
||||||
var items = new List<string[]>
|
|
||||||
{
|
|
||||||
new[] { "-", "-", "-", "-" },
|
|
||||||
new[] { "Name", "Type", "Description", "Required" },
|
|
||||||
new[] { "-", "-", "-", "-" }
|
|
||||||
};
|
|
||||||
|
|
||||||
foreach (var plugin in data)
|
|
||||||
{
|
|
||||||
items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
|
||||||
}
|
|
||||||
|
|
||||||
items.Add(new[] { "-", "-", "-", "-" });
|
|
||||||
Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
|
||||||
});
|
|
||||||
|
|
||||||
AddCommand("dwplug", "download plugin", "dwplug [name]", async args =>
|
|
||||||
{
|
|
||||||
isDownloading = true;
|
|
||||||
Utilities.Spinner spinner = new Utilities.Spinner();
|
|
||||||
if (args.Length == 1)
|
|
||||||
{
|
|
||||||
isDownloading = false;
|
|
||||||
Console.WriteLine("Please specify plugin name");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var name = string.Join(' ', args, 1, args.Length - 1);
|
|
||||||
// info[0] = plugin type
|
|
||||||
// info[1] = plugin link
|
|
||||||
// info[2] = if others are required, or string.Empty if none
|
|
||||||
var info = await manager.GetPluginLinkByName(name);
|
|
||||||
if (info[1] == null) // link is null
|
|
||||||
{
|
|
||||||
if (name == "")
|
|
||||||
{
|
|
||||||
isDownloading = false;
|
|
||||||
Utilities.WriteColorText("Name is invalid");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
isDownloading = false;
|
|
||||||
Utilities.WriteColorText($"Failed to find plugin &b{name} &c!" +
|
|
||||||
" Use &glistplugs &ccommand to display all available plugins !");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
spinner.Start();
|
|
||||||
|
|
||||||
string path;
|
|
||||||
if (info[0] == "Plugin")
|
|
||||||
path = "./Data/Plugins/" + name + ".dll";
|
|
||||||
else
|
|
||||||
path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}";
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine($"Downloading plugin {name} from {info[1]} to {path}");
|
|
||||||
await ServerCom.DownloadFileAsync(info[1], path, null);
|
|
||||||
|
|
||||||
Console.WriteLine("\n");
|
|
||||||
|
|
||||||
// check requirements if any
|
|
||||||
|
|
||||||
if (info.Length == 3 && info[2] != string.Empty && info[2] != null)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Downloading requirements for plugin : {name}");
|
|
||||||
|
|
||||||
var lines = await ServerCom.ReadTextFromURL(info[2]);
|
|
||||||
|
|
||||||
foreach (var line in lines)
|
|
||||||
{
|
|
||||||
if (!(line.Length > 0 && line.Contains(",")))
|
|
||||||
continue;
|
|
||||||
var split = line.Split(',');
|
|
||||||
Console.WriteLine($"\nDownloading item: {split[1]}");
|
|
||||||
if (File.Exists("./" + split[1])) File.Delete("./" + split[1]);
|
|
||||||
await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null);
|
|
||||||
|
|
||||||
Console.WriteLine("Item " + split[1] + " downloaded !");
|
|
||||||
|
|
||||||
Console.WriteLine();
|
|
||||||
if (split[0].EndsWith(".pak"))
|
|
||||||
{
|
|
||||||
File.Move("./" + split[1], "./Data/PAKS/" + split[1], true);
|
|
||||||
}
|
|
||||||
else if (split[0].EndsWith(".zip") || split[0].EndsWith(".pkg"))
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Extracting {split[1]} ...");
|
|
||||||
await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
|
|
||||||
UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE);
|
|
||||||
Console.WriteLine("\n");
|
|
||||||
File.Delete("./" + split[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine();
|
|
||||||
}
|
|
||||||
spinner.Stop();
|
|
||||||
|
|
||||||
var ver = await manager.GetVersionOfPackageFromWeb(name);
|
|
||||||
if (ver is null) throw new Exception("Incorrect version");
|
|
||||||
Config.Plugins[name] = ver.ToShortString();
|
|
||||||
|
|
||||||
isDownloading = false;
|
|
||||||
|
|
||||||
|
|
||||||
Config.Logger.Log("Plugin installed !", this, LogLevel.INFO);
|
|
||||||
|
|
||||||
//await ExecuteCommad("localload " + name);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
AddCommand("value", "read value from VariableStack", "value [key]", args =>
|
|
||||||
{
|
|
||||||
if (args.Length != 2)
|
|
||||||
return;
|
|
||||||
if (!Config.Data.ContainsKey(args[1]))
|
|
||||||
return;
|
|
||||||
|
|
||||||
var data = Config.Data[args[1]];
|
|
||||||
Console.WriteLine($"{args[1]} => {data}");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("add", "add variable to the system variables", "add [key] [value]", args =>
|
|
||||||
{
|
|
||||||
if (args.Length < 4)
|
|
||||||
return;
|
|
||||||
var key = args[1];
|
|
||||||
var value = args[2];
|
|
||||||
var isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Config.Data[key] = value;
|
|
||||||
Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("remv", "remove variable from system variables", "remv [key]", args =>
|
|
||||||
{
|
|
||||||
if (args.Length < 2)
|
|
||||||
return;
|
|
||||||
Config.Data.Remove(args[1]);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("sd", "Shuts down the discord bot", async () =>
|
|
||||||
{
|
|
||||||
if (client is null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data);
|
|
||||||
await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins);
|
|
||||||
await client.StopAsync();
|
|
||||||
await client.DisposeAsync();
|
|
||||||
|
|
||||||
Config.Logger.SaveToFile();
|
|
||||||
await Task.Delay(1000);
|
|
||||||
Environment.Exit(0);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
AddCommand("import", "Load an external command", "import [pluginName]", async args =>
|
|
||||||
{
|
|
||||||
if (args.Length <= 1) return;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var pName = string.Join(' ', args, 1, args.Length - 1);
|
|
||||||
using (var client = new HttpClient())
|
|
||||||
{
|
|
||||||
var url = (await manager.GetPluginLinkByName(pName))[1];
|
|
||||||
if (url is null) throw new Exception($"Invalid plugin name {pName}.");
|
|
||||||
var s = await client.GetStreamAsync(url);
|
|
||||||
var str = new MemoryStream();
|
|
||||||
await s.CopyToAsync(str);
|
|
||||||
var asmb = Assembly.Load(str.ToArray());
|
|
||||||
|
|
||||||
await PluginLoader.LoadPluginFromAssembly(asmb, this.client);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
AddCommand("localload", "Load a local command", "local [pluginName]", async args =>
|
|
||||||
{
|
|
||||||
if (args.Length <= 1) return;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var pName = string.Join(' ', args, 1, args.Length - 1);
|
|
||||||
var asmb = Assembly.LoadFile(Path.GetFullPath("./Data/Plugins/" + pName + ".dll"));
|
|
||||||
|
|
||||||
await PluginLoader.LoadPluginFromAssembly(asmb, this.client);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddCommand(string command, string description, string usage, Action<string[]> action)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Adding command {command} ...");
|
|
||||||
commandList.Add(new ConsoleCommand
|
|
||||||
{ CommandName = command, Description = description, Action = action, Usage = usage });
|
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
|
||||||
Utilities.WriteColorText($"Command &r{command} &cadded to the list of commands");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddCommand(string command, string description, Action action)
|
|
||||||
{
|
|
||||||
AddCommand(command, description, command, args => action());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveCommand(string command)
|
|
||||||
{
|
|
||||||
commandList.RemoveAll(x => x.CommandName == command);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool CommandExists(string command)
|
|
||||||
{
|
|
||||||
return GetCommand(command) is not null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConsoleCommand? GetCommand(string command)
|
|
||||||
{
|
|
||||||
return commandList.FirstOrDefault(t => t.CommandName == command);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool HandleCommand(string command, bool removeCommandExecution = true)
|
|
||||||
{
|
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
|
||||||
var args = command.Split(' ');
|
|
||||||
foreach (var item in commandList.ToList())
|
|
||||||
if (item.CommandName == args[0])
|
|
||||||
{
|
|
||||||
if (args[0].StartsWith("_"))
|
|
||||||
throw new Exception("This command is reserved for internal worker and can not be executed manually !");
|
|
||||||
|
|
||||||
if (removeCommandExecution)
|
|
||||||
{
|
|
||||||
Console.SetCursorPosition(0, Console.CursorTop - 1);
|
|
||||||
for (var i = 0; i < command.Length + 30; i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.SetCursorPosition(0, Console.CursorTop);
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine();
|
|
||||||
item.Action(args);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
435
DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs
Normal file
435
DiscordBot/Utilities/ConsoleCommandsHandler_OLD.cs
Normal file
@@ -0,0 +1,435 @@
|
|||||||
|
// using System;
|
||||||
|
// using System.Collections.Generic;
|
||||||
|
// using System.IO;
|
||||||
|
// using System.Linq;
|
||||||
|
// using System.Net.Http;
|
||||||
|
// using System.Reflection;
|
||||||
|
// using System.Threading.Tasks;
|
||||||
|
|
||||||
|
// using Discord.WebSocket;
|
||||||
|
|
||||||
|
// using PluginManager;
|
||||||
|
// using PluginManager.Loaders;
|
||||||
|
// using PluginManager.Online;
|
||||||
|
// using PluginManager.Others;
|
||||||
|
|
||||||
|
// namespace DiscordBot.Utilities;
|
||||||
|
|
||||||
|
// public class ConsoleCommandsHandlerOLD
|
||||||
|
// {
|
||||||
|
|
||||||
|
// public static ConsoleCommandsHandler handler;
|
||||||
|
// private readonly PluginsManager manager;
|
||||||
|
|
||||||
|
// private readonly List<ConsoleCommand> commandList = new();
|
||||||
|
|
||||||
|
|
||||||
|
// private bool isDownloading;
|
||||||
|
// private bool pluginsLoaded;
|
||||||
|
// private DiscordSocketClient client;
|
||||||
|
|
||||||
|
// public ConsoleCommandsHandler(DiscordSocketClient client)
|
||||||
|
// {
|
||||||
|
// this.client = client;
|
||||||
|
|
||||||
|
// manager = new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
// InitializeBasicCommands();
|
||||||
|
// Console.WriteLine("Done");
|
||||||
|
|
||||||
|
// if (handler == null)
|
||||||
|
// handler = this;
|
||||||
|
// else
|
||||||
|
// throw new Exception("ConsoleCommandsHandler already initialized");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private void InitializeBasicCommands()
|
||||||
|
// {
|
||||||
|
// commandList.Clear();
|
||||||
|
|
||||||
|
// AddCommand("help", "Show help", "help <command>", args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length <= 1)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine("Available commands:");
|
||||||
|
// var items = new List<string[]>
|
||||||
|
// {
|
||||||
|
// new[] { "-", "-", "-" },
|
||||||
|
// new[] { "Command", "Description", "Usage" },
|
||||||
|
// new[] { " ", " ", "Argument type: <optional> [required]" },
|
||||||
|
// new[] { "-", "-", "-" }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// foreach (var command in commandList)
|
||||||
|
// {
|
||||||
|
// if (!command.CommandName.StartsWith("_"))
|
||||||
|
// items.Add(new[] { command.CommandName, command.Description, command.Usage });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// items.Add(new[] { "-", "-", "-" });
|
||||||
|
// Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// foreach (var command in commandList)
|
||||||
|
// if (command.CommandName == args[1])
|
||||||
|
// {
|
||||||
|
// Console.WriteLine("Command description: " + command.Description);
|
||||||
|
// Console.WriteLine("Command execution format:" + command.Usage);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.WriteLine("Command not found");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("lp", "Load plugins", () =>
|
||||||
|
// {
|
||||||
|
// if (pluginsLoaded)
|
||||||
|
// return;
|
||||||
|
// var loader = new PluginLoader(client!);
|
||||||
|
// var cc = Console.ForegroundColor;
|
||||||
|
// loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||||
|
// {
|
||||||
|
// if (name == null || name.Length < 2)
|
||||||
|
// name = typeName;
|
||||||
|
// if (success)
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
// Console.WriteLine("[CMD] Successfully loaded command : " + name);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
// if (exception is null)
|
||||||
|
// Console.WriteLine("An error occured while loading: " + name);
|
||||||
|
// else
|
||||||
|
// Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.ForegroundColor = cc;
|
||||||
|
// };
|
||||||
|
// loader.onEVELoad += (name, typeName, success, exception) =>
|
||||||
|
// {
|
||||||
|
// if (name == null || name.Length < 2)
|
||||||
|
// name = typeName;
|
||||||
|
|
||||||
|
// if (success)
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
// Console.WriteLine("[EVENT] Successfully loaded event : " + name);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
// Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.ForegroundColor = cc;
|
||||||
|
// };
|
||||||
|
|
||||||
|
// loader.onSLSHLoad += (name, typeName, success, exception) =>
|
||||||
|
// {
|
||||||
|
// if (name == null || name.Length < 2)
|
||||||
|
// name = typeName;
|
||||||
|
|
||||||
|
// if (success)
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
// Console.WriteLine("[SLASH] Successfully loaded command : " + name);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
// Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.ForegroundColor = cc;
|
||||||
|
// };
|
||||||
|
|
||||||
|
// loader.LoadPlugins();
|
||||||
|
// Console.ForegroundColor = cc;
|
||||||
|
// pluginsLoaded = true;
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("listplugs", "list available plugins", async () => {
|
||||||
|
// if(manager == null)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine("Plugin manager is null");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// var data = await manager.GetAvailablePlugins();
|
||||||
|
// var items = new List<string[]>
|
||||||
|
// {
|
||||||
|
// new[] { "-", "-", "-", "-" },
|
||||||
|
// new[] { "Name", "Type", "Description", "Required" },
|
||||||
|
// new[] { "-", "-", "-", "-" }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// foreach (var plugin in data)
|
||||||
|
// {
|
||||||
|
// items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// items.Add(new[] { "-", "-", "-", "-" });
|
||||||
|
// Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// AddCommand("dwplug", "download plugin", "dwplug [name]", async args =>
|
||||||
|
// {
|
||||||
|
// isDownloading = true;
|
||||||
|
// Utilities.Spinner spinner = new Utilities.Spinner();
|
||||||
|
// if (args.Length == 1)
|
||||||
|
// {
|
||||||
|
// isDownloading = false;
|
||||||
|
// Console.WriteLine("Please specify plugin name");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var name = string.Join(' ', args, 1, args.Length - 1);
|
||||||
|
// // info[0] = plugin type
|
||||||
|
// // info[1] = plugin link
|
||||||
|
// // info[2] = if others are required, or string.Empty if none
|
||||||
|
// var info = await manager.GetPluginLinkByName(name);
|
||||||
|
// if (info[1] == null) // link is null
|
||||||
|
// {
|
||||||
|
// if (name == "")
|
||||||
|
// {
|
||||||
|
// isDownloading = false;
|
||||||
|
// Utilities.WriteColorText("Name is invalid");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// isDownloading = false;
|
||||||
|
// Utilities.WriteColorText($"Failed to find plugin &b{name} &c!" +
|
||||||
|
// " Use &glistplugs &ccommand to display all available plugins !");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// spinner.Start();
|
||||||
|
|
||||||
|
// string path;
|
||||||
|
// if (info[0] == "Plugin")
|
||||||
|
// path = "./Data/Plugins/" + name + ".dll";
|
||||||
|
// else
|
||||||
|
// path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}";
|
||||||
|
|
||||||
|
|
||||||
|
// Console.WriteLine($"Downloading plugin {name} from {info[1]} to {path}");
|
||||||
|
// await ServerCom.DownloadFileAsync(info[1], path, null);
|
||||||
|
|
||||||
|
// Console.WriteLine("\n");
|
||||||
|
|
||||||
|
// // check requirements if any
|
||||||
|
|
||||||
|
// if (info.Length == 3 && info[2] != string.Empty && info[2] != null)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine($"Downloading requirements for plugin : {name}");
|
||||||
|
|
||||||
|
// var lines = await ServerCom.ReadTextFromURL(info[2]);
|
||||||
|
|
||||||
|
// foreach (var line in lines)
|
||||||
|
// {
|
||||||
|
// if (!(line.Length > 0 && line.Contains(",")))
|
||||||
|
// continue;
|
||||||
|
// var split = line.Split(',');
|
||||||
|
// Console.WriteLine($"\nDownloading item: {split[1]}");
|
||||||
|
// if (File.Exists("./" + split[1])) File.Delete("./" + split[1]);
|
||||||
|
// await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null);
|
||||||
|
|
||||||
|
// Console.WriteLine("Item " + split[1] + " downloaded !");
|
||||||
|
|
||||||
|
// Console.WriteLine();
|
||||||
|
// if (split[0].EndsWith(".pak"))
|
||||||
|
// {
|
||||||
|
// File.Move("./" + split[1], "./Data/PAKS/" + split[1], true);
|
||||||
|
// }
|
||||||
|
// else if (split[0].EndsWith(".zip") || split[0].EndsWith(".pkg"))
|
||||||
|
// {
|
||||||
|
// Console.WriteLine($"Extracting {split[1]} ...");
|
||||||
|
// await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
|
||||||
|
// UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE);
|
||||||
|
// Console.WriteLine("\n");
|
||||||
|
// File.Delete("./" + split[1]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.WriteLine();
|
||||||
|
// }
|
||||||
|
// spinner.Stop();
|
||||||
|
|
||||||
|
// var ver = await manager.GetVersionOfPackageFromWeb(name);
|
||||||
|
// if (ver is null) throw new Exception("Incorrect version");
|
||||||
|
// Config.Plugins[name] = ver.ToShortString();
|
||||||
|
|
||||||
|
// isDownloading = false;
|
||||||
|
|
||||||
|
|
||||||
|
// Config.Logger.Log("Plugin installed !", this, LogLevel.INFO);
|
||||||
|
|
||||||
|
// //await ExecuteCommad("localload " + name);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
|
||||||
|
// AddCommand("value", "read value from VariableStack", "value [key]", args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length != 2)
|
||||||
|
// return;
|
||||||
|
// if (!Config.Data.ContainsKey(args[1]))
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// var data = Config.Data[args[1]];
|
||||||
|
// Console.WriteLine($"{args[1]} => {data}");
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("add", "add variable to the system variables", "add [key] [value]", args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length < 4)
|
||||||
|
// return;
|
||||||
|
// var key = args[1];
|
||||||
|
// var value = args[2];
|
||||||
|
// var isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
||||||
|
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// Config.Data[key] = value;
|
||||||
|
// Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine(ex.ToString());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("remv", "remove variable from system variables", "remv [key]", args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length < 2)
|
||||||
|
// return;
|
||||||
|
// Config.Data.Remove(args[1]);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("sd", "Shuts down the discord bot", async () =>
|
||||||
|
// {
|
||||||
|
// if (client is null)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data);
|
||||||
|
// await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins);
|
||||||
|
// await client.StopAsync();
|
||||||
|
// await client.DisposeAsync();
|
||||||
|
|
||||||
|
// Config.Logger.SaveToFile();
|
||||||
|
// await Task.Delay(1000);
|
||||||
|
// Environment.Exit(0);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
|
||||||
|
// AddCommand("import", "Load an external command", "import [pluginName]", async args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length <= 1) return;
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// var pName = string.Join(' ', args, 1, args.Length - 1);
|
||||||
|
// using (var client = new HttpClient())
|
||||||
|
// {
|
||||||
|
// var url = (await manager.GetPluginLinkByName(pName))[1];
|
||||||
|
// if (url is null) throw new Exception($"Invalid plugin name {pName}.");
|
||||||
|
// var s = await client.GetStreamAsync(url);
|
||||||
|
// var str = new MemoryStream();
|
||||||
|
// await s.CopyToAsync(str);
|
||||||
|
// var asmb = Assembly.Load(str.ToArray());
|
||||||
|
|
||||||
|
// await PluginLoader.LoadPluginFromAssembly(asmb, this.client);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine(ex.Message);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
// AddCommand("localload", "Load a local command", "local [pluginName]", async args =>
|
||||||
|
// {
|
||||||
|
// if (args.Length <= 1) return;
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// var pName = string.Join(' ', args, 1, args.Length - 1);
|
||||||
|
// var asmb = Assembly.LoadFile(Path.GetFullPath("./Data/Plugins/" + pName + ".dll"));
|
||||||
|
|
||||||
|
// await PluginLoader.LoadPluginFromAssembly(asmb, this.client);
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine(ex.Message);
|
||||||
|
// Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
|
||||||
|
// commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void AddCommand(string command, string description, string usage, Action<string[]> action)
|
||||||
|
// {
|
||||||
|
// Console.WriteLine($"Adding command {command} ...");
|
||||||
|
// commandList.Add(new ConsoleCommand
|
||||||
|
// { CommandName = command, Description = description, Action = action, Usage = usage });
|
||||||
|
// Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
// Utilities.WriteColorText($"Command &r{command} &cadded to the list of commands");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void AddCommand(string command, string description, Action action)
|
||||||
|
// {
|
||||||
|
// AddCommand(command, description, command, args => action());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void RemoveCommand(string command)
|
||||||
|
// {
|
||||||
|
// commandList.RemoveAll(x => x.CommandName == command);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public bool CommandExists(string command)
|
||||||
|
// {
|
||||||
|
// return GetCommand(command) is not null;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public ConsoleCommand? GetCommand(string command)
|
||||||
|
// {
|
||||||
|
// return commandList.FirstOrDefault(t => t.CommandName == command);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public bool HandleCommand(string command, bool removeCommandExecution = true)
|
||||||
|
// {
|
||||||
|
// Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
// var args = command.Split(' ');
|
||||||
|
// foreach (var item in commandList.ToList())
|
||||||
|
// if (item.CommandName == args[0])
|
||||||
|
// {
|
||||||
|
// if (args[0].StartsWith("_"))
|
||||||
|
// throw new Exception("This command is reserved for internal worker and can not be executed manually !");
|
||||||
|
|
||||||
|
// if (removeCommandExecution)
|
||||||
|
// {
|
||||||
|
// Console.SetCursorPosition(0, Console.CursorTop - 1);
|
||||||
|
// for (var i = 0; i < command.Length + 30; i++)
|
||||||
|
// Console.Write(" ");
|
||||||
|
// Console.SetCursorPosition(0, Console.CursorTop);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Console.WriteLine();
|
||||||
|
// item.Action(args);
|
||||||
|
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@@ -88,6 +88,9 @@ public class Boot
|
|||||||
|
|
||||||
|
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
|
|
||||||
|
Config._DiscordBotClient = this;
|
||||||
|
|
||||||
while (!isReady) ;
|
while (!isReady) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,19 +5,24 @@ using System.IO;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using PluginManager.Online.Helpers;
|
|
||||||
using PluginManager.Others.Logger;
|
using PluginManager.Others.Logger;
|
||||||
|
|
||||||
namespace PluginManager;
|
namespace PluginManager;
|
||||||
|
|
||||||
public static class Config
|
public class Config
|
||||||
{
|
{
|
||||||
private static bool IsLoaded = false;
|
private static bool IsLoaded = false;
|
||||||
|
|
||||||
public static DBLogger Logger;
|
public static DBLogger Logger;
|
||||||
public static Json<string, string> Data;
|
public static Json<string, string> Data;
|
||||||
public static Json<string, string> Plugins;
|
public static Json<string, string> Plugins;
|
||||||
|
|
||||||
|
internal static Bot.Boot? _DiscordBotClient;
|
||||||
|
|
||||||
|
public static Bot.Boot? DiscordBot
|
||||||
|
{
|
||||||
|
get => _DiscordBotClient;
|
||||||
|
}
|
||||||
|
|
||||||
public static async Task Initialize()
|
public static async Task Initialize()
|
||||||
{
|
{
|
||||||
if (IsLoaded)
|
if (IsLoaded)
|
||||||
|
|||||||
23
PluginManager/Interfaces/IInternalAction.cs
Normal file
23
PluginManager/Interfaces/IInternalAction.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace PluginManager.Interfaces
|
||||||
|
{
|
||||||
|
public interface ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName { get; }
|
||||||
|
|
||||||
|
public string? Description { get; }
|
||||||
|
|
||||||
|
public string? Usage { get; }
|
||||||
|
|
||||||
|
public InternalActionRunType RunType { get; }
|
||||||
|
|
||||||
|
public Task Execute(string[]? args);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
76
PluginManager/Loaders/ActionsLoader.cs
Normal file
76
PluginManager/Loaders/ActionsLoader.cs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Reflection;
|
||||||
|
using PluginManager.Others.Actions;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace PluginManager.Loaders
|
||||||
|
{
|
||||||
|
public class ActionsLoader
|
||||||
|
{
|
||||||
|
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
|
||||||
|
public event ActionLoaded? ActionLoadedEvent;
|
||||||
|
|
||||||
|
private string actionFolder = @"./Data/Actions/";
|
||||||
|
private string actionExtension = "dll";
|
||||||
|
|
||||||
|
public ActionsLoader(string path, string extension)
|
||||||
|
{
|
||||||
|
actionFolder = path;
|
||||||
|
actionExtension = extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<ICommandAction>?> Load()
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(actionFolder);
|
||||||
|
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
|
||||||
|
|
||||||
|
List<ICommandAction> actions = new List<ICommandAction>();
|
||||||
|
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Assembly.LoadFrom(file);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ActionLoadedEvent?.Invoke(file, "", false, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||||
|
.SelectMany(s => s.GetTypes())
|
||||||
|
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
|
||||||
|
|
||||||
|
foreach (var type in types)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var action = (ICommandAction) Activator.CreateInstance(type);
|
||||||
|
if (action.ActionName == null)
|
||||||
|
{
|
||||||
|
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(action.RunType == PluginManager.Others.InternalActionRunType.ON_STARTUP)
|
||||||
|
await action.Execute(null);
|
||||||
|
|
||||||
|
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
|
||||||
|
actions.Add(action);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -110,7 +110,10 @@ namespace PluginManager.Loaders
|
|||||||
Exception = null,
|
Exception = null,
|
||||||
IsLoaded = true,
|
IsLoaded = true,
|
||||||
PluginName = type.FullName,
|
PluginName = type.FullName,
|
||||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : typeof(T) == typeof(DBEvent) ? "DBEvent" : "DBSlashCommand",
|
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" :
|
||||||
|
typeof(T) == typeof(DBEvent) ? "DBEvent" :
|
||||||
|
typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" :
|
||||||
|
null,
|
||||||
Plugin = plugin
|
Plugin = plugin
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using Discord.WebSocket;
|
|||||||
|
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Loaders;
|
namespace PluginManager.Loaders;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,24 @@ namespace PluginManager.Online.Helpers;
|
|||||||
|
|
||||||
public class VersionString
|
public class VersionString
|
||||||
{
|
{
|
||||||
|
private bool Equals(VersionString other)
|
||||||
|
{
|
||||||
|
return PackageCheckVersion == other.PackageCheckVersion && PackageMainVersion == other.PackageMainVersion && PackageVersionID == other.PackageVersionID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
if (ReferenceEquals(null, obj)) return false;
|
||||||
|
if (ReferenceEquals(this, obj)) return true;
|
||||||
|
if (obj.GetType() != this.GetType()) return false;
|
||||||
|
return Equals((VersionString)obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID);
|
||||||
|
}
|
||||||
|
|
||||||
public int PackageCheckVersion;
|
public int PackageCheckVersion;
|
||||||
public int PackageMainVersion;
|
public int PackageMainVersion;
|
||||||
public int PackageVersionID;
|
public int PackageVersionID;
|
||||||
@@ -12,11 +30,24 @@ public class VersionString
|
|||||||
{
|
{
|
||||||
var data = version.Split('.');
|
var data = version.Split('.');
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (data.Length == 3)
|
||||||
{
|
{
|
||||||
PackageVersionID = int.Parse(data[0]);
|
PackageVersionID = int.Parse(data[0]);
|
||||||
PackageMainVersion = int.Parse(data[1]);
|
PackageMainVersion = int.Parse(data[1]);
|
||||||
PackageCheckVersion = int.Parse(data[2]);
|
PackageCheckVersion = int.Parse(data[2]);
|
||||||
}
|
}
|
||||||
|
else if (data.Length == 4)
|
||||||
|
{
|
||||||
|
// ignore the first item data[0]
|
||||||
|
PackageVersionID = int.Parse(data[1]);
|
||||||
|
PackageMainVersion = int.Parse(data[2]);
|
||||||
|
PackageCheckVersion = int.Parse(data[3]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new Exception("Invalid version string");
|
||||||
|
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(version);
|
Console.WriteLine(version);
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PluginManager.Others.Actions
|
|
||||||
{
|
|
||||||
public class ActionManager
|
|
||||||
{
|
|
||||||
public List<InternalAction> Actions { get; private set; }
|
|
||||||
|
|
||||||
private bool _isInitialized = false;
|
|
||||||
|
|
||||||
public ActionManager()
|
|
||||||
{
|
|
||||||
if(_isInitialized) return;
|
|
||||||
|
|
||||||
Actions = new List<InternalAction>();
|
|
||||||
|
|
||||||
_isInitialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool ActionExists(string name)
|
|
||||||
{
|
|
||||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
|
||||||
return Actions.Any(x => x.Name == name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddAction(InternalAction action)
|
|
||||||
{
|
|
||||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
|
||||||
Actions.Add(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ExecuteAction(string name, string[] args)
|
|
||||||
{
|
|
||||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
|
||||||
var action = Actions.FirstOrDefault(x => x.Name == name);
|
|
||||||
if(action == null) throw new Exception($"Action {name} not found");
|
|
||||||
action.Invoke(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ExecuteActionAsync(string name, string[] args)
|
|
||||||
{
|
|
||||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
|
||||||
var action = Actions.FirstOrDefault(x => x.Name == name);
|
|
||||||
if(action == null) throw new Exception($"Action {name} not found");
|
|
||||||
await action.InvokeAsync(args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PluginManager.Others.Actions
|
|
||||||
{
|
|
||||||
public class InternalAction
|
|
||||||
{
|
|
||||||
public string? Name { get; init; }
|
|
||||||
public Action<string[]> Action { get; init; }
|
|
||||||
|
|
||||||
public InternalAction(string name, Action<string[]> action)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Action = action;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InternalAction(string name, Action action)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Action = (o) =>
|
|
||||||
{
|
|
||||||
action();
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Invoke(string[] args)
|
|
||||||
{
|
|
||||||
Action(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task InvokeAsync(string[] args)
|
|
||||||
{
|
|
||||||
await Task.Run(() => Action(args));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
57
PluginManager/Others/Actions/InternalActionsManager.cs
Normal file
57
PluginManager/Others/Actions/InternalActionsManager.cs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Loaders;
|
||||||
|
|
||||||
|
namespace PluginManager.Others.Actions
|
||||||
|
{
|
||||||
|
public class InternalActionManager
|
||||||
|
{
|
||||||
|
public ActionsLoader loader;
|
||||||
|
public Dictionary<string, ICommandAction> Actions = new Dictionary<string, ICommandAction>();
|
||||||
|
|
||||||
|
public InternalActionManager(string path, string extension)
|
||||||
|
{
|
||||||
|
loader = new ActionsLoader(path, extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Initialize()
|
||||||
|
{
|
||||||
|
loader.ActionLoadedEvent += OnActionLoaded;
|
||||||
|
var m_actions = await loader.Load();
|
||||||
|
if(m_actions == null) return;
|
||||||
|
foreach(var action in m_actions)
|
||||||
|
Actions.Add(action.ActionName, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnActionLoaded(string name, string typeName, bool success, Exception? e)
|
||||||
|
{
|
||||||
|
if (!success)
|
||||||
|
{
|
||||||
|
Config.Logger.Error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.Logger.Log($"Action {name} loaded successfully", typeName, LogLevel.INFO);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> Execute(string actionName, string[]? args)
|
||||||
|
{
|
||||||
|
if (!Actions.ContainsKey(actionName))
|
||||||
|
{
|
||||||
|
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING);
|
||||||
|
return "Action not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
await Actions[actionName].Execute(args);
|
||||||
|
return "Action executed";
|
||||||
|
}catch(Exception e){
|
||||||
|
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
|
||||||
|
return e.Message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,3 +35,8 @@ public enum SaveType
|
|||||||
BACKUP
|
BACKUP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum InternalActionRunType
|
||||||
|
{
|
||||||
|
ON_STARTUP,
|
||||||
|
ON_CALL
|
||||||
|
}
|
||||||
@@ -29,13 +29,14 @@ namespace PluginManager.Others.Logger
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
|
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
|
||||||
|
public void Error(Exception? e) => Log(e.Message, e.Source, LogLevel.ERROR);
|
||||||
|
|
||||||
public void Log(LogMessage message)
|
public void Log(LogMessage message)
|
||||||
{
|
{
|
||||||
if(LogEvent is not null)
|
if(LogEvent is not null)
|
||||||
LogEvent?.Invoke(message.Message, message.Type);
|
LogEvent?.Invoke(message.Message, message.Type);
|
||||||
|
|
||||||
if (message.Type != LogLevel.NONE)
|
if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL)
|
||||||
LogHistory.Add(message);
|
LogHistory.Add(message);
|
||||||
else
|
else
|
||||||
ErrorHistory.Add(message);
|
ErrorHistory.Add(message);
|
||||||
|
|||||||
Reference in New Issue
Block a user