Fixed some text and added some missing texts to commands. Added new command to clear screen and formated code.
This commit is contained in:
20
DiscordBot/Bot/Actions/Clear.cs
Normal file
20
DiscordBot/Bot/Actions/Clear.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
namespace DiscordBot.Bot.Actions;
|
||||||
|
|
||||||
|
public class Clear : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "clear";
|
||||||
|
public string Description => "Clears the console";
|
||||||
|
public string Usage => "clear";
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
|
public Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
Console.Clear();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,11 +12,12 @@ public class Exit : ICommandAction
|
|||||||
public string Description => "Exits the bot and saves the config. Use exit help for more info.";
|
public string Description => "Exits the bot and saves the config. Use exit help for more info.";
|
||||||
public string Usage => "exit [help|force]";
|
public string Usage => "exit [help|force]";
|
||||||
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
public async Task Execute(string[] args)
|
public async Task Execute(string[] args)
|
||||||
{
|
{
|
||||||
if (args is null || args.Length == 0)
|
if (args is null || args.Length == 0)
|
||||||
{
|
{
|
||||||
Config.Logger.Log("Exiting...", "Exit", LogLevel.INFO);
|
Config.Logger.Log("Exiting...", "Exit");
|
||||||
Config.Data.Save();
|
Config.Data.Save();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
@@ -31,7 +32,7 @@ public class Exit : ICommandAction
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "force":
|
case "force":
|
||||||
Config.Logger.Log("Exiting...", "Exit", LogLevel.INFO);
|
Config.Logger.Log("Exiting...", "Exit");
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DiscordBot.Utilities;
|
||||||
using PluginManager.Others;
|
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace DiscordBot.Bot.Actions
|
namespace DiscordBot.Bot.Actions;
|
||||||
|
|
||||||
|
public class Help : ICommandAction
|
||||||
{
|
{
|
||||||
public class Help : ICommandAction
|
|
||||||
{
|
|
||||||
public string ActionName => "help";
|
public string ActionName => "help";
|
||||||
|
|
||||||
public string Description => "Shows the list of commands and their usage";
|
public string Description => "Shows the list of commands and their usage";
|
||||||
@@ -30,13 +29,12 @@ namespace DiscordBot.Bot.Actions
|
|||||||
};
|
};
|
||||||
|
|
||||||
foreach (var a in Program.internalActionManager.Actions)
|
foreach (var a in Program.internalActionManager.Actions)
|
||||||
{
|
|
||||||
items.Add(new[] { a.Key, a.Value.Usage, a.Value.Description });
|
items.Add(new[] { a.Key, a.Value.Usage, a.Value.Description });
|
||||||
}
|
|
||||||
|
|
||||||
items.Add(new[] { "-", "-", "-" });
|
items.Add(new[] { "-", "-", "-" });
|
||||||
|
|
||||||
DiscordBot.Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED);
|
Utilities.Utilities.FormatAndAlignTable(items,
|
||||||
|
TableFormat.CENTER_EACH_COLUMN_BASED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,12 +49,12 @@ namespace DiscordBot.Bot.Actions
|
|||||||
{
|
{
|
||||||
new[] { "-", "-", "-" },
|
new[] { "-", "-", "-" },
|
||||||
new[] { "Command", "Usage", "Description" },
|
new[] { "Command", "Usage", "Description" },
|
||||||
new[] { "-", "-", "-"},
|
new[] { "-", "-", "-" },
|
||||||
new[] { action.ActionName, action.Usage, action.Description },
|
new[] { action.ActionName, action.Usage, action.Description },
|
||||||
new[] { "-", "-", "-" }
|
new[] { "-", "-", "-" }
|
||||||
};
|
};
|
||||||
|
|
||||||
DiscordBot.Utilities.Utilities.FormatAndAlignTable(actionData, Utilities.TableFormat.CENTER_EACH_COLUMN_BASED);
|
Utilities.Utilities.FormatAndAlignTable(actionData,
|
||||||
}
|
TableFormat.CENTER_EACH_COLUMN_BASED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DiscordBot.Utilities;
|
||||||
|
using PluginManager;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Loaders;
|
using PluginManager.Loaders;
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
@@ -10,13 +12,12 @@ namespace DiscordBot.Bot.Actions;
|
|||||||
|
|
||||||
public class Plugin : ICommandAction
|
public class Plugin : ICommandAction
|
||||||
{
|
{
|
||||||
|
private bool pluginsLoaded;
|
||||||
public string ActionName => "plugin";
|
public string ActionName => "plugin";
|
||||||
public string Description => "Manages plugins. Use plugin help for more info.";
|
public string Description => "Manages plugins. Use plugin help for more info.";
|
||||||
public string Usage => "plugin [help|list|load|install]";
|
public string Usage => "plugin [help|list|load|install]";
|
||||||
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
private bool pluginsLoaded = false;
|
|
||||||
|
|
||||||
public async Task Execute(string[] args)
|
public async Task Execute(string[] args)
|
||||||
{
|
{
|
||||||
if (args is null || args.Length == 0 || args[0] == "help")
|
if (args is null || args.Length == 0 || args[0] == "help")
|
||||||
@@ -33,31 +34,29 @@ public class Plugin : ICommandAction
|
|||||||
switch (args[0])
|
switch (args[0])
|
||||||
{
|
{
|
||||||
case "list":
|
case "list":
|
||||||
var manager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
var manager =
|
||||||
|
new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
|
||||||
var data = await manager.GetAvailablePlugins();
|
var data = await manager.GetAvailablePlugins();
|
||||||
var items = new List<string[]>
|
var items = new List<string[]>
|
||||||
{
|
{
|
||||||
new[] { "-", "-", "-", "-" },
|
new[] { "-", "-", "-", "-" },
|
||||||
new[] { "Name", "Type", "Description", "Required" },
|
new[] { "Name", "Description", "Type", "Version" },
|
||||||
new[] { "-", "-", "-", "-" }
|
new[] { "-", "-", "-", "-" }
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach (var plugin in data)
|
foreach (var plugin in data) items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
||||||
{
|
|
||||||
items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
|
||||||
}
|
|
||||||
|
|
||||||
items.Add(new[] { "-", "-", "-", "-" });
|
items.Add(new[] { "-", "-", "-", "-" });
|
||||||
|
|
||||||
Utilities.Utilities.FormatAndAlignTable(items, Utilities.TableFormat.DEFAULT);
|
Utilities.Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "load":
|
case "load":
|
||||||
if (pluginsLoaded)
|
if (pluginsLoaded)
|
||||||
break;
|
break;
|
||||||
var loader = new PluginLoader(PluginManager.Config.DiscordBot.client);
|
var loader = new PluginLoader(Config.DiscordBot.client);
|
||||||
var cc = Console.ForegroundColor;
|
var cc = Console.ForegroundColor;
|
||||||
loader.onCMDLoad += (name, typeName, success, exception) =>
|
loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||||
{
|
{
|
||||||
@@ -126,29 +125,30 @@ public class Plugin : ICommandAction
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "install":
|
case "install":
|
||||||
string pluginName = string.Join(' ', args, 1, args.Length-1);
|
var pluginName = string.Join(' ', args, 1, args.Length - 1);
|
||||||
if (string.IsNullOrEmpty(pluginName)|| pluginName.Length < 2)
|
if (string.IsNullOrEmpty(pluginName) || pluginName.Length < 2)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Please specify a plugin name");
|
Console.WriteLine("Please specify a plugin name");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var pluginManager = new PluginManager.Online.PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
var pluginManager =
|
||||||
string[] pluginData = await pluginManager.GetPluginLinkByName(pluginName);
|
new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||||
|
var pluginData = await pluginManager.GetPluginLinkByName(pluginName);
|
||||||
if (pluginData == null || pluginData.Length == 0)
|
if (pluginData == null || pluginData.Length == 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Plugin not found");
|
Console.WriteLine("Plugin not found");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
string pluginType = pluginData[0];
|
var pluginType = pluginData[0];
|
||||||
string pluginLink = pluginData[1];
|
var pluginLink = pluginData[1];
|
||||||
string pluginRequirements = pluginData[2];
|
var pluginRequirements = pluginData[2];
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("Downloading plugin...");
|
Console.WriteLine("Downloading plugin...");
|
||||||
//download plugin progress bar for linux and windows terminals
|
//download plugin progress bar for linux and windows terminals
|
||||||
Utilities.Utilities.Spinner spinner = new Utilities.Utilities.Spinner();
|
var spinner = new Utilities.Utilities.Spinner();
|
||||||
spinner.Start();
|
spinner.Start();
|
||||||
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", null);
|
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", null);
|
||||||
spinner.Stop();
|
spinner.Stop();
|
||||||
@@ -161,15 +161,15 @@ public class Plugin : ICommandAction
|
|||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Downloading plugin requirements...");
|
Console.WriteLine("Downloading plugin requirements...");
|
||||||
List<string> requirementsURLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
var requirementsURLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||||
|
|
||||||
foreach (var requirement in requirementsURLs)
|
foreach (var requirement in requirementsURLs)
|
||||||
{
|
{
|
||||||
if(requirement.Length < 2)
|
if (requirement.Length < 2)
|
||||||
continue;
|
continue;
|
||||||
string[] reqdata = requirement.Split(',');
|
var reqdata = requirement.Split(',');
|
||||||
string url = reqdata[0];
|
var url = reqdata[0];
|
||||||
string filename = reqdata[1];
|
var filename = reqdata[1];
|
||||||
|
|
||||||
Console.WriteLine($"Downloading {filename}... ");
|
Console.WriteLine($"Downloading {filename}... ");
|
||||||
spinner.Start();
|
spinner.Start();
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Discord;
|
using Discord;
|
||||||
|
|
||||||
using PluginManager;
|
using PluginManager;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Loaders;
|
using PluginManager.Loaders;
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Loaders;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace DiscordBot.Bot.Commands.SlashCommands;
|
namespace DiscordBot.Bot.Commands.SlashCommands;
|
||||||
|
|
||||||
public class Help : DBSlashCommand
|
public class Help : DBSlashCommand
|
||||||
@@ -17,7 +14,8 @@ public class Help : DBSlashCommand
|
|||||||
public string Description => "This command allows you to check all loaded commands";
|
public string Description => "This command allows you to check all loaded commands";
|
||||||
public bool canUseDM => true;
|
public bool canUseDM => true;
|
||||||
|
|
||||||
public List<SlashCommandOptionBuilder> Options => new()
|
public List<SlashCommandOptionBuilder> Options =>
|
||||||
|
new()
|
||||||
{
|
{
|
||||||
new SlashCommandOptionBuilder()
|
new SlashCommandOptionBuilder()
|
||||||
.WithName("command")
|
.WithName("command")
|
||||||
@@ -32,21 +30,17 @@ public class Help : DBSlashCommand
|
|||||||
|
|
||||||
embedBuilder.WithTitle("Help Command");
|
embedBuilder.WithTitle("Help Command");
|
||||||
embedBuilder.WithColor(Functions.RandomColor);
|
embedBuilder.WithColor(Functions.RandomColor);
|
||||||
var slashCommands = PluginManager.Loaders.PluginLoader.SlashCommands;
|
var slashCommands = PluginLoader.SlashCommands;
|
||||||
var options = context.Data.Options;
|
var options = context.Data.Options;
|
||||||
|
|
||||||
//Console.WriteLine("Options: " + options.Count);
|
//Console.WriteLine("Options: " + options.Count);
|
||||||
if (options is null || options.Count == 0)
|
if (options is null || options.Count == 0)
|
||||||
{
|
|
||||||
foreach (var slashCommand in slashCommands)
|
foreach (var slashCommand in slashCommands)
|
||||||
{
|
|
||||||
embedBuilder.AddField(slashCommand.Name, slashCommand.Description, true);
|
embedBuilder.AddField(slashCommand.Name, slashCommand.Description, true);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.Count > 0)
|
if (options.Count > 0)
|
||||||
{
|
{
|
||||||
string commandName = options.First().Name;
|
var commandName = options.First().Name;
|
||||||
var slashCommand = slashCommands.FirstOrDefault(x => x.Name == commandName);
|
var slashCommand = slashCommands.FirstOrDefault(x => x.Name == commandName);
|
||||||
if (slashCommand is null)
|
if (slashCommand is null)
|
||||||
{
|
{
|
||||||
@@ -54,7 +48,8 @@ public class Help : DBSlashCommand
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
embedBuilder.AddField(slashCommand.Name, slashCommand.canUseDM,true).WithDescription(slashCommand.Description);
|
embedBuilder.AddField(slashCommand.Name, slashCommand.canUseDM, true)
|
||||||
|
.WithDescription(slashCommand.Description);
|
||||||
}
|
}
|
||||||
|
|
||||||
await context.RespondAsync(embed: embedBuilder.Build());
|
await context.RespondAsync(embed: embedBuilder.Build());
|
||||||
|
|||||||
@@ -1,32 +1,27 @@
|
|||||||
using PluginManager.Others;
|
using System;
|
||||||
using System;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DiscordBot
|
namespace DiscordBot;
|
||||||
|
|
||||||
|
public class Entry
|
||||||
{
|
{
|
||||||
|
|
||||||
public class Entry
|
|
||||||
{
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
AppDomain currentDomain = AppDomain.CurrentDomain;
|
var currentDomain = AppDomain.CurrentDomain;
|
||||||
currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
|
currentDomain.AssemblyResolve += LoadFromSameFolder;
|
||||||
|
|
||||||
static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
|
static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
|
||||||
{
|
{
|
||||||
string folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "./Libraries");
|
var folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||||
string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
|
"./Libraries");
|
||||||
|
var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
|
||||||
if (!File.Exists(assemblyPath)) return null;
|
if (!File.Exists(assemblyPath)) return null;
|
||||||
Assembly assembly = Assembly.LoadFrom(assemblyPath);
|
var assembly = Assembly.LoadFrom(assemblyPath);
|
||||||
|
|
||||||
return assembly;
|
return assembly;
|
||||||
}
|
}
|
||||||
|
|
||||||
Program.Startup(args);
|
Program.Startup(args);
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using PluginManager;
|
using PluginManager;
|
||||||
using PluginManager.Others;
|
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
|
|
||||||
namespace DiscordBot
|
namespace DiscordBot;
|
||||||
{
|
|
||||||
public static class Installer
|
|
||||||
{
|
|
||||||
|
|
||||||
|
public static class Installer
|
||||||
|
{
|
||||||
public static void GenerateStartupConfig()
|
public static void GenerateStartupConfig()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Welcome to the SethBot installer !");
|
Console.WriteLine("Welcome to the SethBot installer !");
|
||||||
@@ -41,13 +36,11 @@ namespace DiscordBot
|
|||||||
Config.Data.Add("ServerID", serverId);
|
Config.Data.Add("ServerID", serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO);
|
Config.Logger.Log("Config Saved", "Installer");
|
||||||
|
|
||||||
Config.Data.Save();
|
Config.Data.Save();
|
||||||
|
|
||||||
Console.WriteLine("Config saved !");
|
Console.WriteLine("Config saved !");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task SetupPluginDatabase()
|
public static async Task SetupPluginDatabase()
|
||||||
@@ -56,7 +49,7 @@ namespace DiscordBot
|
|||||||
Console.WriteLine("Please select one option : ");
|
Console.WriteLine("Please select one option : ");
|
||||||
Console.WriteLine("1. Download the official database file");
|
Console.WriteLine("1. Download the official database file");
|
||||||
Console.WriteLine("2. Create a new (CUSTOM) database file");
|
Console.WriteLine("2. Create a new (CUSTOM) database file");
|
||||||
int choice = 0;
|
var choice = 0;
|
||||||
Console.Write("Choice : ");
|
Console.Write("Choice : ");
|
||||||
choice = int.Parse(Console.ReadLine());
|
choice = int.Parse(Console.ReadLine());
|
||||||
if (choice != 1 && choice != 2)
|
if (choice != 1 && choice != 2)
|
||||||
@@ -87,7 +80,7 @@ namespace DiscordBot
|
|||||||
if (answer == "y")
|
if (answer == "y")
|
||||||
{
|
{
|
||||||
Console.WriteLine("A new file will be generated at ./Data/Resources/URLs.json");
|
Console.WriteLine("A new file will be generated at ./Data/Resources/URLs.json");
|
||||||
System.Console.WriteLine("Please edit the file and restart the bot !");
|
Console.WriteLine("Please edit the file and restart the bot !");
|
||||||
Directory.CreateDirectory("./Data/Resources");
|
Directory.CreateDirectory("./Data/Resources");
|
||||||
await File.WriteAllTextAsync("./Data/Resources/URLs.json",
|
await File.WriteAllTextAsync("./Data/Resources/URLs.json",
|
||||||
@"
|
@"
|
||||||
@@ -103,20 +96,19 @@ namespace DiscordBot
|
|||||||
}
|
}
|
||||||
".Replace(" ", ""));
|
".Replace(" ", ""));
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task DownloadPluginDatabase(string url = "https://raw.githubusercontent.com/Wizzy69/SethDiscordBot/gh-pages/defaultURLs.json")
|
private static async Task DownloadPluginDatabase(
|
||||||
|
string url = "https://raw.githubusercontent.com/andreitdr/SethDiscordBot/gh-pages/defaultURLs.json")
|
||||||
{
|
{
|
||||||
string path = "./Data/Resources/URLs.json";
|
var path = "./Data/Resources/URLs.json";
|
||||||
|
|
||||||
Directory.CreateDirectory("./Data/Resources");
|
Directory.CreateDirectory("./Data/Resources");
|
||||||
Utilities.Utilities.Spinner spinner = new Utilities.Utilities.Spinner();
|
var spinner = new Utilities.Utilities.Spinner();
|
||||||
spinner.Start();
|
spinner.Start();
|
||||||
await ServerCom.DownloadFileAsync(url, path, null);
|
await ServerCom.DownloadFileAsync(url, path, null);
|
||||||
spinner.Stop();
|
spinner.Stop();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
using PluginManager;
|
|
||||||
using PluginManager.Bot;
|
using PluginManager.Bot;
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
using PluginManager.Online.Helpers;
|
using PluginManager.Online.Helpers;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
using PluginManager.Others.Actions;
|
||||||
using DiscordBot.Utilities;
|
|
||||||
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;
|
||||||
public static PluginManager.Others.Actions.InternalActionManager internalActionManager;
|
public static InternalActionManager internalActionManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main entry point for the application.
|
/// The main entry point for the application.
|
||||||
@@ -30,15 +26,13 @@ public class Program
|
|||||||
{
|
{
|
||||||
PreLoadComponents(args).Wait();
|
PreLoadComponents(args).Wait();
|
||||||
|
|
||||||
if (!Config.Data.ContainsKey("ServerID") || !Config.Data.ContainsKey("token") ||
|
if (!Data.ContainsKey("ServerID") || !Data.ContainsKey("token") ||
|
||||||
Config.Data["token"] == null ||
|
Data["token"] == null ||
|
||||||
(Config.Data["token"]?.Length != 70 && Config.Data["token"]?.Length != 59) ||
|
(Data["token"]?.Length != 70 && Data["token"]?.Length != 59) ||
|
||||||
!Config.Data.ContainsKey("prefix") || Config.Data["prefix"] == null ||
|
!Data.ContainsKey("prefix") || Data["prefix"] == null ||
|
||||||
Config.Data["prefix"]?.Length != 1 ||
|
Data["prefix"]?.Length != 1 ||
|
||||||
(args.Length == 1 && args[0] == "/reset"))
|
(args.Length == 1 && args[0] == "/reset"))
|
||||||
{
|
|
||||||
Installer.GenerateStartupConfig();
|
Installer.GenerateStartupConfig();
|
||||||
}
|
|
||||||
|
|
||||||
HandleInput(args.ToList()).Wait();
|
HandleInput(args.ToList()).Wait();
|
||||||
}
|
}
|
||||||
@@ -55,11 +49,11 @@ public class Program
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var cmd = Console.ReadLine();
|
string cmd = Console.ReadLine();
|
||||||
string[] args = cmd.Split(' ');
|
string[] args = cmd.Split(' ');
|
||||||
string command = args[0];
|
string command = args[0];
|
||||||
args = args.Skip(1).ToArray();
|
args = args.Skip(1).ToArray();
|
||||||
if(args.Length == 0)
|
if (args.Length == 0)
|
||||||
args = null;
|
args = null;
|
||||||
|
|
||||||
internalActionManager.Execute(command, args).Wait(); // Execute the command
|
internalActionManager.Execute(command, args).Wait(); // Execute the command
|
||||||
@@ -83,39 +77,39 @@ public class Program
|
|||||||
|
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
$"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}");
|
$"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}");
|
||||||
Console.WriteLine($"Git URL: {Config.Data["GitURL"]}");
|
Console.WriteLine($"Git URL: {Data["GitURL"]}");
|
||||||
|
|
||||||
Utilities.Utilities.WriteColorText(
|
Utilities.Utilities.WriteColorText(
|
||||||
"&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n");
|
"&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n");
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
|
||||||
if (Config.Data.ContainsKey("LaunchMessage"))
|
if (Data.ContainsKey("LaunchMessage"))
|
||||||
Utilities.Utilities.WriteColorText(Config.Data["LaunchMessage"]);
|
Utilities.Utilities.WriteColorText(Data["LaunchMessage"]);
|
||||||
|
|
||||||
|
|
||||||
Utilities.Utilities.WriteColorText(
|
Utilities.Utilities.WriteColorText(
|
||||||
"Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)");
|
"Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)");
|
||||||
|
|
||||||
Console.WriteLine("Running on " + Functions.GetOperatingSystem().ToString());
|
Console.WriteLine("Running on " + Functions.GetOperatingSystem());
|
||||||
Console.WriteLine("============================ LOG ============================");
|
Console.WriteLine("============================ LOG ============================");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string token = "";
|
var token = "";
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
if (File.Exists("./Data/Resources/token.txt")) token = File.ReadAllText("./Data/Resources/token.txt");
|
if (File.Exists("./Data/Resources/token.txt")) token = File.ReadAllText("./Data/Resources/token.txt");
|
||||||
else token = Config.Data["token"];
|
else token = Data["token"];
|
||||||
#else
|
#else
|
||||||
token = Config.Data["token"];
|
token = Config.Data["token"];
|
||||||
#endif
|
#endif
|
||||||
var prefix = Config.Data["prefix"];
|
var prefix = Data["prefix"];
|
||||||
var discordbooter = new Boot(token, prefix);
|
var discordbooter = new Boot(token, prefix);
|
||||||
await discordbooter.Awake();
|
await discordbooter.Awake();
|
||||||
return discordbooter;
|
return discordbooter;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,12 +120,11 @@ public class Program
|
|||||||
/// <param name="args">The arguments</param>
|
/// <param name="args">The arguments</param>
|
||||||
private static async Task HandleInput(List<string> args)
|
private static async Task HandleInput(List<string> args)
|
||||||
{
|
{
|
||||||
|
|
||||||
Console.WriteLine("Loading Core ...");
|
Console.WriteLine("Loading Core ...");
|
||||||
|
|
||||||
//Handle arguments here:
|
//Handle arguments here:
|
||||||
|
|
||||||
if(args.Contains("--gui"))
|
if (args.Contains("--gui"))
|
||||||
{
|
{
|
||||||
// GUI not implemented yet
|
// GUI not implemented yet
|
||||||
Console.WriteLine("GUI not implemented yet");
|
Console.WriteLine("GUI not implemented yet");
|
||||||
@@ -143,7 +136,7 @@ public class Program
|
|||||||
var b = await StartNoGui();
|
var b = await StartNoGui();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
internalActionManager = new PluginManager.Others.Actions.InternalActionManager("./Data/Actions", "*.dll");
|
internalActionManager = new InternalActionManager("./Data/Actions", "*.dll");
|
||||||
await internalActionManager.Initialize();
|
await internalActionManager.Initialize();
|
||||||
|
|
||||||
NoGUI();
|
NoGUI();
|
||||||
@@ -152,42 +145,40 @@ public class Program
|
|||||||
{
|
{
|
||||||
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
|
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
|
||||||
{
|
{
|
||||||
if (Config.Data.ContainsKey("LaunchMessage"))
|
if (Data.ContainsKey("LaunchMessage"))
|
||||||
Config.Data.Add("LaunchMessage",
|
Data.Add("LaunchMessage",
|
||||||
"An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !");
|
"An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !");
|
||||||
Config.Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", "Bot", LogLevel.ERROR);
|
Logger
|
||||||
|
.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
|
||||||
|
"Bot", LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task PreLoadComponents(string[] args)
|
private static async Task PreLoadComponents(string[] args)
|
||||||
{
|
{
|
||||||
|
await Initialize();
|
||||||
await Config.Initialize();
|
|
||||||
|
|
||||||
if (!Directory.Exists("./Data/Resources") || !File.Exists("./Data/Resources/URLs.json"))
|
if (!Directory.Exists("./Data/Resources") || !File.Exists("./Data/Resources/URLs.json"))
|
||||||
{
|
|
||||||
await Installer.SetupPluginDatabase();
|
await Installer.SetupPluginDatabase();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
URLs = new Json<string, string>("./Data/Resources/URLs.json");
|
URLs = new Json<string, string>("./Data/Resources/URLs.json");
|
||||||
|
|
||||||
Config.Logger.LogEvent += (message, type) => { Console.WriteLine(message); };
|
Logger.LogEvent += (message, type) => { Console.WriteLine(message); };
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("Loading resources ...");
|
Console.WriteLine("Loading resources ...");
|
||||||
|
|
||||||
if (Config.Data.ContainsKey("DeleteLogsAtStartup"))
|
if (Data.ContainsKey("DeleteLogsAtStartup"))
|
||||||
if (Config.Data["DeleteLogsAtStartup"] == "true")
|
if (Data["DeleteLogsAtStartup"] == "true")
|
||||||
foreach (var file in Directory.GetFiles("./Output/Logs/"))
|
foreach (var file in Directory.GetFiles("./Output/Logs/"))
|
||||||
File.Delete(file);
|
File.Delete(file);
|
||||||
var OnlineDefaultKeys =
|
var OnlineDefaultKeys =
|
||||||
await ServerCom.ReadTextFromURL(URLs["SetupKeys"]);
|
await ServerCom.ReadTextFromURL(URLs["SetupKeys"]);
|
||||||
|
|
||||||
|
|
||||||
Config.Data["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
Data["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||||
|
|
||||||
foreach (var key in OnlineDefaultKeys)
|
foreach (var key in OnlineDefaultKeys)
|
||||||
{
|
{
|
||||||
@@ -195,11 +186,11 @@ public class Program
|
|||||||
var s = key.Split(' ');
|
var s = key.Split(' ');
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Config.Data[s[0]] = s[1];
|
Data[s[0]] = s[1];
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,27 +204,27 @@ public class Program
|
|||||||
switch (s[0])
|
switch (s[0])
|
||||||
{
|
{
|
||||||
case "CurrentVersion":
|
case "CurrentVersion":
|
||||||
var currentVersion = Config.Data["Version"];
|
var currentVersion = Data["Version"];
|
||||||
var newVersion = s[1];
|
var newVersion = s[1];
|
||||||
if(new VersionString(newVersion) != new VersionString(newVersion))
|
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"]);
|
var changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
|
||||||
foreach (var item in changeLog)
|
foreach (var item in changeLog)
|
||||||
Utilities.Utilities.WriteColorText(item);
|
Utilities.Utilities.WriteColorText(item);
|
||||||
Console.WriteLine("Current version: " + currentVersion);
|
Console.WriteLine("Current version: " + currentVersion);
|
||||||
Console.WriteLine("Latest version: " + newVersion);
|
Console.WriteLine("Latest version: " + newVersion);
|
||||||
|
|
||||||
Console.WriteLine($"Download from here: https://github.com/andreitdr/SethDiscordBot/releases");
|
Console.WriteLine("Download from here: https://github.com/andreitdr/SethDiscordBot/releases");
|
||||||
|
|
||||||
Console.WriteLine("Press any key to continue ...");
|
Console.WriteLine("Press any key to continue ...");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
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 PluginManager;
|
|
||||||
|
|
||||||
namespace DiscordBot.Utilities;
|
namespace DiscordBot.Utilities;
|
||||||
|
|
||||||
public static class Utilities
|
public static class Utilities
|
||||||
{
|
{
|
||||||
private static Dictionary<char, ConsoleColor> Colors = new()
|
private static readonly Dictionary<char, ConsoleColor> Colors = new()
|
||||||
{
|
{
|
||||||
{ 'g', ConsoleColor.Green },
|
{ 'g', ConsoleColor.Green },
|
||||||
{ 'b', ConsoleColor.Blue },
|
{ 'b', ConsoleColor.Blue },
|
||||||
@@ -19,7 +15,7 @@ public static class Utilities
|
|||||||
{ 'y', ConsoleColor.Yellow }
|
{ 'y', ConsoleColor.Yellow }
|
||||||
};
|
};
|
||||||
|
|
||||||
private static char ColorPrefix = '&';
|
private static readonly char ColorPrefix = '&';
|
||||||
|
|
||||||
|
|
||||||
private static bool CanAproximateTo(this float f, float y)
|
private static bool CanAproximateTo(this float f, float y)
|
||||||
@@ -203,25 +199,25 @@ public static class Utilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Spinner
|
public class Spinner
|
||||||
{
|
{
|
||||||
private Thread thread;
|
|
||||||
private bool isRunning;
|
|
||||||
private readonly string[] Sequence;
|
private readonly string[] Sequence;
|
||||||
|
private bool isRunning;
|
||||||
private int position;
|
private int position;
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
public Spinner()
|
public Spinner()
|
||||||
{
|
{
|
||||||
Sequence = new[] {"|", "/", "-", "\\"};
|
Sequence = new[] { "|", "/", "-", "\\" };
|
||||||
position = 0;
|
position = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
Console.CursorVisible = false;
|
Console.CursorVisible = false;
|
||||||
isRunning=true;
|
isRunning = true;
|
||||||
thread = new Thread(() => {
|
thread = new Thread(() =>
|
||||||
|
{
|
||||||
while (isRunning)
|
while (isRunning)
|
||||||
{
|
{
|
||||||
Console.Write("\r" + Sequence[position]);
|
Console.Write("\r" + Sequence[position]);
|
||||||
@@ -233,14 +229,12 @@ public static class Utilities
|
|||||||
});
|
});
|
||||||
|
|
||||||
thread.Start();
|
thread.Start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
isRunning=false;
|
isRunning = false;
|
||||||
Console.CursorVisible = true;
|
Console.CursorVisible = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
using System;
|
namespace DiscordBot.Utilities;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DiscordBot.Utilities
|
public enum TableFormat
|
||||||
{
|
{
|
||||||
public enum TableFormat
|
|
||||||
{
|
|
||||||
CENTER_EACH_COLUMN_BASED,
|
CENTER_EACH_COLUMN_BASED,
|
||||||
CENTER_OVERALL_LENGTH,
|
CENTER_OVERALL_LENGTH,
|
||||||
DEFAULT
|
DEFAULT
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
using System.Net.Mime;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Bot;
|
namespace PluginManager.Bot;
|
||||||
|
|
||||||
@@ -51,20 +49,22 @@ public class Boot
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if the bot is ready
|
/// Checks if the bot is ready
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value> true if the bot is ready, othwerwise false </value>
|
/// <value> true if the bot is ready, otherwise false </value>
|
||||||
public bool isReady { get; private set; }
|
public bool isReady { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The start method for the bot. This method is used to load the bot
|
/// The start method for the bot. This method is used to load the bot
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="config">The discord socket config. If null then the default one will be applied (AlwaysDownloadUsers=true, UseInteractionSnowflakeDate=false, GatewayIntents=GatewayIntents.All)</param>
|
/// <param name="config">
|
||||||
|
/// The discord socket config. If null then the default one will be applied (AlwaysDownloadUsers=true,
|
||||||
|
/// UseInteractionSnowflakeDate=false, GatewayIntents=GatewayIntents.All)
|
||||||
|
/// </param>
|
||||||
/// <returns>Task</returns>
|
/// <returns>Task</returns>
|
||||||
public async Task Awake(DiscordSocketConfig? config = null)
|
public async Task Awake(DiscordSocketConfig? config = null)
|
||||||
{
|
{
|
||||||
if (config is null)
|
if (config is null)
|
||||||
config = new DiscordSocketConfig
|
config = new DiscordSocketConfig
|
||||||
{
|
{
|
||||||
|
|
||||||
AlwaysDownloadUsers = true,
|
AlwaysDownloadUsers = true,
|
||||||
|
|
||||||
//Disable system clock checkup (for responses at slash commands)
|
//Disable system clock checkup (for responses at slash commands)
|
||||||
@@ -86,7 +86,6 @@ public class Boot
|
|||||||
await commandServiceHandler.InstallCommandsAsync();
|
await commandServiceHandler.InstallCommandsAsync();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await Task.Delay(2000);
|
await Task.Delay(2000);
|
||||||
|
|
||||||
Config._DiscordBotClient = this;
|
Config._DiscordBotClient = this;
|
||||||
@@ -109,7 +108,8 @@ public class Boot
|
|||||||
if (arg.Message.Contains("401"))
|
if (arg.Message.Contains("401"))
|
||||||
{
|
{
|
||||||
Config.Data.Remove("token");
|
Config.Data.Remove("token");
|
||||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR);
|
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this,
|
||||||
|
LogLevel.ERROR);
|
||||||
Config.Data.Save();
|
Config.Data.Save();
|
||||||
await Task.Delay(4000);
|
await Task.Delay(4000);
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
@@ -140,7 +140,7 @@ public class Boot
|
|||||||
{
|
{
|
||||||
case LogSeverity.Error:
|
case LogSeverity.Error:
|
||||||
case LogSeverity.Critical:
|
case LogSeverity.Critical:
|
||||||
Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR);
|
Config.Logger.Log(message.Message, this, LogLevel.ERROR);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ using System;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
@@ -29,7 +28,6 @@ internal class CommandHandler
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
this.commandService = commandService;
|
this.commandService = commandService;
|
||||||
this.botPrefix = botPrefix;
|
this.botPrefix = botPrefix;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -63,7 +61,6 @@ internal class CommandHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -73,7 +70,6 @@ internal class CommandHandler
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task MessageHandler(SocketMessage Message)
|
private async Task MessageHandler(SocketMessage Message)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Message.Author.IsBot)
|
if (Message.Author.IsBot)
|
||||||
@@ -97,17 +93,21 @@ internal class CommandHandler
|
|||||||
await commandService.ExecuteAsync(context, argPos, null);
|
await commandService.ExecuteAsync(context, argPos, null);
|
||||||
|
|
||||||
DBCommand? plugin;
|
DBCommand? plugin;
|
||||||
string cleanMessage = "";
|
var cleanMessage = "";
|
||||||
|
|
||||||
if (message.HasMentionPrefix(client.CurrentUser, ref argPos))
|
if (message.HasMentionPrefix(client.CurrentUser, ref argPos))
|
||||||
{
|
{
|
||||||
string mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
var mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
||||||
|
|
||||||
plugin = PluginLoader.Commands!
|
plugin = PluginLoader.Commands!
|
||||||
.FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
|
.FirstOrDefault(plug => plug.Command ==
|
||||||
|
message.Content.Substring(mentionPrefix.Length + 1)
|
||||||
|
.Split(' ')[0] ||
|
||||||
(
|
(
|
||||||
plug.Aliases is not null &&
|
plug.Aliases is not null &&
|
||||||
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
|
plug.Aliases.Contains(message.CleanContent
|
||||||
|
.Substring(mentionPrefix.Length + 1)
|
||||||
|
.Split(' ')[0])
|
||||||
));
|
));
|
||||||
|
|
||||||
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
|
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
|
||||||
@@ -116,23 +116,27 @@ internal class CommandHandler
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
plugin = PluginLoader.Commands!
|
plugin = PluginLoader.Commands!
|
||||||
.FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
.FirstOrDefault(p => p.Command ==
|
||||||
|
message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||||
(p.Aliases is not null &&
|
(p.Aliases is not null &&
|
||||||
p.Aliases.Contains(
|
p.Aliases.Contains(
|
||||||
message.Content.Split(' ')[0].Substring(botPrefix.Length))));
|
message.Content.Split(' ')[0]
|
||||||
|
.Substring(botPrefix.Length))));
|
||||||
cleanMessage = message.Content.Substring(botPrefix.Length);
|
cleanMessage = message.Content.Substring(botPrefix.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (plugin is null)
|
if (plugin is null)
|
||||||
throw new Exception($"Failed to run command ! " + message.CleanContent + " (user: " + context.Message.Author.Username + " - " + context.Message.Author.Id + ")");
|
throw new Exception("Failed to run command ! " + message.CleanContent + " (user: " +
|
||||||
|
context.Message.Author.Username + " - " + context.Message.Author.Id + ")");
|
||||||
|
|
||||||
if (plugin.requireAdmin && !context.Message.Author.isAdmin())
|
if (plugin.requireAdmin && !context.Message.Author.isAdmin())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
string[] split = cleanMessage.Split(' ');
|
var split = cleanMessage.Split(' ');
|
||||||
|
|
||||||
string[]? argsClean = null;
|
string[]? argsClean = null;
|
||||||
if(split.Length > 1)
|
if (split.Length > 1)
|
||||||
argsClean = string.Join(' ', split, 1, split.Length-1).Split(' ');
|
argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' ');
|
||||||
|
|
||||||
DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean);
|
DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean);
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,24 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using PluginManager.Others;
|
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using PluginManager.Bot;
|
||||||
|
using PluginManager.Others;
|
||||||
using PluginManager.Others.Logger;
|
using PluginManager.Others.Logger;
|
||||||
|
|
||||||
namespace PluginManager;
|
namespace PluginManager;
|
||||||
|
|
||||||
public class Config
|
public class Config
|
||||||
{
|
{
|
||||||
private static bool IsLoaded = false;
|
private static bool IsLoaded;
|
||||||
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;
|
internal static Boot? _DiscordBotClient;
|
||||||
|
|
||||||
public static Bot.Boot? DiscordBot
|
public static Boot? DiscordBot => _DiscordBotClient;
|
||||||
{
|
|
||||||
get => _DiscordBotClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task Initialize()
|
public static async Task Initialize()
|
||||||
{
|
{
|
||||||
@@ -37,8 +34,8 @@ public class Config
|
|||||||
Data = new Json<string, string>("./Data/Resources/config.json");
|
Data = new Json<string, string>("./Data/Resources/config.json");
|
||||||
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
|
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
|
||||||
|
|
||||||
Config.Data["LogFolder"] = "./Data/Logs/Logs";
|
Data["LogFolder"] = "./Data/Logs/Logs";
|
||||||
Config.Data["ErrorFolder"] = "./Data/Logs/Errors";
|
Data["ErrorFolder"] = "./Data/Logs/Errors";
|
||||||
|
|
||||||
Logger = new DBLogger();
|
Logger = new DBLogger();
|
||||||
|
|
||||||
@@ -51,8 +48,8 @@ public class Config
|
|||||||
|
|
||||||
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
|
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
|
||||||
{
|
{
|
||||||
protected IDictionary<TKey, TValue> _dictionary;
|
|
||||||
private readonly string _file = "";
|
private readonly string _file = "";
|
||||||
|
private readonly IDictionary<TKey, TValue>? _dictionary;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Empty constructor
|
/// Empty constructor
|
||||||
@@ -65,13 +62,7 @@ public class Config
|
|||||||
public Json(string file)
|
public Json(string file)
|
||||||
{
|
{
|
||||||
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
|
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
|
||||||
this._file = file;
|
_file = file;
|
||||||
}
|
|
||||||
|
|
||||||
public async void Save()
|
|
||||||
{
|
|
||||||
if(!string.IsNullOrEmpty(_file))
|
|
||||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Add(TKey key, TValue value)
|
public virtual void Add(TKey key, TValue value)
|
||||||
@@ -101,9 +92,9 @@ public class Config
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_dictionary.TryGetValue(key, out TValue value)) return value;
|
if (_dictionary.TryGetValue(key, out var value)) return value;
|
||||||
throw new Exception("Key not found in dictionary " + key.ToString() + " (Json )" + this.GetType().Name + ")");
|
throw new Exception("Key not found in dictionary " + key + " (Json )" + GetType().Name +
|
||||||
|
")");
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
@@ -118,30 +109,6 @@ public class Config
|
|||||||
return _dictionary.TryGetValue(key, out value);
|
return _dictionary.TryGetValue(key, out value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
|
||||||
{
|
|
||||||
if (!File.Exists(file))
|
|
||||||
{
|
|
||||||
var dictionary = new Dictionary<TKey, TValue>();
|
|
||||||
await Functions.SaveToJsonFile(file, _dictionary);
|
|
||||||
return dictionary;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
|
|
||||||
if (d is null)
|
|
||||||
throw new Exception("Failed to read config file");
|
|
||||||
|
|
||||||
return d;
|
|
||||||
}catch (Exception ex)
|
|
||||||
{
|
|
||||||
File.Delete(file);
|
|
||||||
return new Dictionary<TKey, TValue>();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Remove(TKey key)
|
public bool Remove(TKey key)
|
||||||
{
|
{
|
||||||
return _dictionary.Remove(key);
|
return _dictionary.Remove(key);
|
||||||
@@ -176,6 +143,35 @@ public class Config
|
|||||||
{
|
{
|
||||||
return ((IEnumerable)_dictionary).GetEnumerator();
|
return ((IEnumerable)_dictionary).GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async void Save()
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(_file))
|
||||||
|
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
||||||
|
{
|
||||||
|
if (!File.Exists(file))
|
||||||
|
{
|
||||||
|
var dictionary = new Dictionary<TKey, TValue>();
|
||||||
|
await Functions.SaveToJsonFile(file, _dictionary);
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
|
||||||
|
if (d is null)
|
||||||
|
throw new Exception("Failed to read config file");
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
File.Delete(file);
|
||||||
|
return new Dictionary<TKey, TValue>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PluginManager.Database
|
namespace PluginManager.Database;
|
||||||
|
|
||||||
|
public class SqlDatabase
|
||||||
{
|
{
|
||||||
public class SqlDatabase
|
private readonly SQLiteConnection Connection;
|
||||||
{
|
private readonly string ConnectionString;
|
||||||
private string ConnectionString;
|
|
||||||
private SQLiteConnection Connection;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize a SQL connection by specifing its private path
|
/// Initialize a SQL connection by specifing its private path
|
||||||
@@ -44,8 +46,8 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InsertAsync(string tableName, params string[] values)
|
public async Task InsertAsync(string tableName, params string[] values)
|
||||||
{
|
{
|
||||||
string query = $"INSERT INTO {tableName} VALUES (";
|
var query = $"INSERT INTO {tableName} VALUES (";
|
||||||
for (int i = 0; i < values.Length; i++)
|
for (var i = 0; i < values.Length; i++)
|
||||||
{
|
{
|
||||||
query += $"'{values[i]}'";
|
query += $"'{values[i]}'";
|
||||||
if (i != values.Length - 1)
|
if (i != values.Length - 1)
|
||||||
@@ -54,7 +56,7 @@ namespace PluginManager.Database
|
|||||||
|
|
||||||
query += ")";
|
query += ")";
|
||||||
|
|
||||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +70,8 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public void Insert(string tableName, params string[] values)
|
public void Insert(string tableName, params string[] values)
|
||||||
{
|
{
|
||||||
string query = $"INSERT INTO {tableName} VALUES (";
|
var query = $"INSERT INTO {tableName} VALUES (";
|
||||||
for (int i = 0; i < values.Length; i++)
|
for (var i = 0; i < values.Length; i++)
|
||||||
{
|
{
|
||||||
query += $"'{values[i]}'";
|
query += $"'{values[i]}'";
|
||||||
if (i != values.Length - 1)
|
if (i != values.Length - 1)
|
||||||
@@ -78,7 +80,7 @@ namespace PluginManager.Database
|
|||||||
|
|
||||||
query += ")";
|
query += ")";
|
||||||
|
|
||||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +93,9 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue)
|
public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue)
|
||||||
{
|
{
|
||||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||||
|
|
||||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,9 +108,9 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public void RemoveKey(string tableName, string KeyName, string KeyValue)
|
public void RemoveKey(string tableName, string KeyName, string KeyValue)
|
||||||
{
|
{
|
||||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||||
|
|
||||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +123,7 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<bool> KeyExistsAsync(string tableName, string keyName, string KeyValue)
|
public async Task<bool> KeyExistsAsync(string tableName, string keyName, string KeyValue)
|
||||||
{
|
{
|
||||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
var query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||||
|
|
||||||
if (await ReadDataAsync(query) is not null)
|
if (await ReadDataAsync(query) is not null)
|
||||||
return true;
|
return true;
|
||||||
@@ -138,7 +140,7 @@ namespace PluginManager.Database
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public bool KeyExists(string tableName, string keyName, string KeyValue)
|
public bool KeyExists(string tableName, string keyName, string KeyValue)
|
||||||
{
|
{
|
||||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
var query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||||
|
|
||||||
if (ReadData(query) is not null)
|
if (ReadData(query) is not null)
|
||||||
return true;
|
return true;
|
||||||
@@ -154,12 +156,12 @@ namespace PluginManager.Database
|
|||||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||||
|
public async Task SetValueAsync(
|
||||||
public async Task SetValueAsync(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||||
string ResultColumnValue)
|
string ResultColumnValue)
|
||||||
{
|
{
|
||||||
if (!await TableExistsAsync(tableName))
|
if (!await TableExistsAsync(tableName))
|
||||||
throw new System.Exception($"Table {tableName} does not exist");
|
throw new Exception($"Table {tableName} does not exist");
|
||||||
|
|
||||||
await ExecuteAsync(
|
await ExecuteAsync(
|
||||||
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||||
@@ -173,12 +175,12 @@ namespace PluginManager.Database
|
|||||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||||
|
public void SetValue(
|
||||||
public void SetValue(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||||
string ResultColumnValue)
|
string ResultColumnValue)
|
||||||
{
|
{
|
||||||
if (!TableExists(tableName))
|
if (!TableExists(tableName))
|
||||||
throw new System.Exception($"Table {tableName} does not exist");
|
throw new Exception($"Table {tableName} does not exist");
|
||||||
|
|
||||||
Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||||
}
|
}
|
||||||
@@ -191,11 +193,12 @@ namespace PluginManager.Database
|
|||||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||||
/// <param name="ResultColumnName">The column that has the result</param>
|
/// <param name="ResultColumnName">The column that has the result</param>
|
||||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||||
public async Task<string?> GetValueAsync(string tableName, string keyName, string KeyValue,
|
public async Task<string?> GetValueAsync(
|
||||||
|
string tableName, string keyName, string KeyValue,
|
||||||
string ResultColumnName)
|
string ResultColumnName)
|
||||||
{
|
{
|
||||||
if (!await TableExistsAsync(tableName))
|
if (!await TableExistsAsync(tableName))
|
||||||
throw new System.Exception($"Table {tableName} does not exist");
|
throw new Exception($"Table {tableName} does not exist");
|
||||||
|
|
||||||
return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||||
}
|
}
|
||||||
@@ -208,11 +211,10 @@ namespace PluginManager.Database
|
|||||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||||
/// <param name="ResultColumnName">The column that has the result</param>
|
/// <param name="ResultColumnName">The column that has the result</param>
|
||||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||||
|
|
||||||
public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName)
|
public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName)
|
||||||
{
|
{
|
||||||
if (!TableExists(tableName))
|
if (!TableExists(tableName))
|
||||||
throw new System.Exception($"Table {tableName} does not exist");
|
throw new Exception($"Table {tableName} does not exist");
|
||||||
|
|
||||||
return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||||
}
|
}
|
||||||
@@ -239,18 +241,16 @@ namespace PluginManager.Database
|
|||||||
command.CommandText = $"SELECT * FROM {tableName}";
|
command.CommandText = $"SELECT * FROM {tableName}";
|
||||||
var reader = await command.ExecuteReaderAsync();
|
var reader = await command.ExecuteReaderAsync();
|
||||||
var tableColumns = new List<string>();
|
var tableColumns = new List<string>();
|
||||||
for (int i = 0; i < reader.FieldCount; i++)
|
for (var i = 0; i < reader.FieldCount; i++)
|
||||||
tableColumns.Add(reader.GetName(i));
|
tableColumns.Add(reader.GetName(i));
|
||||||
|
|
||||||
foreach (var column in columns)
|
foreach (var column in columns)
|
||||||
{
|
|
||||||
if (!tableColumns.Contains(column))
|
if (!tableColumns.Contains(column))
|
||||||
{
|
{
|
||||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Change the structure of a table by adding new columns
|
/// Change the structure of a table by adding new columns
|
||||||
@@ -259,25 +259,22 @@ namespace PluginManager.Database
|
|||||||
/// <param name="columns">The columns to be added</param>
|
/// <param name="columns">The columns to be added</param>
|
||||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
||||||
public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT")
|
public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT")
|
||||||
{
|
{
|
||||||
var command = Connection.CreateCommand();
|
var command = Connection.CreateCommand();
|
||||||
command.CommandText = $"SELECT * FROM {tableName}";
|
command.CommandText = $"SELECT * FROM {tableName}";
|
||||||
var reader = command.ExecuteReader();
|
var reader = command.ExecuteReader();
|
||||||
var tableColumns = new List<string>();
|
var tableColumns = new List<string>();
|
||||||
for (int i = 0; i < reader.FieldCount; i++)
|
for (var i = 0; i < reader.FieldCount; i++)
|
||||||
tableColumns.Add(reader.GetName(i));
|
tableColumns.Add(reader.GetName(i));
|
||||||
|
|
||||||
foreach (var column in columns)
|
foreach (var column in columns)
|
||||||
{
|
|
||||||
if (!tableColumns.Contains(column))
|
if (!tableColumns.Contains(column))
|
||||||
{
|
{
|
||||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||||
command.ExecuteNonQuery();
|
command.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if a table exists
|
/// Check if a table exists
|
||||||
@@ -317,7 +314,6 @@ namespace PluginManager.Database
|
|||||||
/// <param name="tableName">The table name</param>
|
/// <param name="tableName">The table name</param>
|
||||||
/// <param name="columns">The columns of the table</param>
|
/// <param name="columns">The columns of the table</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
||||||
public async Task CreateTableAsync(string tableName, params string[] columns)
|
public async Task CreateTableAsync(string tableName, params string[] columns)
|
||||||
{
|
{
|
||||||
var cmd = Connection.CreateCommand();
|
var cmd = Connection.CreateCommand();
|
||||||
@@ -331,7 +327,6 @@ namespace PluginManager.Database
|
|||||||
/// <param name="tableName">The table name</param>
|
/// <param name="tableName">The table name</param>
|
||||||
/// <param name="columns">The columns of the table</param>
|
/// <param name="columns">The columns of the table</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
||||||
public void CreateTable(string tableName, params string[] columns)
|
public void CreateTable(string tableName, params string[] columns)
|
||||||
{
|
{
|
||||||
var cmd = Connection.CreateCommand();
|
var cmd = Connection.CreateCommand();
|
||||||
@@ -346,10 +341,10 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The number of rows that the query modified</returns>
|
/// <returns>The number of rows that the query modified</returns>
|
||||||
public async Task<int> ExecuteAsync(string query)
|
public async Task<int> ExecuteAsync(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
await Connection.OpenAsync();
|
await Connection.OpenAsync();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
int answer = await command.ExecuteNonQueryAsync();
|
var answer = await command.ExecuteNonQueryAsync();
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,10 +355,10 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The number of rows that the query modified</returns>
|
/// <returns>The number of rows that the query modified</returns>
|
||||||
public int Execute(string query)
|
public int Execute(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
Connection.Open();
|
Connection.Open();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
int r = command.ExecuteNonQuery();
|
var r = command.ExecuteNonQuery();
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@@ -375,12 +370,12 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||||
public async Task<string?> ReadDataAsync(string query)
|
public async Task<string?> ReadDataAsync(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
await Connection.OpenAsync();
|
await Connection.OpenAsync();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
var reader = await command.ExecuteReaderAsync();
|
var reader = await command.ExecuteReaderAsync();
|
||||||
|
|
||||||
object[] values = new object[reader.FieldCount];
|
var values = new object[reader.FieldCount];
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
{
|
{
|
||||||
reader.GetValues(values);
|
reader.GetValues(values);
|
||||||
@@ -397,12 +392,12 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||||
public string? ReadData(string query)
|
public string? ReadData(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
Connection.Open();
|
Connection.Open();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
var reader = command.ExecuteReader();
|
var reader = command.ExecuteReader();
|
||||||
|
|
||||||
object[] values = new object[reader.FieldCount];
|
var values = new object[reader.FieldCount];
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
{
|
{
|
||||||
reader.GetValues(values);
|
reader.GetValues(values);
|
||||||
@@ -419,12 +414,12 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The first row as separated items</returns>
|
/// <returns>The first row as separated items</returns>
|
||||||
public async Task<object[]?> ReadDataArrayAsync(string query)
|
public async Task<object[]?> ReadDataArrayAsync(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
await Connection.OpenAsync();
|
await Connection.OpenAsync();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
var reader = await command.ExecuteReaderAsync();
|
var reader = await command.ExecuteReaderAsync();
|
||||||
|
|
||||||
object[] values = new object[reader.FieldCount];
|
var values = new object[reader.FieldCount];
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
{
|
{
|
||||||
reader.GetValues(values);
|
reader.GetValues(values);
|
||||||
@@ -442,12 +437,12 @@ namespace PluginManager.Database
|
|||||||
/// <returns>The first row as separated items</returns>
|
/// <returns>The first row as separated items</returns>
|
||||||
public object[]? ReadDataArray(string query)
|
public object[]? ReadDataArray(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
Connection.Open();
|
Connection.Open();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
var reader = command.ExecuteReader();
|
var reader = command.ExecuteReader();
|
||||||
|
|
||||||
object[] values = new object[reader.FieldCount];
|
var values = new object[reader.FieldCount];
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
{
|
{
|
||||||
reader.GetValues(values);
|
reader.GetValues(values);
|
||||||
@@ -458,14 +453,14 @@ namespace PluginManager.Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read all rows from the result table and return them as a list of string arrays. The string arrays contain the values of each row
|
/// Read all rows from the result table and return them as a list of string arrays. The string arrays contain the
|
||||||
|
/// values of each row
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="query">The query</param>
|
/// <param name="query">The query</param>
|
||||||
/// <returns>A list of string arrays representing the values that the query returns</returns>
|
/// <returns>A list of string arrays representing the values that the query returns</returns>
|
||||||
|
|
||||||
public async Task<List<string[]>?> ReadAllRowsAsync(string query)
|
public async Task<List<string[]>?> ReadAllRowsAsync(string query)
|
||||||
{
|
{
|
||||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||||
await Connection.OpenAsync();
|
await Connection.OpenAsync();
|
||||||
var command = new SQLiteCommand(query, Connection);
|
var command = new SQLiteCommand(query, Connection);
|
||||||
var reader = await command.ExecuteReaderAsync();
|
var reader = await command.ExecuteReaderAsync();
|
||||||
@@ -476,7 +471,7 @@ namespace PluginManager.Database
|
|||||||
List<string[]> rows = new();
|
List<string[]> rows = new();
|
||||||
while (await reader.ReadAsync())
|
while (await reader.ReadAsync())
|
||||||
{
|
{
|
||||||
string[] values = new string[reader.FieldCount];
|
var values = new string[reader.FieldCount];
|
||||||
reader.GetValues(values);
|
reader.GetValues(values);
|
||||||
rows.Add(values);
|
rows.Add(values);
|
||||||
}
|
}
|
||||||
@@ -485,5 +480,4 @@ namespace PluginManager.Database
|
|||||||
|
|
||||||
return rows;
|
return rows;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
namespace PluginManager.Interfaces
|
namespace PluginManager.Interfaces;
|
||||||
|
|
||||||
|
public interface DBSlashCommand
|
||||||
{
|
{
|
||||||
public interface DBSlashCommand
|
|
||||||
{
|
|
||||||
string Name { get; }
|
string Name { get; }
|
||||||
string Description { get; }
|
string Description { get; }
|
||||||
|
|
||||||
@@ -16,10 +15,7 @@ namespace PluginManager.Interfaces
|
|||||||
|
|
||||||
void ExecuteServer(SocketSlashCommand context)
|
void ExecuteServer(SocketSlashCommand context)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExecuteDM(SocketSlashCommand context) { }
|
void ExecuteDM(SocketSlashCommand context) { }
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Interfaces
|
namespace PluginManager.Interfaces;
|
||||||
|
|
||||||
|
public interface ICommandAction
|
||||||
{
|
{
|
||||||
public interface ICommandAction
|
|
||||||
{
|
|
||||||
public string ActionName { get; }
|
public string ActionName { get; }
|
||||||
|
|
||||||
public string? Description { get; }
|
public string? Description { get; }
|
||||||
@@ -18,6 +14,4 @@ namespace PluginManager.Interfaces
|
|||||||
public InternalActionRunType RunType { get; }
|
public InternalActionRunType RunType { get; }
|
||||||
|
|
||||||
public Task Execute(string[]? args);
|
public Task Execute(string[]? args);
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,21 @@
|
|||||||
using System.IO;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using PluginManager.Others.Actions;
|
using System.Threading.Tasks;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using System.Collections;
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Loaders
|
namespace PluginManager.Loaders;
|
||||||
|
|
||||||
|
public class ActionsLoader
|
||||||
{
|
{
|
||||||
public class ActionsLoader
|
|
||||||
{
|
|
||||||
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
|
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
|
||||||
public event ActionLoaded? ActionLoadedEvent;
|
|
||||||
|
|
||||||
private string actionFolder = @"./Data/Actions/";
|
private readonly string actionExtension = "dll";
|
||||||
private string actionExtension = "dll";
|
|
||||||
|
private readonly string actionFolder = @"./Data/Actions/";
|
||||||
|
|
||||||
public ActionsLoader(string path, string extension)
|
public ActionsLoader(string path, string extension)
|
||||||
{
|
{
|
||||||
@@ -24,15 +23,16 @@ namespace PluginManager.Loaders
|
|||||||
actionExtension = extension;
|
actionExtension = extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public event ActionLoaded? ActionLoadedEvent;
|
||||||
|
|
||||||
public async Task<List<ICommandAction>?> Load()
|
public async Task<List<ICommandAction>?> Load()
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(actionFolder);
|
Directory.CreateDirectory(actionFolder);
|
||||||
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
|
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
|
||||||
|
|
||||||
List<ICommandAction> actions = new List<ICommandAction>();
|
var actions = new List<ICommandAction>();
|
||||||
|
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Assembly.LoadFrom(file);
|
Assembly.LoadFrom(file);
|
||||||
@@ -41,24 +41,22 @@ namespace PluginManager.Loaders
|
|||||||
{
|
{
|
||||||
ActionLoadedEvent?.Invoke(file, "", false, e);
|
ActionLoadedEvent?.Invoke(file, "", false, e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||||
.SelectMany(s => s.GetTypes())
|
.SelectMany(s => s.GetTypes())
|
||||||
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
|
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
|
||||||
|
|
||||||
foreach (var type in types)
|
foreach (var type in types)
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var action = (ICommandAction) Activator.CreateInstance(type);
|
var action = (ICommandAction)Activator.CreateInstance(type);
|
||||||
if (action.ActionName == null)
|
if (action.ActionName == null)
|
||||||
{
|
{
|
||||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
|
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(action.RunType == PluginManager.Others.InternalActionRunType.ON_STARTUP)
|
if (action.RunType == InternalActionRunType.ON_STARTUP)
|
||||||
await action.Execute(null);
|
await action.Execute(null);
|
||||||
|
|
||||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
|
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
|
||||||
@@ -68,9 +66,7 @@ namespace PluginManager.Loaders
|
|||||||
{
|
{
|
||||||
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
|
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return actions;
|
return actions;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -3,21 +3,22 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Loaders
|
namespace PluginManager.Loaders;
|
||||||
|
|
||||||
|
internal class LoaderArgs : EventArgs
|
||||||
{
|
{
|
||||||
internal class LoaderArgs : EventArgs
|
|
||||||
{
|
|
||||||
internal string? PluginName { get; init; }
|
internal string? PluginName { get; init; }
|
||||||
internal string? TypeName { get; init; }
|
internal string? TypeName { get; init; }
|
||||||
internal bool IsLoaded { get; init; }
|
internal bool IsLoaded { get; init; }
|
||||||
internal Exception? Exception { get; init; }
|
internal Exception? Exception { get; init; }
|
||||||
internal object? Plugin { get; init; }
|
internal object? Plugin { get; init; }
|
||||||
}
|
}
|
||||||
internal class Loader
|
|
||||||
{
|
internal class Loader
|
||||||
|
{
|
||||||
internal Loader(string path, string extension)
|
internal Loader(string path, string extension)
|
||||||
{
|
{
|
||||||
this.path = path;
|
this.path = path;
|
||||||
@@ -33,14 +34,8 @@ namespace PluginManager.Loaders
|
|||||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||||
|
|
||||||
|
|
||||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
|
||||||
|
|
||||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
|
||||||
|
|
||||||
|
|
||||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||||
{
|
{
|
||||||
|
|
||||||
List<DBEvent> events = new();
|
List<DBEvent> events = new();
|
||||||
List<DBSlashCommand> slashCommands = new();
|
List<DBSlashCommand> slashCommands = new();
|
||||||
List<DBCommand> commands = new();
|
List<DBCommand> commands = new();
|
||||||
@@ -60,9 +55,11 @@ namespace PluginManager.Loaders
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.LogLevel.ERROR);
|
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this,
|
||||||
|
LogLevel.ERROR);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FileLoaded != null)
|
if (FileLoaded != null)
|
||||||
{
|
{
|
||||||
var args = new LoaderArgs
|
var args = new LoaderArgs
|
||||||
@@ -78,7 +75,6 @@ namespace PluginManager.Loaders
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,12 +130,16 @@ namespace PluginManager.Loaders
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||||
|
|
||||||
|
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Online;
|
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Loaders;
|
namespace PluginManager.Loaders;
|
||||||
@@ -62,11 +58,14 @@ public class PluginLoader
|
|||||||
public static List<DBEvent>? Events { get; set; }
|
public static List<DBEvent>? Events { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A list of <see cref="DBSlashCommand"/> commands
|
/// A list of <see cref="DBSlashCommand" /> commands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static List<DBSlashCommand>? SlashCommands { get; set; }
|
public static List<DBSlashCommand>? SlashCommands { get; set; }
|
||||||
|
|
||||||
public static int PluginsLoaded { get {
|
public static int PluginsLoaded
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
var count = 0;
|
var count = 0;
|
||||||
if (Commands is not null)
|
if (Commands is not null)
|
||||||
count += Commands.Count;
|
count += Commands.Count;
|
||||||
@@ -75,7 +74,8 @@ public class PluginLoader
|
|||||||
if (SlashCommands is not null)
|
if (SlashCommands is not null)
|
||||||
count += SlashCommands.Count;
|
count += SlashCommands.Count;
|
||||||
return count;
|
return count;
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main mathod that is called to load all events
|
/// The main mathod that is called to load all events
|
||||||
@@ -88,10 +88,11 @@ public class PluginLoader
|
|||||||
Events = new List<DBEvent>();
|
Events = new List<DBEvent>();
|
||||||
SlashCommands = new List<DBSlashCommand>();
|
SlashCommands = new List<DBSlashCommand>();
|
||||||
|
|
||||||
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.LogLevel.INFO);
|
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this,
|
||||||
|
LogLevel.INFO);
|
||||||
|
|
||||||
var loader = new Loader("./Data/Plugins", "dll");
|
var loader = new Loader("./Data/Plugins", "dll");
|
||||||
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.LogLevel.INFO);
|
loader.FileLoaded += args => Config.Logger.Log($"{args.PluginName} file Loaded", this, LogLevel.INFO);
|
||||||
loader.PluginLoaded += Loader_PluginLoaded;
|
loader.PluginLoaded += Loader_PluginLoaded;
|
||||||
var res = loader.Load();
|
var res = loader.Load();
|
||||||
Events = res.Item1;
|
Events = res.Item1;
|
||||||
@@ -101,7 +102,6 @@ public class PluginLoader
|
|||||||
|
|
||||||
private async void Loader_PluginLoaded(LoaderArgs args)
|
private async void Loader_PluginLoaded(LoaderArgs args)
|
||||||
{
|
{
|
||||||
|
|
||||||
switch (args.TypeName)
|
switch (args.TypeName)
|
||||||
{
|
{
|
||||||
case "DBCommand":
|
case "DBCommand":
|
||||||
@@ -117,27 +117,29 @@ public class PluginLoader
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "DBSlashCommand":
|
case "DBSlashCommand":
|
||||||
if (args.IsLoaded)
|
if (args.IsLoaded)
|
||||||
{
|
{
|
||||||
var slash = (DBSlashCommand)args.Plugin;
|
var slash = (DBSlashCommand)args.Plugin;
|
||||||
SlashCommandBuilder builder = new SlashCommandBuilder();
|
var builder = new SlashCommandBuilder();
|
||||||
builder.WithName(slash.Name);
|
builder.WithName(slash.Name);
|
||||||
builder.WithDescription(slash.Description);
|
builder.WithDescription(slash.Description);
|
||||||
builder.WithDMPermission(slash.canUseDM);
|
builder.WithDMPermission(slash.canUseDM);
|
||||||
builder.Options = slash.Options;
|
builder.Options = slash.Options;
|
||||||
|
|
||||||
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded, args.Exception);
|
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded,
|
||||||
|
args.Exception);
|
||||||
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
|
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task LoadPluginFromAssembly(Assembly asmb, DiscordSocketClient client)
|
public static async Task LoadPluginFromAssembly(Assembly asmb, DiscordSocketClient client)
|
||||||
{
|
{
|
||||||
var types = asmb.GetTypes();
|
var types = asmb.GetTypes();
|
||||||
@@ -146,28 +148,27 @@ public class PluginLoader
|
|||||||
{
|
{
|
||||||
var instance = (DBEvent)Activator.CreateInstance(type);
|
var instance = (DBEvent)Activator.CreateInstance(type);
|
||||||
instance.Start(client);
|
instance.Start(client);
|
||||||
PluginLoader.Events.Add(instance);
|
Events.Add(instance);
|
||||||
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.LogLevel.INFO);
|
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", LogLevel.INFO);
|
||||||
}
|
}
|
||||||
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
|
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
|
||||||
{
|
{
|
||||||
var instance = (DBCommand)Activator.CreateInstance(type);
|
var instance = (DBCommand)Activator.CreateInstance(type);
|
||||||
PluginLoader.Commands.Add(instance);
|
Commands.Add(instance);
|
||||||
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", LogLevel.INFO);
|
||||||
}
|
}
|
||||||
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
|
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
|
||||||
{
|
{
|
||||||
var instance = (DBSlashCommand)Activator.CreateInstance(type);
|
var instance = (DBSlashCommand)Activator.CreateInstance(type);
|
||||||
SlashCommandBuilder builder = new SlashCommandBuilder();
|
var builder = new SlashCommandBuilder();
|
||||||
builder.WithName(instance.Name);
|
builder.WithName(instance.Name);
|
||||||
builder.WithDescription(instance.Description);
|
builder.WithDescription(instance.Description);
|
||||||
builder.WithDMPermission(instance.canUseDM);
|
builder.WithDMPermission(instance.canUseDM);
|
||||||
builder.Options = instance.Options;
|
builder.Options = instance.Options;
|
||||||
|
|
||||||
await client.CreateGlobalApplicationCommandAsync(builder.Build());
|
await client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||||
PluginLoader.SlashCommands.Add(instance);
|
SlashCommands.Add(instance);
|
||||||
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", LogLevel.INFO);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,6 @@ using System.IO;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
namespace PluginManager.Online.Helpers;
|
namespace PluginManager.Online.Helpers;
|
||||||
@@ -19,7 +18,8 @@ internal static class OnlineFunctions
|
|||||||
/// <param name="progress">The <see cref="IProgress{T}" /> that is used to track the download progress</param>
|
/// <param name="progress">The <see cref="IProgress{T}" /> that is used to track the download progress</param>
|
||||||
/// <param name="cancellation">The cancellation token</param>
|
/// <param name="cancellation">The cancellation token</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination,
|
internal static async Task DownloadFileAsync(
|
||||||
|
this HttpClient client, string url, Stream destination,
|
||||||
IProgress<float>? progress = null,
|
IProgress<float>? progress = null,
|
||||||
IProgress<long>? downloadedBytes = null, int bufferSize = 81920,
|
IProgress<long>? downloadedBytes = null, int bufferSize = 81920,
|
||||||
CancellationToken cancellation = default)
|
CancellationToken cancellation = default)
|
||||||
@@ -41,7 +41,8 @@ internal static class OnlineFunctions
|
|||||||
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
|
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
|
||||||
var relativeProgress = new Progress<long>(totalBytes =>
|
var relativeProgress = new Progress<long>(totalBytes =>
|
||||||
{
|
{
|
||||||
progress?.Report((float)totalBytes / contentLength.Value * 100);
|
progress?.Report((float)totalBytes / contentLength.Value *
|
||||||
|
100);
|
||||||
downloadedBytes?.Report(totalBytes);
|
downloadedBytes?.Report(totalBytes);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -4,24 +4,6 @@ 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;
|
||||||
@@ -45,8 +27,9 @@ public class VersionString
|
|||||||
PackageCheckVersion = int.Parse(data[3]);
|
PackageCheckVersion = int.Parse(data[3]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
throw new Exception("Invalid version string");
|
throw new Exception("Invalid version string");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -55,6 +38,25 @@ 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() != GetType()) return false;
|
||||||
|
return Equals((VersionString)obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID);
|
||||||
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return "{PackageID: " + PackageVersionID + ", PackageVersion: " + PackageMainVersion +
|
return "{PackageID: " + PackageVersionID + ", PackageVersion: " + PackageMainVersion +
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PluginManager.Online.Helpers;
|
using PluginManager.Online.Helpers;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
||||||
|
|
||||||
namespace PluginManager.Online;
|
namespace PluginManager.Online;
|
||||||
@@ -26,7 +24,8 @@ public class PluginsManager
|
|||||||
/// The URL of the server
|
/// The URL of the server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string PluginsLink { get; }
|
public string PluginsLink { get; }
|
||||||
public string VersionsLink {get; }
|
|
||||||
|
public string VersionsLink { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The method to load all plugins
|
/// The method to load all plugins
|
||||||
@@ -83,7 +82,8 @@ public class PluginsManager
|
|||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
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 null;
|
return null;
|
||||||
@@ -97,14 +97,15 @@ public class PluginsManager
|
|||||||
if (item.StartsWith("#"))
|
if (item.StartsWith("#"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
string[] split = item.Split(',');
|
var split = item.Split(',');
|
||||||
if (split[0] == pakName)
|
if (split[0] == pakName)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Searched for " + pakName + " and found " + split[1] + " as version.\nUsed url: " + VersionsLink);
|
Console.WriteLine("Searched for " + pakName + " and found " + split[1] + " as version.\nUsed url: " +
|
||||||
|
VersionsLink);
|
||||||
return new VersionString(split[1]);
|
return new VersionString(split[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +136,8 @@ public class PluginsManager
|
|||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
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 null;
|
return null;
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
using PluginManager.Online.Helpers;
|
using PluginManager.Online.Helpers;
|
||||||
using PluginManager.Others;
|
|
||||||
|
|
||||||
namespace PluginManager.Online;
|
namespace PluginManager.Online;
|
||||||
|
|
||||||
@@ -32,7 +29,8 @@ public static class ServerCom
|
|||||||
/// <param name="location">The location where to store the downloaded data</param>
|
/// <param name="location">The location where to store the downloaded data</param>
|
||||||
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
|
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> progress,
|
public static async Task DownloadFileAsync(
|
||||||
|
string URL, string location, IProgress<float> progress,
|
||||||
IProgress<long>? downloadedBytes)
|
IProgress<long>? downloadedBytes)
|
||||||
{
|
{
|
||||||
using (var client = new HttpClient())
|
using (var client = new HttpClient())
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Loaders;
|
using PluginManager.Loaders;
|
||||||
|
|
||||||
namespace PluginManager.Others.Actions
|
namespace PluginManager.Others.Actions;
|
||||||
|
|
||||||
|
public class InternalActionManager
|
||||||
{
|
{
|
||||||
public class InternalActionManager
|
public Dictionary<string, ICommandAction> Actions = new();
|
||||||
{
|
|
||||||
public ActionsLoader loader;
|
public ActionsLoader loader;
|
||||||
public Dictionary<string, ICommandAction> Actions = new Dictionary<string, ICommandAction>();
|
|
||||||
|
|
||||||
public InternalActionManager(string path, string extension)
|
public InternalActionManager(string path, string extension)
|
||||||
{
|
{
|
||||||
@@ -21,8 +20,8 @@ namespace PluginManager.Others.Actions
|
|||||||
{
|
{
|
||||||
loader.ActionLoadedEvent += OnActionLoaded;
|
loader.ActionLoadedEvent += OnActionLoaded;
|
||||||
var m_actions = await loader.Load();
|
var m_actions = await loader.Load();
|
||||||
if(m_actions == null) return;
|
if (m_actions == null) return;
|
||||||
foreach(var action in m_actions)
|
foreach (var action in m_actions)
|
||||||
Actions.Add(action.ActionName, action);
|
Actions.Add(action.ActionName, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +33,7 @@ namespace PluginManager.Others.Actions
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.Logger.Log($"Action {name} loaded successfully", typeName, LogLevel.INFO);
|
Config.Logger.Log($"Action {name} loaded successfully", typeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> Execute(string actionName, params string[]? args)
|
public async Task<string> Execute(string actionName, params string[]? args)
|
||||||
@@ -45,13 +44,15 @@ namespace PluginManager.Others.Actions
|
|||||||
return "Action not found";
|
return "Action not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
await Actions[actionName].Execute(args);
|
await Actions[actionName].Execute(args);
|
||||||
return "Action executed";
|
return "Action executed";
|
||||||
}catch(Exception e){
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
|
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
|
||||||
return e.Message;
|
return e.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ using System.IO.Compression;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PluginManager.Others
|
namespace PluginManager.Others;
|
||||||
|
|
||||||
|
public static class ArchiveManager
|
||||||
{
|
{
|
||||||
public static class ArchiveManager
|
|
||||||
{
|
|
||||||
public static bool isInitialized { get; private set; }
|
|
||||||
private static string? archiveFolder;
|
private static string? archiveFolder;
|
||||||
|
public static bool isInitialized { get; private set; }
|
||||||
|
|
||||||
public static void Initialize()
|
public static void Initialize()
|
||||||
{
|
{
|
||||||
@@ -21,8 +21,8 @@ namespace PluginManager.Others
|
|||||||
archiveFolder = Config.Data["ArchiveFolder"];
|
archiveFolder = Config.Data["ArchiveFolder"];
|
||||||
|
|
||||||
isInitialized = true;
|
isInitialized = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read data from a file that is inside an archive (ZIP format)
|
/// Read data from a file that is inside an archive (ZIP format)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -72,7 +72,8 @@ namespace PluginManager.Others
|
|||||||
/// <param name="progress">The progress that is updated as a file is processed</param>
|
/// <param name="progress">The progress that is updated as a file is processed</param>
|
||||||
/// <param name="type">The type of progress</param>
|
/// <param name="type">The type of progress</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task ExtractArchive(string zip, string folder, IProgress<float> progress,
|
public static async Task ExtractArchive(
|
||||||
|
string zip, string folder, IProgress<float> progress,
|
||||||
UnzipProgressType type)
|
UnzipProgressType type)
|
||||||
{
|
{
|
||||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||||
@@ -95,7 +96,8 @@ namespace PluginManager.Others
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}",
|
||||||
|
"Archive Manager", LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
currentZIPFile++;
|
currentZIPFile++;
|
||||||
@@ -127,7 +129,8 @@ namespace PluginManager.Others
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}",
|
||||||
|
"Archive Manager", LogLevel.ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(10);
|
await Task.Delay(10);
|
||||||
@@ -137,5 +140,4 @@ namespace PluginManager.Others
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,11 @@
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PluginManager.Others
|
namespace PluginManager.Others;
|
||||||
|
|
||||||
|
public class DBCommandExecutingArguments
|
||||||
{
|
{
|
||||||
public class DBCommandExecutingArguments
|
public DBCommandExecutingArguments(
|
||||||
{
|
SocketCommandContext context, string cleanContent, string commandUsed, string[]? arguments)
|
||||||
public SocketCommandContext context { get; init; }
|
|
||||||
public string cleanContent { get; init; }
|
|
||||||
public string commandUsed { get;init; }
|
|
||||||
public string[]? arguments { get;init; }
|
|
||||||
|
|
||||||
public DBCommandExecutingArguments(SocketCommandContext context, string cleanContent, string commandUsed, string[]? arguments)
|
|
||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.cleanContent = cleanContent;
|
this.cleanContent = cleanContent;
|
||||||
@@ -23,5 +13,8 @@ namespace PluginManager.Others
|
|||||||
this.arguments = arguments;
|
this.arguments = arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public SocketCommandContext context { get; init; }
|
||||||
|
public string cleanContent { get; init; }
|
||||||
|
public string commandUsed { get; init; }
|
||||||
|
public string[]? arguments { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,15 @@ public static class Functions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly string dataFolder = @"./Data/Resources/";
|
public static readonly string dataFolder = @"./Data/Resources/";
|
||||||
|
|
||||||
|
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>
|
/// <summary>
|
||||||
/// Get the Operating system you are runnin on
|
/// Get the Operating system you are runnin on
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -43,7 +52,8 @@ public static class Functions
|
|||||||
/// <exception cref="ArgumentOutOfRangeException">Triggered if <paramref name="bufferSize" /> is less then or equal to 0</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="InvalidOperationException">Triggered if <paramref name="stream" /> is not readable</exception>
|
||||||
/// <exception cref="ArgumentException">Triggered in <paramref name="destination" /> is not writable</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,
|
public static async Task CopyToOtherStreamAsync(
|
||||||
|
this Stream stream, Stream destination, int bufferSize,
|
||||||
IProgress<long>? progress = null,
|
IProgress<long>? progress = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
@@ -57,7 +67,8 @@ public static class Functions
|
|||||||
var buffer = new byte[bufferSize];
|
var buffer = new byte[bufferSize];
|
||||||
long totalBytesRead = 0;
|
long totalBytesRead = 0;
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)
|
||||||
|
.ConfigureAwait(false)) != 0)
|
||||||
{
|
{
|
||||||
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
|
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
|
||||||
totalBytesRead += bytesRead;
|
totalBytesRead += bytesRead;
|
||||||
@@ -102,7 +113,7 @@ public static class Functions
|
|||||||
return (obj ?? default)!;
|
return (obj ?? default)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T SelectRandomValueOf<T> ()
|
public static T SelectRandomValueOf<T>()
|
||||||
{
|
{
|
||||||
var enums = Enum.GetValues(typeof(T));
|
var enums = Enum.GetValues(typeof(T));
|
||||||
var random = new Random();
|
var random = new Random();
|
||||||
@@ -114,13 +125,4 @@ public static class Functions
|
|||||||
Random random = new();
|
Random random = new();
|
||||||
return values[random.Next(values.Length)];
|
return values[random.Next(values.Length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Discord.Color RandomColor
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
Random random = new Random();
|
|
||||||
return new Discord.Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PluginManager.Others.Logger
|
namespace PluginManager.Others.Logger;
|
||||||
|
|
||||||
|
public class DBLogger
|
||||||
{
|
{
|
||||||
public class DBLogger
|
|
||||||
{
|
|
||||||
|
|
||||||
private List<LogMessage> LogHistory = new List<LogMessage>();
|
|
||||||
private List<LogMessage> ErrorHistory = new List<LogMessage>();
|
|
||||||
|
|
||||||
public IReadOnlyList<LogMessage> Logs => LogHistory;
|
|
||||||
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
|
||||||
|
|
||||||
public delegate void LogHandler(string message, LogLevel logType);
|
public delegate void LogHandler(string message, LogLevel logType);
|
||||||
public event LogHandler LogEvent;
|
|
||||||
|
|
||||||
private string _logFolder;
|
private readonly string _errFolder;
|
||||||
private string _errFolder;
|
|
||||||
|
private readonly string _logFolder;
|
||||||
|
private readonly List<LogMessage> ErrorHistory = new();
|
||||||
|
private readonly List<LogMessage> LogHistory = new();
|
||||||
|
|
||||||
public DBLogger()
|
public DBLogger()
|
||||||
{
|
{
|
||||||
@@ -28,12 +19,24 @@ namespace PluginManager.Others.Logger
|
|||||||
_errFolder = Config.Data["ErrorFolder"];
|
_errFolder = Config.Data["ErrorFolder"];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
|
public IReadOnlyList<LogMessage> Logs => LogHistory;
|
||||||
public void Error(Exception? e) => Log(e.Message, e.Source, LogLevel.ERROR);
|
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
||||||
|
|
||||||
|
public event LogHandler LogEvent;
|
||||||
|
|
||||||
|
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.ERROR && message.Type != LogLevel.CRITICAL)
|
if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL)
|
||||||
@@ -42,12 +45,16 @@ namespace PluginManager.Others.Logger
|
|||||||
ErrorHistory.Add(message);
|
ErrorHistory.Add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Log(string message, object sender, LogLevel type = LogLevel.NONE) => Log(message, sender.GetType().Name, type);
|
public void Log(string message, object sender, LogLevel type = LogLevel.NONE)
|
||||||
|
{
|
||||||
|
Log(message, sender.GetType().Name, type);
|
||||||
|
}
|
||||||
|
|
||||||
public async void SaveToFile()
|
public async void SaveToFile()
|
||||||
{
|
{
|
||||||
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", LogHistory);
|
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", ErrorHistory);
|
LogHistory);
|
||||||
}
|
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||||
|
ErrorHistory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PluginManager.Others.Logger
|
namespace PluginManager.Others.Logger;
|
||||||
|
|
||||||
|
public class LogMessage
|
||||||
{
|
{
|
||||||
public class LogMessage
|
|
||||||
{
|
|
||||||
public string Message { get; set; }
|
|
||||||
public LogLevel Type { get; set; }
|
|
||||||
public string Time { get; set; }
|
|
||||||
public string Sender { get; set; }
|
|
||||||
public LogMessage(string message, LogLevel type)
|
public LogMessage(string message, LogLevel type)
|
||||||
{
|
{
|
||||||
Message = message;
|
Message = message;
|
||||||
@@ -24,6 +16,11 @@ namespace PluginManager.Others.Logger
|
|||||||
Sender = sender;
|
Sender = sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string Message { get; set; }
|
||||||
|
public LogLevel Type { get; set; }
|
||||||
|
public string Time { get; set; }
|
||||||
|
public string Sender { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"[{Time}] {Message}";
|
return $"[{Time}] {Message}";
|
||||||
@@ -43,5 +40,4 @@ namespace PluginManager.Others.Logger
|
|||||||
{
|
{
|
||||||
return new LogMessage(tuple.message, tuple.type, tuple.sender);
|
return new LogMessage(tuple.message, tuple.type, tuple.sender);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,10 @@
|
|||||||
<DebugSymbols>false</DebugSymbols>
|
<DebugSymbols>false</DebugSymbols>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="BlankWindow1.xaml" />
|
<None Remove="BlankWindow1.xaml"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Discord.Net" Version="3.10.0" />
|
<PackageReference Include="Discord.Net" Version="3.10.0"/>
|
||||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
|
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
Reference in New Issue
Block a user