Fixed bug in PluginManager at getting info about plugin & new console commands
This commit is contained in:
44
DiscordBot/Discord/Actions/Exit.cs
Normal file
44
DiscordBot/Discord/Actions/Exit.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace DiscordBot.Discord.Actions;
|
||||||
|
|
||||||
|
public class Exit : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "exit";
|
||||||
|
public string Description => "Exits the bot and saves the config. Use exit help for more info.";
|
||||||
|
public string Usage => "exit [help|force]";
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
if (args is null || args.Length == 0)
|
||||||
|
{
|
||||||
|
Config.Logger.Log("Exiting...", "Exit", LogLevel.INFO);
|
||||||
|
Config.Data.Save();
|
||||||
|
Environment.Exit(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch (args[0])
|
||||||
|
{
|
||||||
|
case "help":
|
||||||
|
Console.WriteLine("Usage : exit [help|force]");
|
||||||
|
Console.WriteLine("help : Displays this message");
|
||||||
|
Console.WriteLine("force : Exits the bot without saving the config");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "force":
|
||||||
|
Config.Logger.Log("Exiting...", "Exit", LogLevel.INFO);
|
||||||
|
Environment.Exit(0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Console.WriteLine("Invalid argument !");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
189
DiscordBot/Discord/Actions/Plugin.cs
Normal file
189
DiscordBot/Discord/Actions/Plugin.cs
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Loaders;
|
||||||
|
using PluginManager.Online;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace DiscordBot.Discord.Actions;
|
||||||
|
|
||||||
|
public class Plugin : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "plugin";
|
||||||
|
public string Description => "Manages plugins. Use plugin help for more info.";
|
||||||
|
public string Usage => "plugin [help|list|load|install]";
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
|
private bool pluginsLoaded = false;
|
||||||
|
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
if (args is null || args.Length == 0 || args[0] == "help")
|
||||||
|
{
|
||||||
|
Console.WriteLine("Usage : plugin [help|list|load|install|remove|update]");
|
||||||
|
Console.WriteLine("help : Displays this message");
|
||||||
|
Console.WriteLine("list : Lists all plugins");
|
||||||
|
Console.WriteLine("load : Loads all plugins");
|
||||||
|
Console.WriteLine("install : Installs a plugin");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (args[0])
|
||||||
|
{
|
||||||
|
case "list":
|
||||||
|
var manager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
case "load":
|
||||||
|
if (pluginsLoaded)
|
||||||
|
break;
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "install":
|
||||||
|
string pluginName = string.Join(' ', args, 1, args.Length-1);
|
||||||
|
if (string.IsNullOrEmpty(pluginName)|| pluginName.Length < 2)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Please specify a plugin name");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pluginManager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
string[] pluginData = await pluginManager.GetPluginLinkByName(pluginName);
|
||||||
|
if (pluginData == null || pluginData.Length == 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Plugin not found");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
string pluginType = pluginData[0];
|
||||||
|
string pluginLink = pluginData[1];
|
||||||
|
string pluginRequirements = pluginData[2];
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("Downloading plugin...");
|
||||||
|
//download plugin progress bar for linux and windows terminals
|
||||||
|
Utilities.Utilities.Spinner spinner = new Utilities.Utilities.Spinner();
|
||||||
|
spinner.Start();
|
||||||
|
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", null);
|
||||||
|
spinner.Stop();
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
if (pluginRequirements == string.Empty)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Plugin installed successfully");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Downloading plugin requirements...");
|
||||||
|
List<string> requirementsURLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||||
|
|
||||||
|
foreach (var requirement in requirementsURLs)
|
||||||
|
{
|
||||||
|
if(requirement.Length < 2)
|
||||||
|
continue;
|
||||||
|
string[] reqdata = requirement.Split(',');
|
||||||
|
string url = reqdata[0];
|
||||||
|
string filename = reqdata[1];
|
||||||
|
|
||||||
|
Console.WriteLine($"Downloading {filename}... ");
|
||||||
|
spinner.Start();
|
||||||
|
|
||||||
|
await ServerCom.DownloadFileAsync(url, $"./{filename}.dll", null);
|
||||||
|
spinner.Stop();
|
||||||
|
await Task.Delay(1000);
|
||||||
|
Console.WriteLine("Downloaded " + filename + " successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -113,14 +113,10 @@ namespace DiscordBot
|
|||||||
string path = "./Data/Resources/URLs.json";
|
string path = "./Data/Resources/URLs.json";
|
||||||
|
|
||||||
Directory.CreateDirectory("./Data/Resources");
|
Directory.CreateDirectory("./Data/Resources");
|
||||||
Utilities.Utilities.ProgressBar bar = new Utilities.Utilities.ProgressBar(Utilities.ProgressBarType.NORMAL){
|
Utilities.Utilities.Spinner spinner = new Utilities.Utilities.Spinner();
|
||||||
Max = 100,
|
spinner.Start();
|
||||||
Color = ConsoleColor.Green,
|
await ServerCom.DownloadFileAsync(url, path, null);
|
||||||
NoColor = true
|
spinner.Stop();
|
||||||
};
|
|
||||||
IProgress<float> downloadProgress = new Progress<float>(p => bar.Update(p));
|
|
||||||
await ServerCom.DownloadFileAsync(url, path, downloadProgress, null);
|
|
||||||
bar.Update(bar.Max);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,8 +174,6 @@ public class Program
|
|||||||
|
|
||||||
|
|
||||||
Console.WriteLine("Loading resources ...");
|
Console.WriteLine("Loading resources ...");
|
||||||
var main = new Utilities.Utilities.ProgressBar(ProgressBarType.NO_END);
|
|
||||||
main.Start();
|
|
||||||
|
|
||||||
if (Config.Data.ContainsKey("DeleteLogsAtStartup"))
|
if (Config.Data.ContainsKey("DeleteLogsAtStartup"))
|
||||||
if (Config.Data["DeleteLogsAtStartup"] == "true")
|
if (Config.Data["DeleteLogsAtStartup"] == "true")
|
||||||
@@ -203,7 +201,6 @@ public class Program
|
|||||||
|
|
||||||
|
|
||||||
var onlineSettingsList = await ServerCom.ReadTextFromURL(URLs["Versions"]);
|
var onlineSettingsList = await ServerCom.ReadTextFromURL(URLs["Versions"]);
|
||||||
main.Stop("Loaded online settings. Loading updates ...");
|
|
||||||
foreach (var key in onlineSettingsList)
|
foreach (var key in onlineSettingsList)
|
||||||
{
|
{
|
||||||
if (key.Length <= 3 || !key.Contains(' ')) continue;
|
if (key.Length <= 3 || !key.Contains(' ')) continue;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -201,6 +202,8 @@ public static class Utilities
|
|||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Spinner
|
public class Spinner
|
||||||
{
|
{
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
@@ -240,160 +243,4 @@ public static class Utilities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Progress bar object
|
|
||||||
/// </summary>
|
|
||||||
public class ProgressBar
|
|
||||||
{
|
|
||||||
private readonly int BarLength = 32;
|
|
||||||
|
|
||||||
private bool isRunning;
|
|
||||||
private int position = 1;
|
|
||||||
private bool positive = true;
|
|
||||||
|
|
||||||
public ProgressBar(ProgressBarType type)
|
|
||||||
{
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float Max { get; init; }
|
|
||||||
public ConsoleColor Color { get; init; }
|
|
||||||
public bool NoColor { get; init; }
|
|
||||||
public ProgressBarType type { get; set; }
|
|
||||||
|
|
||||||
public int TotalLength { get; private set; }
|
|
||||||
|
|
||||||
|
|
||||||
public async void Start()
|
|
||||||
{
|
|
||||||
Console.WriteLine();
|
|
||||||
if (type != ProgressBarType.NO_END)
|
|
||||||
throw new Exception("Only NO_END progress bar can use this method");
|
|
||||||
if (isRunning)
|
|
||||||
throw new Exception("This progress bar is already running");
|
|
||||||
|
|
||||||
isRunning = true;
|
|
||||||
while (isRunning)
|
|
||||||
{
|
|
||||||
UpdateNoEnd();
|
|
||||||
await Task.Delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void Start(string message)
|
|
||||||
{
|
|
||||||
if (type != ProgressBarType.NO_END)
|
|
||||||
throw new Exception("Only NO_END progress bar can use this method");
|
|
||||||
if (isRunning)
|
|
||||||
throw new Exception("This progress bar is already running");
|
|
||||||
|
|
||||||
isRunning = true;
|
|
||||||
|
|
||||||
TotalLength = message.Length + BarLength + 5;
|
|
||||||
while (isRunning)
|
|
||||||
{
|
|
||||||
UpdateNoEnd(message);
|
|
||||||
await Task.Delay(100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Stop()
|
|
||||||
{
|
|
||||||
if (type != ProgressBarType.NO_END)
|
|
||||||
throw new Exception("Only NO_END progress bar can use this method");
|
|
||||||
if (!isRunning)
|
|
||||||
throw new Exception("Can not stop a progressbar that did not start");
|
|
||||||
isRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Stop(string message)
|
|
||||||
{
|
|
||||||
Stop();
|
|
||||||
|
|
||||||
if (message is not null)
|
|
||||||
{
|
|
||||||
Console.CursorLeft = 0;
|
|
||||||
for (var i = 0; i < BarLength + message.Length + 1; i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.CursorLeft = 0;
|
|
||||||
Console.WriteLine(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Update(float progress)
|
|
||||||
{
|
|
||||||
if (type == ProgressBarType.NO_END)
|
|
||||||
throw new Exception("This function is for progress bars with end");
|
|
||||||
|
|
||||||
UpdateNormal(progress);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateNoEnd(string message)
|
|
||||||
{
|
|
||||||
Console.CursorLeft = 0;
|
|
||||||
Console.Write("[");
|
|
||||||
for (var i = 1; i <= position; i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.Write("<==()==>");
|
|
||||||
position += positive ? 1 : -1;
|
|
||||||
for (var i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.Write("] " + message);
|
|
||||||
|
|
||||||
|
|
||||||
if (position == BarLength - 1 || position == 1)
|
|
||||||
positive = !positive;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateNoEnd()
|
|
||||||
{
|
|
||||||
Console.CursorLeft = 0;
|
|
||||||
Console.Write("[");
|
|
||||||
for (var i = 1; i <= position; i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.Write("<==()==>");
|
|
||||||
position += positive ? 1 : -1;
|
|
||||||
for (var i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++)
|
|
||||||
Console.Write(" ");
|
|
||||||
Console.Write("]");
|
|
||||||
|
|
||||||
|
|
||||||
if (position == BarLength - 1 || position == 1)
|
|
||||||
positive = !positive;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateNormal(float progress)
|
|
||||||
{
|
|
||||||
Console.CursorLeft = 0;
|
|
||||||
Console.Write("[");
|
|
||||||
Console.CursorLeft = BarLength;
|
|
||||||
Console.Write("]");
|
|
||||||
Console.CursorLeft = 1;
|
|
||||||
var onechunk = 30.0f / Max;
|
|
||||||
|
|
||||||
var position = 1;
|
|
||||||
|
|
||||||
for (var i = 0; i < onechunk * progress; i++)
|
|
||||||
{
|
|
||||||
Console.BackgroundColor = NoColor ? ConsoleColor.Black : Color;
|
|
||||||
Console.CursorLeft = position++;
|
|
||||||
Console.Write("#");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = position; i < BarLength; i++)
|
|
||||||
{
|
|
||||||
Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray;
|
|
||||||
Console.CursorLeft = position++;
|
|
||||||
Console.Write(" ");
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.CursorLeft = BarLength + 4;
|
|
||||||
Console.BackgroundColor = ConsoleColor.Black;
|
|
||||||
if (progress.CanAproximateTo(Max))
|
|
||||||
Console.Write(progress + " % ✓");
|
|
||||||
else
|
|
||||||
Console.Write(MathF.Round(progress, 2) + " % ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -138,6 +138,6 @@ public class PluginsManager
|
|||||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new string[] { null!, null!, null! };
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user