Removed UI and Added IsDisabled plugin property

This commit is contained in:
2024-07-04 16:12:55 +03:00
parent d186efcdaf
commit 79c6daa11a
21 changed files with 126 additions and 894 deletions

View File

@@ -48,7 +48,7 @@ namespace DiscordBot.Bot.Actions
}
FileInfo fileInfo = new FileInfo(path);
PluginInfo pluginInfo = new PluginInfo(args[0], new(1, 0, 0), [], false, true);
PluginInfo pluginInfo = new PluginInfo(args[0], new(1, 0, 0), [], false, true, false);
await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(pluginInfo);
}
}

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using DiscordBot.Utilities;
using DiscordBotCore;
using DiscordBotCore.Interfaces.PluginManager;
using DiscordBotCore.Loaders;
using DiscordBotCore.Online;
using DiscordBotCore.Others;
@@ -19,15 +20,17 @@ namespace DiscordBot.Bot.Actions.Extra;
internal static class PluginMethods
{
internal static async Task List(PluginManager manager)
internal static async Task List()
{
Console.WriteLine($"Fetching plugin list from branch {manager.Branch} ...");
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetPluginsList(), "Reading remote database");
Console.WriteLine($"Fetching plugin list from branch {Application.CurrentApplication.PluginManager.Branch} ...");
var data = await ConsoleUtilities.ExecuteWithProgressBar(Application.CurrentApplication.PluginManager.GetPluginsList(), "Reading remote database");
TableData tableData = new(["Name", "Description", "Version", "Is Installed", "Dependencies"]);
var installedPlugins = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetInstalledPlugins(), "Reading local database ");
var installedPlugins = await ConsoleUtilities.ExecuteWithProgressBar(Application.CurrentApplication.PluginManager.GetInstalledPlugins(), "Reading local database ");
foreach (var plugin in data)
{
@@ -87,9 +90,19 @@ internal static class PluginMethods
}
internal static async Task DownloadPlugin(PluginManager manager, string pluginName)
internal static async Task DisablePlugin(string pluginName)
{
var pluginData = await manager.GetPluginDataByName(pluginName);
await Application.CurrentApplication.PluginManager.SetDisabledStatus(pluginName, true);
}
internal static async Task EnablePlugin(string pluginName)
{
await Application.CurrentApplication.PluginManager.SetDisabledStatus(pluginName, false);
}
internal static async Task DownloadPlugin(string pluginName)
{
var pluginData = await Application.CurrentApplication.PluginManager.GetPluginDataByName(pluginName);
if (pluginData is null)
{
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
@@ -127,12 +140,12 @@ internal static class PluginMethods
if (pluginData.HasScriptDependencies)
{
Console.WriteLine("Executing post install scripts ...");
await manager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
await Application.CurrentApplication.PluginManager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
}
PluginInfo pluginInfo = new(pluginName, pluginData.Version, []);
Console.WriteLine("Finished installing " + pluginName + " successfully");
await manager.AppendPluginToDatabase(pluginInfo);
await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(pluginInfo);
await RefreshPlugins(false);
return;
}
@@ -175,7 +188,7 @@ internal static class PluginMethods
await Parallel.ForEachAsync(downloadTasks, options, async (tuple, token) =>
{
tuple.Item1.IsIndeterminate = false;
string downloadLocation = manager.GenerateDependencyLocation(pluginName, tuple.Item4);
string downloadLocation = Application.CurrentApplication.PluginManager.GenerateDependencyLocation(pluginName, tuple.Item4);
await ServerCom.DownloadFileAsync(tuple.Item3, downloadLocation, tuple.Item2);
}
);
@@ -189,10 +202,10 @@ internal static class PluginMethods
if(pluginData.HasScriptDependencies)
{
Console.WriteLine("Executing post install scripts ...");
await manager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
await Application.CurrentApplication.PluginManager.ExecutePluginInstallScripts(pluginData.ScriptDependencies);
}
await manager.AppendPluginToDatabase(PluginInfo.FromOnlineInfo(pluginData));
await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(PluginInfo.FromOnlineInfo(pluginData));
await RefreshPlugins(false);
}

View File

@@ -29,6 +29,12 @@ public class Plugin: ICommandAction
new InternalActionOption("branch", "Sets a plugin option", [
new InternalActionOption("set", "Sets the branch"),
new InternalActionOption("get", "Gets the branch")
]),
new InternalActionOption("enable", "Enables a plugin", [
new InternalActionOption("name", "The name of the plugin to enable")
]),
new InternalActionOption("disable", "Disables a plugin", [
new InternalActionOption("name", "The name of the plugin to disable")
])
};
@@ -36,20 +42,40 @@ public class Plugin: ICommandAction
public async Task Execute(string[] args)
{
if (args is null || args.Length == 0 || args[0] == "help")
if (args is null || args.Length == 0)
{
Console.WriteLine("Usage : plugin [help|list|load|install]");
Console.WriteLine("help : Displays this message");
Console.WriteLine("list : Lists all plugins");
Console.WriteLine("load : Loads all plugins");
Console.WriteLine("install : Installs a plugin");
Console.WriteLine("refresh : Refreshes the plugin list");
await Application.CurrentApplication.InternalActionManager.Execute("help", ActionName);
return;
}
switch (args[0])
{
case "enable":
{
if (args.Length < 2)
{
Console.WriteLine("Usage : plugin enable <plugin name>");
return;
}
string pluginName = string.Join(' ', args, 1, args.Length - 1);
await Application.CurrentApplication.PluginManager.SetDisabledStatus(pluginName, false);
break;
}
case "disable":
{
if (args.Length < 2)
{
Console.WriteLine("Usage : plugin disable <plugin name>");
return;
}
string pluginName = string.Join(' ', args, 1, args.Length - 1);
await Application.CurrentApplication.PluginManager.SetDisabledStatus(pluginName, true);
break;
}
case "branch":
if (args.Length < 2)
{
@@ -87,14 +113,15 @@ public class Plugin: ICommandAction
break;
case "uninstall":
string plugName = string.Join(' ', args, 1, args.Length-1);
{
string plugName = string.Join(' ', args, 1, args.Length - 1);
bool result = await Application.CurrentApplication.PluginManager.MarkPluginToUninstall(plugName);
if(result)
if (result)
Console.WriteLine($"Marked to uninstall plugin {plugName}. Please restart the bot");
break;
}
case "list":
await PluginMethods.List(Application.CurrentApplication.PluginManager);
await PluginMethods.List();
break;
case "load":
if (pluginsLoaded)
@@ -113,6 +140,7 @@ public class Plugin: ICommandAction
break;
case "install":
{
var pluginName = string.Join(' ', args, 1, args.Length - 1);
if (string.IsNullOrEmpty(pluginName) || pluginName.Length < 2)
{
@@ -126,8 +154,10 @@ public class Plugin: ICommandAction
}
}
await PluginMethods.DownloadPlugin(Application.CurrentApplication.PluginManager, pluginName);
await PluginMethods.DownloadPlugin(pluginName);
break;
}
}
}
}

View File

@@ -3,9 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace DiscordBot;
@@ -20,35 +17,24 @@ public static class Entry
{
File.Delete(plugin);
}
File.Delete("./Data/Resources/plugins.json");
Directory.Delete("./Libraries/", true);
})
];
private static readonly string logo =
#if DEBUG
private static readonly string logo =
@"
_____ _ _ _____ _ _ ____ _
/ ____| | | | | | __ \(_) | | | _ \ | |
| (___ ___| |_| |__ | | | |_ ___ ___ ___ _ __ __| | | |_) | ___ | |_
\___ \ / _ \ __| '_ \ | | | | / __|/ __/ _ \| '__/ _` | | _ < / _ \| __|
____) | __/ |_| | | | | |__| | \__ \ (_| (_) | | | (_| | | |_) | (_) | |_
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
(Debug)
";
#else
@"
_____ _ _ _____ _ _ ____ _
/ ____| | | | | | __ \(_) | | | _ \ | |
| (___ ___| |_| |__ | | | |_ ___ ___ ___ _ __ __| | | |_) | ___ | |_
\___ \ / _ \ __| '_ \ | | | | / __|/ __/ _ \| '__/ _` | | _ < / _ \| __|
____) | __/ |_| | | | | |__| | \__ \ (_| (_) | | | (_| | | |_) | (_) | |_
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
_____ _ _ _____ _ _ ____ _
/ ____| | | | | | __ \(_) | | | _ \ | |
| (___ ___| |_| |__ | | | |_ ___ ___ ___ _ __ __| | | |_) | ___ | |_
\___ \ / _ \ __| '_ \ | | | | / __|/ __/ _ \| '__/ _` | | _ < / _ \| __|
____) | __/ |_| | | | | |__| | \__ \ (_| (_) | | | (_| | | |_) | (_) | |_
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
";
#endif
public static void Main(string[] args)
{
#if DEBUG
@@ -82,6 +68,7 @@ public static class Entry
}
Program.Startup(args).Wait();
}