Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e4c60f1606 | |||
| 77f1bef862 | |||
| f16c139362 | |||
| c94cdca6eb | |||
| dcdf80112d | |||
| eb836c5b74 | |||
| de680c6771 | |||
|
|
bcef58a46b | ||
|
|
0dc8cdbce5 | ||
|
|
dbdbaa9802 | ||
|
|
5edcf93371 | ||
|
|
b0be76c62b | ||
| 75a77389a8 | |||
| 0bbced3d58 | |||
| 244209093e | |||
| 54a68d635d | |||
| d7a5cb5a64 | |||
| 6124f89cb0 | |||
| 810a527cc1 | |||
| 0a2dff0c6d | |||
| 382c376c03 | |||
| 84b7d663bc | |||
| 623232b67e | |||
| d5df6cfb9d | |||
| 10b9548c29 | |||
| fa1a136ef1 | |||
| d20cb62139 | |||
| f2418d0395 | |||
| 460a85944a | |||
| 7e2fa02d07 | |||
| 873855937f | |||
| 1cdd2644df | |||
| 532540b74f | |||
| 9ba4ca43e2 | |||
| 8bcaf3f254 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -371,3 +371,6 @@ FodyWeavers.xsd
|
||||
/DiscordBot.rar
|
||||
/DiscordBot/Data/
|
||||
/DiscordBot/Updater/
|
||||
.idea/
|
||||
/DiscordBotWeb/
|
||||
DiscordBot/Launcher.exe
|
||||
|
||||
26
.vscode/launch.json
vendored
Normal file
26
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||
// Use hover for the description of the existing attributes
|
||||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
|
||||
"name": ".NET Core Launch (console)",
|
||||
"type": "coreclr",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "build",
|
||||
// If you have changed target frameworks, make sure to update the program path.
|
||||
"program": "${workspaceFolder}/DiscordBot/bin/Debug/net6.0/DiscordBot.dll",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}/DiscordBot/bin/Debug/net6.0/",
|
||||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||
"console": "externalTerminal",
|
||||
"stopAtEntry": false
|
||||
},
|
||||
{
|
||||
"name": ".NET Core Attach",
|
||||
"type": "coreclr",
|
||||
"request": "attach"
|
||||
}
|
||||
]
|
||||
}
|
||||
41
.vscode/tasks.json
vendored
Normal file
41
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "build",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"build",
|
||||
"${workspaceFolder}/DiscordBot/DiscordBot.csproj",
|
||||
"/property:GenerateFullPaths=true",
|
||||
"/consoleloggerparameters:NoSummary"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
},
|
||||
{
|
||||
"label": "publish",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"publish",
|
||||
"${workspaceFolder}/DiscordBot/DiscordBot.csproj",
|
||||
"/property:GenerateFullPaths=true",
|
||||
"/consoleloggerparameters:NoSummary"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
},
|
||||
{
|
||||
"label": "watch",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"watch",
|
||||
"run",
|
||||
"--project",
|
||||
"${workspaceFolder}/DiscordBot/DiscordBot.csproj"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
|
||||
using PluginManager;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Loaders;
|
||||
@@ -41,7 +40,7 @@ internal class Help : DBCommand
|
||||
/// The main body of the command
|
||||
/// </summary>
|
||||
/// <param name="context">The command context</param>
|
||||
public void ExecuteServer(CmdArgs args)
|
||||
public void ExecuteServer(DBCommandExecutingArguments args)
|
||||
{
|
||||
if (args.arguments is not null)
|
||||
{
|
||||
@@ -60,16 +59,16 @@ internal class Help : DBCommand
|
||||
var adminCommands = "";
|
||||
var normalCommands = "";
|
||||
|
||||
foreach (var cmd in PluginLoader.Commands!)
|
||||
foreach (var cmd in PluginLoader.Commands)
|
||||
if (cmd.requireAdmin)
|
||||
adminCommands += cmd.Command + " ";
|
||||
else
|
||||
normalCommands += cmd.Command + " ";
|
||||
|
||||
|
||||
if(adminCommands.Length > 0)
|
||||
if (adminCommands.Length > 0)
|
||||
embedBuilder.AddField("Admin Commands", adminCommands);
|
||||
if(normalCommands.Length > 0)
|
||||
if (normalCommands.Length > 0)
|
||||
embedBuilder.AddField("Normal Commands", normalCommands);
|
||||
args.context.Channel.SendMessageAsync(embed: embedBuilder.Build());
|
||||
}
|
||||
@@ -77,11 +76,11 @@ internal class Help : DBCommand
|
||||
private EmbedBuilder GenerateHelpCommand(string command)
|
||||
{
|
||||
var embedBuilder = new EmbedBuilder();
|
||||
var cmd = PluginLoader.Commands!.Find(p => p.Command == command ||
|
||||
var cmd = PluginLoader.Commands.Find(p => p.Command == command ||
|
||||
(p.Aliases is not null && p.Aliases.Contains(command)));
|
||||
if (cmd == null) return null;
|
||||
|
||||
embedBuilder.AddField("Usage", Config.Variables.GetValue("prefix") + cmd.Usage);
|
||||
embedBuilder.AddField("Usage", Config.Data["prefix"] + cmd.Usage);
|
||||
embedBuilder.AddField("Description", cmd.Description);
|
||||
if (cmd.Aliases is null)
|
||||
return embedBuilder;
|
||||
|
||||
@@ -1,48 +1,39 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<SignAssembly>False</SignAssembly>
|
||||
<IsPublishable>True</IsPublishable>
|
||||
<AssemblyVersion>1.0.1.2</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Data\**" />
|
||||
<Compile Remove="obj\**" />
|
||||
<Compile Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="Data\**" />
|
||||
<EmbeddedResource Remove="obj\**" />
|
||||
<EmbeddedResource Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Data\**" />
|
||||
<None Remove="obj\**" />
|
||||
<None Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.9.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>disable</Nullable>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<SignAssembly>False</SignAssembly>
|
||||
<IsPublishable>True</IsPublishable>
|
||||
<AssemblyVersion>1.0.2.1</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="Data\**" />
|
||||
<Compile Remove="obj\**" />
|
||||
<Compile Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="Data\**" />
|
||||
<EmbeddedResource Remove="obj\**" />
|
||||
<EmbeddedResource Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Data\**" />
|
||||
<None Remove="obj\**" />
|
||||
<None Remove="Output\**" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.9.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -10,7 +10,6 @@ namespace DiscordBot
|
||||
|
||||
public class Entry
|
||||
{
|
||||
internal static StartupArguments startupArguments;
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
AppDomain currentDomain = AppDomain.CurrentDomain;
|
||||
|
||||
117
DiscordBot/Installer.cs
Normal file
117
DiscordBot/Installer.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager;
|
||||
using PluginManager.Others;
|
||||
using PluginManager.Online;
|
||||
|
||||
namespace DiscordBot
|
||||
{
|
||||
public static class Installer
|
||||
{
|
||||
|
||||
public static void GenerateStartupConfig()
|
||||
{
|
||||
Console.WriteLine("Welcome to the SethBot installer !");
|
||||
Console.WriteLine("First, we need to configure the bot. Don't worry, it will be quick !");
|
||||
Console.WriteLine("The following information will be stored in the config.json file in the ./Data/Resources folder. You can change it later from there.");
|
||||
Console.WriteLine("The bot tokn is required to run the bot. You can get it from the Discord Developer Portal. (https://discord.com/developers/applications)");
|
||||
Console.WriteLine("Please enter the bot token :");
|
||||
var token = Console.ReadLine();
|
||||
|
||||
Console.WriteLine("Please enter the bot prefix :");
|
||||
var prefix = Console.ReadLine();
|
||||
|
||||
Console.WriteLine("Please enter the Server ID :");
|
||||
var serverId = Console.ReadLine();
|
||||
|
||||
Config.Data.Add("token", token);
|
||||
Config.Data.Add("prefix", prefix);
|
||||
Config.Data.Add("ServerID", serverId);
|
||||
|
||||
Config.Logger.Log("Config Saved", "Installer", LogLevel.INFO);
|
||||
|
||||
Config.Data.Save();
|
||||
|
||||
Console.WriteLine("Config saved !");
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static async Task SetupPluginDatabase()
|
||||
{
|
||||
Console.WriteLine("The plugin database is required to run the bot but there is nothing configured yet.");
|
||||
Console.WriteLine("Please select one option : ");
|
||||
Console.WriteLine("1. Download the official database file");
|
||||
Console.WriteLine("2. Create a new (CUSTOM) database file");
|
||||
int choice = 0;
|
||||
Console.Write("Choice : ");
|
||||
choice = int.Parse(Console.ReadLine());
|
||||
if (choice != 1 && choice != 2)
|
||||
{
|
||||
Console.WriteLine("Invalid choice !");
|
||||
Console.WriteLine("Please restart the installer !");
|
||||
Console.ReadKey();
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
if (choice == 1)
|
||||
await DownloadPluginDatabase();
|
||||
|
||||
if (choice == 2)
|
||||
{
|
||||
Console.WriteLine("Do you have a url to a valid database file ? (y/n)");
|
||||
var answer = Console.ReadLine();
|
||||
if (answer == "y")
|
||||
{
|
||||
Console.WriteLine("Please enter the url :");
|
||||
var url = Console.ReadLine();
|
||||
await DownloadPluginDatabase(url);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Do you want to create a new database file ? (y/n)");
|
||||
answer = Console.ReadLine();
|
||||
if (answer == "y")
|
||||
{
|
||||
Console.WriteLine("A new file will be generated at ./Data/Resources/URLs.json");
|
||||
System.Console.WriteLine("Please edit the file and restart the bot !");
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
await File.WriteAllTextAsync("./Data/Resources/URLs.json",
|
||||
@"
|
||||
{
|
||||
""PluginList"": """",
|
||||
""PluginVersions"": """",
|
||||
""StartupMessage"": """",
|
||||
""SetupKeys"": """",
|
||||
""Versions"": """",
|
||||
""Changelog"": """",
|
||||
""LinuxBot"": """",
|
||||
""WindowsLauncher"": """",
|
||||
}
|
||||
".Replace(" ", ""));
|
||||
Environment.Exit(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task DownloadPluginDatabase(string url = "https://raw.githubusercontent.com/Wizzy69/SethDiscordBot/gh-pages/defaultURLs.json")
|
||||
{
|
||||
string path = "./Data/Resources/URLs.json";
|
||||
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
Utilities.Utilities.ProgressBar bar = new Utilities.Utilities.ProgressBar(Utilities.ProgressBarType.NORMAL){
|
||||
Max = 100,
|
||||
Color = ConsoleColor.Green,
|
||||
NoColor = true
|
||||
};
|
||||
IProgress<float> downloadProgress = new Progress<float>(p => bar.Update(p));
|
||||
await ServerCom.DownloadFileAsync(url, path, downloadProgress, null);
|
||||
bar.Update(bar.Max);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,27 +5,26 @@ using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using DiscordBot.Discord.Core;
|
||||
using System.Linq;
|
||||
|
||||
using PluginManager;
|
||||
using PluginManager.Database;
|
||||
using PluginManager.Items;
|
||||
using PluginManager.Bot;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Online.Helpers;
|
||||
using PluginManager.Others;
|
||||
|
||||
using Terminal.Gui;
|
||||
|
||||
using DiscordBot.Utilities;
|
||||
using Microsoft.VisualBasic.CompilerServices;
|
||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
||||
using static PluginManager.Config;
|
||||
|
||||
namespace DiscordBot;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static bool loadPluginsOnStartup;
|
||||
public static Json<string, string> URLs;
|
||||
private static bool loadPluginsOnStartup = false;
|
||||
private static ConsoleCommandsHandler consoleCommandsHandler;
|
||||
//private static bool isUI_ON;
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
@@ -35,17 +34,17 @@ public class Program
|
||||
{
|
||||
PreLoadComponents(args).Wait();
|
||||
|
||||
if (!Config.Variables.Exists("ServerID") || !Config.Variables.Exists("token") ||
|
||||
Config.Variables.GetValue("token") == null ||
|
||||
(Config.Variables.GetValue("token")?.Length != 70 && Config.Variables.GetValue("token")?.Length != 59) ||
|
||||
!Config.Variables.Exists("prefix") || Config.Variables.GetValue("prefix") == null ||
|
||||
Config.Variables.GetValue("prefix")?.Length != 1 ||
|
||||
if (!Config.Data.ContainsKey("ServerID") || !Config.Data.ContainsKey("token") ||
|
||||
Config.Data["token"] == null ||
|
||||
(Config.Data["token"]?.Length != 70 && Config.Data["token"]?.Length != 59) ||
|
||||
!Config.Data.ContainsKey("prefix") || Config.Data["prefix"] == null ||
|
||||
Config.Data["prefix"]?.Length != 1 ||
|
||||
(args.Length == 1 && args[0] == "/reset"))
|
||||
{
|
||||
GenerateStartUI("First time setup. Please fill the following with your discord bot data.\nThis are saved ONLY on YOUR computer.");
|
||||
Installer.GenerateStartupConfig();
|
||||
}
|
||||
|
||||
HandleInput(args).Wait();
|
||||
HandleInput(args.ToList()).Wait();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -54,9 +53,7 @@ public class Program
|
||||
private static void NoGUI()
|
||||
{
|
||||
#if DEBUG
|
||||
Logger.WriteLine();
|
||||
Logger.WriteLine("Debug mode enabled");
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine("Debug mode enabled");
|
||||
|
||||
#endif
|
||||
if (loadPluginsOnStartup)
|
||||
@@ -71,7 +68,7 @@ public class Program
|
||||
#endif
|
||||
|
||||
) && cmd.Length > 0)
|
||||
Logger.WriteLine("Failed to run command " + cmd);
|
||||
Console.WriteLine("Failed to run command " + cmd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,45 +82,46 @@ public class Program
|
||||
Console.ForegroundColor = ConsoleColor.DarkYellow;
|
||||
|
||||
var startupMessageList =
|
||||
await ServerCom.ReadTextFromURL(
|
||||
"https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/StartupMessage");
|
||||
await ServerCom.ReadTextFromURL(URLs["StartupMessage"]);
|
||||
|
||||
foreach (var message in startupMessageList)
|
||||
Logger.WriteLine(message);
|
||||
Console.WriteLine(message);
|
||||
|
||||
Logger.WriteLine(
|
||||
Console.WriteLine(
|
||||
$"Running on version: {Assembly.GetExecutingAssembly().GetName().Version}");
|
||||
Logger.WriteLine($"Git URL: {Settings.Variables.WebsiteURL}");
|
||||
Console.WriteLine($"Git URL: {Config.Data["GitURL"]}");
|
||||
|
||||
Utilities.WriteColorText(
|
||||
Utilities.Utilities.WriteColorText(
|
||||
"&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
if (Config.Variables.Exists("LaunchMessage"))
|
||||
Utilities.WriteColorText(Config.Variables.GetValue("LaunchMessage"));
|
||||
if (Config.Data.ContainsKey("LaunchMessage"))
|
||||
Utilities.Utilities.WriteColorText(Config.Data["LaunchMessage"]);
|
||||
|
||||
|
||||
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)");
|
||||
Logger.WriteLine("============================ LOG ============================");
|
||||
|
||||
Console.WriteLine("Running on " + Functions.GetOperatingSystem().ToString());
|
||||
Console.WriteLine("============================ LOG ============================");
|
||||
|
||||
try
|
||||
{
|
||||
string token = "";
|
||||
#if DEBUG
|
||||
if (File.Exists("./Data/Resources/token.txt")) token = File.ReadAllText("./Data/Resources/token.txt");
|
||||
else token = Config.Variables.GetValue("token");
|
||||
else token = Config.Data["token"];
|
||||
#else
|
||||
token = Config.Variables.GetValue("token");
|
||||
token = Config.Data["token"];
|
||||
#endif
|
||||
var prefix = Config.Variables.GetValue("prefix");
|
||||
var prefix = Config.Data["prefix"];
|
||||
var discordbooter = new Boot(token, prefix);
|
||||
await discordbooter.Awake();
|
||||
return discordbooter;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex);
|
||||
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -132,91 +130,65 @@ public class Program
|
||||
/// Handle user input arguments from the startup of the application
|
||||
/// </summary>
|
||||
/// <param name="args">The arguments</param>
|
||||
private static async Task HandleInput(string[] args)
|
||||
private static async Task HandleInput(List<string> args)
|
||||
{
|
||||
var len = args.Length;
|
||||
|
||||
Console.WriteLine("Loading Core ...");
|
||||
|
||||
var b = await StartNoGui();
|
||||
consoleCommandsHandler = new ConsoleCommandsHandler(b.client);
|
||||
|
||||
if (Entry.startupArguments.loadPluginsAtStartup) { loadPluginsOnStartup = true; }
|
||||
|
||||
if (len > 0 && args[0] == "/remplug")
|
||||
try
|
||||
{
|
||||
var plugName = string.Join(' ', args, 1, args.Length - 1);
|
||||
Logger.WriteLine("Starting to remove " + plugName);
|
||||
await ConsoleCommandsHandler.ExecuteCommad("remplug " + plugName);
|
||||
loadPluginsOnStartup = true;
|
||||
if(args.Contains("--gui"))
|
||||
{
|
||||
// GUI not implemented yet
|
||||
Console.WriteLine("GUI not implemented yet");
|
||||
return;
|
||||
}
|
||||
NoGUI();
|
||||
}
|
||||
|
||||
|
||||
|
||||
var mainThread = new Thread(() =>
|
||||
catch (IOException ex)
|
||||
{
|
||||
try
|
||||
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
|
||||
{
|
||||
NoGUI();
|
||||
if (Config.Data.ContainsKey("LaunchMessage"))
|
||||
Config.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 !");
|
||||
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);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
|
||||
{
|
||||
if (Config.Variables.Exists("LaunchMessage"))
|
||||
Config.Variables.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 !",
|
||||
false);
|
||||
Logger.WriteErrFile(ex.ToString());
|
||||
}
|
||||
}
|
||||
});
|
||||
mainThread.Start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private static async Task PreLoadComponents(string[] args)
|
||||
{
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
Directory.CreateDirectory("./Data/Plugins");
|
||||
Directory.CreateDirectory("./Data/PAKS");
|
||||
|
||||
if (!File.Exists(Functions.dataFolder + "loader.json"))
|
||||
{
|
||||
Entry.startupArguments = new StartupArguments();
|
||||
await Functions.SaveToJsonFile(Functions.dataFolder + "loader.json", Entry.startupArguments);
|
||||
}
|
||||
else
|
||||
Entry.startupArguments = await Functions.ConvertFromJson<StartupArguments>(Functions.dataFolder + "loader.json");
|
||||
|
||||
|
||||
Settings.sqlDatabase = new SqlDatabase("SetDB.dat");
|
||||
|
||||
await Settings.sqlDatabase.Open();
|
||||
await Config.Initialize();
|
||||
Logger.Initialize(true);
|
||||
ArchiveManager.Initialize();
|
||||
|
||||
if (!Directory.Exists("./Data/Resources") || !File.Exists("./Data/Resources/URLs.json"))
|
||||
{
|
||||
await Installer.SetupPluginDatabase();
|
||||
}
|
||||
|
||||
|
||||
Logger.LogEvent += (message) => { Console.Write(message); };
|
||||
URLs = new Json<string, string>("./Data/Resources/URLs.json");
|
||||
|
||||
Logger.WriteLine("Loading resources ...");
|
||||
var main = new Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
Config.Logger.LogEvent += (message, type) => { Console.WriteLine(message); };
|
||||
|
||||
|
||||
Console.WriteLine("Loading resources ...");
|
||||
var main = new Utilities.Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
main.Start();
|
||||
|
||||
if (await Config.Variables.ExistsAsync("DeleteLogsAtStartup"))
|
||||
if (await Config.Variables.GetValueAsync("DeleteLogsAtStartup") == "true")
|
||||
if (Config.Data.ContainsKey("DeleteLogsAtStartup"))
|
||||
if (Config.Data["DeleteLogsAtStartup"] == "true")
|
||||
foreach (var file in Directory.GetFiles("./Output/Logs/"))
|
||||
File.Delete(file);
|
||||
var OnlineDefaultKeys =
|
||||
await ServerCom.ReadTextFromURL(
|
||||
"https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/SetupKeys");
|
||||
await ServerCom.ReadTextFromURL(URLs["SetupKeys"]);
|
||||
|
||||
|
||||
if (!await Config.Variables.ExistsAsync("Version"))
|
||||
await Config.Variables.AddAsync("Version", Assembly.GetExecutingAssembly().GetName().Version.ToString(),
|
||||
false);
|
||||
else
|
||||
await Config.Variables.SetValueAsync(
|
||||
"Version", Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
||||
|
||||
Config.Data["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
||||
|
||||
foreach (var key in OnlineDefaultKeys)
|
||||
{
|
||||
@@ -224,20 +196,16 @@ public class Program
|
||||
var s = key.Split(' ');
|
||||
try
|
||||
{
|
||||
if (await Config.Variables.ExistsAsync(s[0])) await Config.Variables.SetValueAsync(s[0], s[1]);
|
||||
else
|
||||
await Config.Variables.AddAsync(s[0], s[1], s[2].ToLower() == "true");
|
||||
Config.Data[s[0]] = s[1];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteErrFile(ex.Message);
|
||||
Config.Logger.Log(ex.ToString(), "Bot", LogLevel.ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var onlineSettingsList =
|
||||
await ServerCom.ReadTextFromURL(
|
||||
"https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/OnlineData");
|
||||
var onlineSettingsList = await ServerCom.ReadTextFromURL(URLs["Versions"]);
|
||||
main.Stop("Loaded online settings. Loading updates ...");
|
||||
foreach (var key in onlineSettingsList)
|
||||
{
|
||||
@@ -248,104 +216,106 @@ public class Program
|
||||
{
|
||||
case "CurrentVersion":
|
||||
var newVersion = s[1];
|
||||
if (!newVersion.Equals(await Config.Variables.GetValueAsync("Version")))
|
||||
var currentVersion = Config.Data["Version"];
|
||||
if (!newVersion.Equals(currentVersion))
|
||||
{
|
||||
var nVer = new VersionString(newVersion.Substring(2));
|
||||
var cVer = new VersionString((await Config.Variables.GetValueAsync("Version")).Substring(2));
|
||||
var cVer = new VersionString((Config.Data["Version"]).Substring(2));
|
||||
if (cVer > nVer)
|
||||
{
|
||||
await Config.Variables.SetValueAsync("Version", "1." + cVer.ToShortString() + " (Beta)");
|
||||
Config.Data["Version"] = "1." + cVer.ToShortString() + " (Beta)";
|
||||
break;
|
||||
}
|
||||
|
||||
if (OperatingSystem.WINDOWS == Functions.GetOperatingSystem())
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine("A new update was found !");
|
||||
Console.WriteLine("Run the launcher to update");
|
||||
Console.WriteLine("Current version: " + currentVersion);
|
||||
Console.WriteLine("Latest version: " + s[1]);
|
||||
|
||||
File.WriteAllText("version.txt", currentVersion);
|
||||
|
||||
await Task.Delay(3000);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Console.Clear();
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Logger.WriteLine("A new version of the bot is available !");
|
||||
Console.WriteLine("A new version of the bot is available !");
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Logger.WriteLine("Current version : " +
|
||||
Console.WriteLine("Current version : " +
|
||||
Assembly.GetExecutingAssembly().GetName().Version.ToString());
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Logger.WriteLine("New version : " + newVersion);
|
||||
Console.WriteLine("New version : " + newVersion);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
|
||||
Logger.WriteLine("Changelog :");
|
||||
File.WriteAllText("version.txt", newVersion);
|
||||
|
||||
List<string> changeLog = await ServerCom.ReadTextFromURL(
|
||||
"https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/VersionData/DiscordBot");
|
||||
Console.WriteLine("Changelog :");
|
||||
|
||||
List<string> changeLog = await ServerCom.ReadTextFromURL(URLs["Changelog"]);
|
||||
foreach (var item in changeLog)
|
||||
Utilities.WriteColorText(item);
|
||||
Logger.WriteLine("Do you want to update the bot ? (y/n)");
|
||||
Utilities.Utilities.WriteColorText(item);
|
||||
Console.WriteLine("Do you want to update the bot ? (y/n)");
|
||||
if (Console.ReadKey().Key == ConsoleKey.Y)
|
||||
{
|
||||
if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS)
|
||||
var url = URLs["LinuxBot"].Replace("{0}", newVersion);
|
||||
Config.Logger.Log($"executing: download_file {url}");
|
||||
|
||||
await ServerCom.DownloadFileAsync(url, "./update.zip", new Progress<float>(percent => { Console.WriteLine($"\rProgress: {percent}% "); }));
|
||||
await File.WriteAllTextAsync("Install.sh",
|
||||
"#!/bin/bash\nunzip -qq -o update.zip \nrm update.zip\nchmod a+x DiscordBot");
|
||||
|
||||
try
|
||||
{
|
||||
var url =
|
||||
$"https://github.com/Wizzy69/SethDiscordBot/releases/download/v{newVersion}/net6.0.zip";
|
||||
Process.Start($"{Functions.dataFolder}Applications/Updater.exe",
|
||||
$"{newVersion} {url} {Process.GetCurrentProcess().ProcessName}");
|
||||
Console.WriteLine("executing: chmod a+x Install.sh");
|
||||
Process.Start("chmod", "a+x Install.sh").WaitForExit();
|
||||
Process.Start("Install.sh").WaitForExit();
|
||||
|
||||
Console.WriteLine("executing: rm Install.sh");
|
||||
Process.Start("rm", "Install.sh").WaitForExit();
|
||||
|
||||
Config.Logger.Log("The new version of the bot has been installed.");
|
||||
Console.WriteLine("Please restart the bot.");
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
var url =
|
||||
$"https://github.com/Wizzy69/SethDiscordBot/releases/download/v{newVersion}/net6.0_linux.zip";
|
||||
if (Logger.isConsole)
|
||||
Console.SetCursorPosition(0, Console.CursorTop);
|
||||
Logger.WriteLine($"executing: download_file {url}");
|
||||
|
||||
await ServerCom.DownloadFileAsync(url, "./update.zip", new Progress<float>(percent => { Logger.Write($"\rProgress: {percent}% "); }));
|
||||
await File.WriteAllTextAsync("Install.sh",
|
||||
"#!/bin/bash\nunzip -qq -o update.zip \nrm update.zip\nchmod a+x DiscordBot");
|
||||
Logger.WriteLine();
|
||||
|
||||
try
|
||||
{
|
||||
Logger.WriteLine("executing: chmod a+x Install.sh");
|
||||
Process.Start("chmod", "a+x Install.sh").WaitForExit();
|
||||
Process.Start("Install.sh").WaitForExit();
|
||||
|
||||
Logger.WriteLine("executing: rm Install.sh");
|
||||
Process.Start("rm", "Install.sh").WaitForExit();
|
||||
|
||||
Logger.WriteLine("The new version of the bot has been installed.");
|
||||
Logger.WriteLine("Please restart the bot.");
|
||||
Environment.Exit(0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteErrFile(ex.Message);
|
||||
if (ex.Message.Contains("Access de"))
|
||||
Logger.WriteLine("Please run the bot as root.");
|
||||
}
|
||||
|
||||
//Process.Start(Functions.dataFolder + "Applications/Updater", $"{url}");
|
||||
|
||||
Config.Logger.Log(ex.Message, "Updater", LogLevel.ERROR);
|
||||
if (ex.Message.Contains("Access de"))
|
||||
Config.Logger.Log("Please run the bot as root.");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case "UpdaterVersion":
|
||||
case "LauncherVersion":
|
||||
var updaternewversion = s[1];
|
||||
//File.WriteAllText(updaternewversion + ".txt", updaternewversion);
|
||||
if (Functions.GetOperatingSystem() == OperatingSystem.LINUX)
|
||||
break;
|
||||
|
||||
if (!await Config.Variables.ExistsAsync("UpdaterVersion"))
|
||||
await Config.Variables.AddAsync("UpdaterVersion", "0.0.0.0", false);
|
||||
if (await Config.Variables.GetValueAsync("UpdaterVersion") != updaternewversion ||
|
||||
!File.Exists(Functions.dataFolder+"Applications/Updater.exe"))
|
||||
Directory.CreateDirectory(Functions.dataFolder + "Applications");
|
||||
if (!Config.Data.ContainsKey("LauncherVersion"))
|
||||
Config.Data["LauncherVersion"] = "0.0.0.0";
|
||||
if (Config.Data["LauncherVersion"] != updaternewversion ||
|
||||
!File.Exists("./Launcher.exe"))
|
||||
{
|
||||
Console.Clear();
|
||||
Logger.WriteLine("Installing updater ...\nDo NOT close the bot during update !");
|
||||
var bar = new Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
Console.WriteLine("Installing a new Launcher ...\nDo NOT close the bot during update !");
|
||||
var bar = new Utilities.Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
bar.Start();
|
||||
await ServerCom.DownloadFileNoProgressAsync(
|
||||
"https://github.com/Wizzy69/installer/releases/download/release-1-discordbot/Updater.exe",
|
||||
$"{Functions.dataFolder}Applications/Updater.exe");
|
||||
await ServerCom.DownloadFileAsync(URLs["WindowsLauncher"], $"./Launcher.exe", null);
|
||||
//await ArchiveManager.ExtractArchive("./Updater.zip", "./", null,
|
||||
// UnzipProgressType.PercentageFromTotalSize);
|
||||
await Config.Variables.SetValueAsync("UpdaterVersion", updaternewversion);
|
||||
Config.Data["LauncherVersion"] = updaternewversion;
|
||||
// File.Delete("Updater.zip");
|
||||
bar.Stop("Updater has been updated !");
|
||||
bar.Stop("The launcher has been updated !");
|
||||
Console.Clear();
|
||||
}
|
||||
|
||||
@@ -355,146 +325,4 @@ public class Program
|
||||
|
||||
Console.Clear();
|
||||
}
|
||||
|
||||
public static void GenerateStartUI(string titleMessage)
|
||||
{
|
||||
Application.Init();
|
||||
var top = Application.Top;
|
||||
var win = new Window("Discord Bot Config - " + Assembly.GetExecutingAssembly().GetName().Version)
|
||||
{
|
||||
X = 0,
|
||||
Y = 1,
|
||||
Width = Dim.Fill(),
|
||||
Height = Dim.Fill()
|
||||
};
|
||||
|
||||
top.Add(win);
|
||||
|
||||
var labelInfo = new Label(titleMessage)
|
||||
{
|
||||
X = Pos.Center(),
|
||||
Y = 2
|
||||
};
|
||||
|
||||
|
||||
var labelToken = new Label("Please insert your token here: ")
|
||||
{
|
||||
X = 5,
|
||||
Y = 5
|
||||
};
|
||||
|
||||
var textFiledToken = new TextField(Config.Variables.GetValue("token") ?? "")
|
||||
{
|
||||
X = Pos.Left(labelToken) + labelToken.Text.Length + 2,
|
||||
Y = labelToken.Y,
|
||||
Width = 70
|
||||
};
|
||||
|
||||
var labelPrefix = new Label("Please insert your prefix here: ")
|
||||
{
|
||||
X = 5,
|
||||
Y = 8
|
||||
};
|
||||
var textFiledPrefix = new TextField(Config.Variables.GetValue("prefix") ?? "")
|
||||
{
|
||||
X = Pos.Left(labelPrefix) + labelPrefix.Text.Length + 2,
|
||||
Y = labelPrefix.Y,
|
||||
Width = 1
|
||||
};
|
||||
|
||||
var labelServerid = new Label("Please insert your server id here (optional): ")
|
||||
{
|
||||
X = 5,
|
||||
Y = 11
|
||||
};
|
||||
var textFiledServerID = new TextField(Config.Variables.GetValue("ServerID") ?? "")
|
||||
{
|
||||
X = Pos.Left(labelServerid) + labelServerid.Text.Length + 2,
|
||||
Y = labelServerid.Y,
|
||||
Width = 18
|
||||
};
|
||||
|
||||
var button = new Button("Submit")
|
||||
{
|
||||
X = Pos.Center() - 10,
|
||||
Y = 16
|
||||
};
|
||||
|
||||
var button2 = new Button("License")
|
||||
{
|
||||
X = Pos.Center() + 10,
|
||||
Y = 16
|
||||
};
|
||||
|
||||
var button3 = new Button("ⓘ")
|
||||
{
|
||||
X = Pos.Left(textFiledServerID) + 20,
|
||||
Y = textFiledServerID.Y
|
||||
};
|
||||
|
||||
Console.CancelKeyPress += (sender, e) => { top.Running = false; };
|
||||
|
||||
button.Clicked += () =>
|
||||
{
|
||||
var passMessage = "";
|
||||
if (textFiledToken.Text.Length != 70 && textFiledToken.Text.Length != 59)
|
||||
passMessage += "Invalid token, ";
|
||||
if (textFiledPrefix.Text.ContainsAny("0123456789/\\ ") || textFiledPrefix.Text.Length != 1)
|
||||
passMessage += "Invalid prefix, ";
|
||||
if (textFiledServerID.Text.Length != 18 && textFiledServerID.Text.Length > 0)
|
||||
passMessage += "Invalid serverID";
|
||||
|
||||
if (passMessage != "")
|
||||
{
|
||||
MessageBox.ErrorQuery("Discord Bot Settings",
|
||||
"Failed to pass check. Invalid information given:\n" + passMessage, "Retry");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Config.Variables.Add("ServerID", (string)textFiledServerID.Text, true);
|
||||
Config.Variables.Add("token", (string)textFiledToken.Text, true);
|
||||
Config.Variables.Add("prefix", (string)textFiledPrefix.Text, true);
|
||||
|
||||
MessageBox.Query("Discord Bot Settings", "Successfully saved config !\nJust start the bot :D",
|
||||
"Start :D");
|
||||
top.Running = false;
|
||||
};
|
||||
|
||||
button2.Clicked += async () =>
|
||||
{
|
||||
var license =
|
||||
await ServerCom.ReadTextFromURL(
|
||||
"https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/LICENSE.txt");
|
||||
var ProductLicense =
|
||||
"Seth Discord Bot\n\nDeveloped by Wizzy#9181\nThis application can be used and modified by anyone. Plugin development for this application is also free and supported";
|
||||
var r = MessageBox.Query("Discord Bot Settings", ProductLicense, "Close", "Read about libraries used");
|
||||
if (r == 1)
|
||||
{
|
||||
var i = 0;
|
||||
while (i < license.Count)
|
||||
{
|
||||
var print_message = license[i++] + "\n";
|
||||
for (; i < license.Count && !license[i].StartsWith("-----------"); i++)
|
||||
print_message += license[i] + "\n";
|
||||
if (print_message.Contains("https://"))
|
||||
print_message += "\n\nCTRL + Click on a link to open it";
|
||||
if (MessageBox.Query("Licenses", print_message, "Next", "Quit") == 1) break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
button3.Clicked += () =>
|
||||
{
|
||||
MessageBox.Query("Discord Bot Settings",
|
||||
"Server ID can be found in Server settings => Widget => Server ID",
|
||||
"Close");
|
||||
};
|
||||
|
||||
win.Add(labelInfo, labelPrefix, labelServerid, labelToken);
|
||||
win.Add(textFiledToken, textFiledPrefix, textFiledServerID, button3);
|
||||
win.Add(button, button2);
|
||||
Application.Run();
|
||||
Application.Shutdown();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiscordBot
|
||||
{
|
||||
internal class StartupArguments
|
||||
{
|
||||
public string runArgs { get; } = "";
|
||||
public bool loadPluginsAtStartup { get; } = true;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace PluginManager.Items;
|
||||
namespace DiscordBot.Utilities;
|
||||
|
||||
public class ConsoleCommand
|
||||
{
|
||||
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others;
|
||||
using PluginManager;
|
||||
|
||||
namespace DiscordBot.Utilities;
|
||||
|
||||
public static class Utilities
|
||||
{
|
||||
@@ -46,38 +49,38 @@ public static class Utilities
|
||||
foreach (var row in data)
|
||||
{
|
||||
if (row[0][0] == tableLine)
|
||||
Logger.Write(tableCross);
|
||||
Console.Write(tableCross);
|
||||
else
|
||||
Logger.Write(tableWall);
|
||||
Console.Write(tableWall);
|
||||
for (var l = 0; l < row.Length; l++)
|
||||
{
|
||||
if (row[l][0] == tableLine)
|
||||
{
|
||||
for (var i = 0; i < len[l] + 4; ++i)
|
||||
Logger.Write(tableLine);
|
||||
Console.Write(tableLine);
|
||||
}
|
||||
else if (row[l].Length == len[l])
|
||||
{
|
||||
Logger.Write(" ");
|
||||
Logger.Write(row[l]);
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
Console.Write(row[l]);
|
||||
Console.Write(" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
var lenHalf = row[l].Length / 2;
|
||||
for (var i = 0; i < (len[l] + 4) / 2 - lenHalf; ++i)
|
||||
Logger.Write(" ");
|
||||
Logger.Write(row[l]);
|
||||
Console.Write(" ");
|
||||
Console.Write(row[l]);
|
||||
for (var i = (len[l] + 4) / 2 + lenHalf + 1; i < len[l] + 4; ++i)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
if (row[l].Length % 2 == 0)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
}
|
||||
|
||||
Logger.Write(row[l][0] == tableLine ? tableCross : tableWall);
|
||||
Console.Write(row[l][0] == tableLine ? tableCross : tableWall);
|
||||
}
|
||||
|
||||
Logger.WriteLine(); //end line
|
||||
Console.WriteLine(); //end line
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -95,44 +98,44 @@ public static class Utilities
|
||||
|
||||
foreach (var row in data)
|
||||
{
|
||||
Logger.Write("\t");
|
||||
Console.Write("\t");
|
||||
if (row[0] == "-")
|
||||
Logger.Write("+");
|
||||
Console.Write("+");
|
||||
else
|
||||
Logger.Write("|");
|
||||
Console.Write("|");
|
||||
|
||||
foreach (var s in row)
|
||||
{
|
||||
if (s == "-")
|
||||
{
|
||||
for (var i = 0; i < maxLen + 4; ++i)
|
||||
Logger.Write("-");
|
||||
Console.Write("-");
|
||||
}
|
||||
else if (s.Length == maxLen)
|
||||
{
|
||||
Logger.Write(" ");
|
||||
Logger.Write(s);
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
Console.Write(s);
|
||||
Console.Write(" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
var lenHalf = s.Length / 2;
|
||||
for (var i = 0; i < div - lenHalf; ++i)
|
||||
Logger.Write(" ");
|
||||
Logger.Write(s);
|
||||
Console.Write(" ");
|
||||
Console.Write(s);
|
||||
for (var i = div + lenHalf + 1; i < maxLen + 4; ++i)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
if (s.Length % 2 == 0)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
}
|
||||
|
||||
if (s == "-")
|
||||
Logger.Write("+");
|
||||
Console.Write("+");
|
||||
else
|
||||
Logger.Write("|");
|
||||
Console.Write("|");
|
||||
}
|
||||
|
||||
Logger.WriteLine(); //end line
|
||||
Console.WriteLine(); //end line
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -153,12 +156,12 @@ public static class Utilities
|
||||
{
|
||||
if (data[i][j] == "-")
|
||||
data[i][j] = " ";
|
||||
Logger.Write(data[i][j]);
|
||||
Console.Write(data[i][j]);
|
||||
for (var k = 0; k < widths[j] - data[i][j].Length + 1 + space_between_columns; k++)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
}
|
||||
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -169,16 +172,6 @@ public static class Utilities
|
||||
|
||||
public static void WriteColorText(string text, bool appendNewLineAtEnd = true)
|
||||
{
|
||||
if (!Logger.isConsole)
|
||||
{
|
||||
foreach (var item in Colors)
|
||||
text = text.Replace($"{ColorPrefix}{item.Key}", "").Replace("&c", "");
|
||||
Logger.Write(text);
|
||||
if (appendNewLineAtEnd)
|
||||
Logger.WriteLine();
|
||||
return;
|
||||
|
||||
}
|
||||
var initialForeGround = Console.ForegroundColor;
|
||||
var input = text.ToCharArray();
|
||||
for (var i = 0; i < input.Length; i++)
|
||||
@@ -200,12 +193,51 @@ public static class Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Write(input[i]);
|
||||
Console.Write(input[i]);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = initialForeGround;
|
||||
if (appendNewLineAtEnd)
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public class Spinner
|
||||
{
|
||||
private Thread thread;
|
||||
private bool isRunning;
|
||||
private readonly string[] Sequence;
|
||||
private int position;
|
||||
|
||||
public Spinner()
|
||||
{
|
||||
Sequence = new[] {"|", "/", "-", "\\"};
|
||||
position = 0;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Console.CursorVisible = false;
|
||||
isRunning=true;
|
||||
thread = new Thread(() => {
|
||||
while (isRunning)
|
||||
{
|
||||
Console.Write("\r" + Sequence[position]);
|
||||
position++;
|
||||
if (position >= Sequence.Length)
|
||||
position = 0;
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
});
|
||||
|
||||
thread.Start();
|
||||
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
isRunning=false;
|
||||
Console.CursorVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -222,8 +254,6 @@ public static class Utilities
|
||||
|
||||
public ProgressBar(ProgressBarType type)
|
||||
{
|
||||
if (!Logger.isConsole)
|
||||
throw new Exception("This class (or function) can be used with console only. For UI please use another approach.");
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -237,6 +267,7 @@ public static class Utilities
|
||||
|
||||
public async void Start()
|
||||
{
|
||||
Console.WriteLine();
|
||||
if (type != ProgressBarType.NO_END)
|
||||
throw new Exception("Only NO_END progress bar can use this method");
|
||||
if (isRunning)
|
||||
@@ -284,9 +315,9 @@ public static class Utilities
|
||||
{
|
||||
Console.CursorLeft = 0;
|
||||
for (var i = 0; i < BarLength + message.Length + 1; i++)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
Console.CursorLeft = 0;
|
||||
Logger.WriteLine(message);
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,14 +332,14 @@ public static class Utilities
|
||||
private void UpdateNoEnd(string message)
|
||||
{
|
||||
Console.CursorLeft = 0;
|
||||
Logger.Write("[");
|
||||
Console.Write("[");
|
||||
for (var i = 1; i <= position; i++)
|
||||
Logger.Write(" ");
|
||||
Logger.Write("<==()==>");
|
||||
Console.Write(" ");
|
||||
Console.Write("<==()==>");
|
||||
position += positive ? 1 : -1;
|
||||
for (var i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++)
|
||||
Logger.Write(" ");
|
||||
Logger.Write("] " + message);
|
||||
Console.Write(" ");
|
||||
Console.Write("] " + message);
|
||||
|
||||
|
||||
if (position == BarLength - 1 || position == 1)
|
||||
@@ -318,14 +349,14 @@ public static class Utilities
|
||||
private void UpdateNoEnd()
|
||||
{
|
||||
Console.CursorLeft = 0;
|
||||
Logger.Write("[");
|
||||
Console.Write("[");
|
||||
for (var i = 1; i <= position; i++)
|
||||
Logger.Write(" ");
|
||||
Logger.Write("<==()==>");
|
||||
Console.Write(" ");
|
||||
Console.Write("<==()==>");
|
||||
position += positive ? 1 : -1;
|
||||
for (var i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++)
|
||||
Logger.Write(" ");
|
||||
Logger.Write("]");
|
||||
Console.Write(" ");
|
||||
Console.Write("]");
|
||||
|
||||
|
||||
if (position == BarLength - 1 || position == 1)
|
||||
@@ -335,9 +366,9 @@ public static class Utilities
|
||||
private void UpdateNormal(float progress)
|
||||
{
|
||||
Console.CursorLeft = 0;
|
||||
Logger.Write("[");
|
||||
Console.Write("[");
|
||||
Console.CursorLeft = BarLength;
|
||||
Logger.Write("]");
|
||||
Console.Write("]");
|
||||
Console.CursorLeft = 1;
|
||||
var onechunk = 30.0f / Max;
|
||||
|
||||
@@ -347,22 +378,22 @@ public static class Utilities
|
||||
{
|
||||
Console.BackgroundColor = NoColor ? ConsoleColor.Black : Color;
|
||||
Console.CursorLeft = position++;
|
||||
Logger.Write("#");
|
||||
Console.Write("#");
|
||||
}
|
||||
|
||||
for (var i = position; i < BarLength; i++)
|
||||
{
|
||||
Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray;
|
||||
Console.CursorLeft = position++;
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
}
|
||||
|
||||
Console.CursorLeft = BarLength + 4;
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
if (progress.CanAproximateTo(Max))
|
||||
Logger.Write(progress + " % ✓");
|
||||
Console.Write(progress + " % ✓");
|
||||
else
|
||||
Logger.Write(MathF.Round(progress, 2) + " % ");
|
||||
Console.Write(MathF.Round(progress, 2) + " % ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
@@ -9,34 +10,40 @@ using System.Threading.Tasks;
|
||||
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager;
|
||||
using PluginManager.Loaders;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Others;
|
||||
|
||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
||||
|
||||
namespace PluginManager.Items;
|
||||
namespace DiscordBot.Utilities;
|
||||
|
||||
public class ConsoleCommandsHandler
|
||||
{
|
||||
private static readonly PluginsManager manager =
|
||||
new("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/Plugins.txt");
|
||||
|
||||
private static readonly List<ConsoleCommand> commandList = new();
|
||||
public static ConsoleCommandsHandler handler;
|
||||
private readonly PluginsManager manager;
|
||||
|
||||
private readonly List<ConsoleCommand> commandList = new();
|
||||
|
||||
|
||||
private static bool isDownloading;
|
||||
private static bool pluginsLoaded;
|
||||
private readonly DiscordSocketClient? client;
|
||||
private bool isDownloading;
|
||||
private bool pluginsLoaded;
|
||||
private DiscordSocketClient client;
|
||||
|
||||
public ConsoleCommandsHandler(DiscordSocketClient client)
|
||||
{
|
||||
if (!Logger.isConsole) throw new Exception("Can not use ConsoleCommandsHandler for Non console apps");
|
||||
this.client = client;
|
||||
|
||||
manager = new PluginsManager(Program.URLs["PluginList"], Program.URLs["PluginVersions"]);
|
||||
InitializeBasicCommands();
|
||||
Console.WriteLine("Done");
|
||||
|
||||
|
||||
//Logger.WriteLine("Initialized console command handler !");
|
||||
if (handler == null)
|
||||
handler = this;
|
||||
else
|
||||
throw new Exception("ConsoleCommandsHandler already initialized");
|
||||
}
|
||||
|
||||
private void InitializeBasicCommands()
|
||||
@@ -47,12 +54,14 @@ public class ConsoleCommandsHandler
|
||||
{
|
||||
if (args.Length <= 1)
|
||||
{
|
||||
Logger.WriteLine("Available commands:");
|
||||
var items = new List<string[]>();
|
||||
items.Add(new[] { "-", "-", "-" });
|
||||
items.Add(new[] { "Command", "Description", "Usage" });
|
||||
items.Add(new[] { " ", " ", "Argument type: <optional> [required]" });
|
||||
items.Add(new[] { "-", "-", "-" });
|
||||
Console.WriteLine("Available commands:");
|
||||
var items = new List<string[]>
|
||||
{
|
||||
new[] { "-", "-", "-" },
|
||||
new[] { "Command", "Description", "Usage" },
|
||||
new[] { " ", " ", "Argument type: <optional> [required]" },
|
||||
new[] { "-", "-", "-" }
|
||||
};
|
||||
|
||||
foreach (var command in commandList)
|
||||
{
|
||||
@@ -68,17 +77,16 @@ public class ConsoleCommandsHandler
|
||||
foreach (var command in commandList)
|
||||
if (command.CommandName == args[1])
|
||||
{
|
||||
Logger.WriteLine("Command description: " + command.Description);
|
||||
Logger.WriteLine("Command execution format:" + command.Usage);
|
||||
Console.WriteLine("Command description: " + command.Description);
|
||||
Console.WriteLine("Command execution format:" + command.Usage);
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.WriteLine("Command not found");
|
||||
Console.WriteLine("Command not found");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
AddCommand("lp", "Load plugins", () =>
|
||||
{
|
||||
if (pluginsLoaded)
|
||||
@@ -92,16 +100,16 @@ public class ConsoleCommandsHandler
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Logger.WriteLine("[CMD] Successfully loaded command : " + name);
|
||||
Console.WriteLine("[CMD] Successfully loaded command : " + name);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
if (exception is null)
|
||||
Logger.WriteLine("An error occured while loading: " + name);
|
||||
Console.WriteLine("An error occured while loading: " + name);
|
||||
else
|
||||
Logger.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
|
||||
Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
@@ -114,12 +122,12 @@ public class ConsoleCommandsHandler
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Logger.WriteLine("[EVENT] Successfully loaded event : " + name);
|
||||
Console.WriteLine("[EVENT] Successfully loaded event : " + name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Logger.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
||||
Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
@@ -133,12 +141,12 @@ public class ConsoleCommandsHandler
|
||||
if (success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Logger.WriteLine("[SLASH] Successfully loaded command : " + name);
|
||||
Console.WriteLine("[SLASH] Successfully loaded command : " + name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Logger.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message);
|
||||
Console.WriteLine("[SLASH] Failed to load command : " + name + " because " + exception!.Message);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
@@ -150,15 +158,37 @@ public class ConsoleCommandsHandler
|
||||
}
|
||||
);
|
||||
|
||||
AddCommand("listplugs", "list available plugins", () => { manager.ListAvailablePlugins().Wait(); });
|
||||
AddCommand("listplugs", "list available plugins", async () => {
|
||||
if(manager == null)
|
||||
{
|
||||
Console.WriteLine("Plugin manager is null");
|
||||
return;
|
||||
}
|
||||
var data = await manager.GetAvailablePlugins();
|
||||
var items = new List<string[]>
|
||||
{
|
||||
new[] { "-", "-", "-", "-" },
|
||||
new[] { "Name", "Type", "Description", "Required" },
|
||||
new[] { "-", "-", "-", "-" }
|
||||
};
|
||||
|
||||
foreach (var plugin in data)
|
||||
{
|
||||
items.Add(new[] { plugin[0], plugin[1], plugin[2], plugin[3] });
|
||||
}
|
||||
|
||||
items.Add(new[] { "-", "-", "-", "-" });
|
||||
Utilities.FormatAndAlignTable(items, TableFormat.DEFAULT);
|
||||
});
|
||||
|
||||
AddCommand("dwplug", "download plugin", "dwplug [name]", async args =>
|
||||
{
|
||||
isDownloading = true;
|
||||
Utilities.Spinner spinner = new Utilities.Spinner();
|
||||
if (args.Length == 1)
|
||||
{
|
||||
isDownloading = false;
|
||||
Logger.WriteLine("Please specify plugin name");
|
||||
Console.WriteLine("Please specify plugin name");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -182,32 +212,25 @@ public class ConsoleCommandsHandler
|
||||
return;
|
||||
}
|
||||
|
||||
spinner.Start();
|
||||
|
||||
string path;
|
||||
if (info[0] == "Plugin")
|
||||
path = "./Data/Plugins/" + name + ".dll";
|
||||
else
|
||||
path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}";
|
||||
|
||||
if (OperatingSystem.WINDOWS == Functions.GetOperatingSystem())
|
||||
{
|
||||
await ServerCom.DownloadFileAsync(info[1], path);
|
||||
}
|
||||
else if (OperatingSystem.LINUX == Functions.GetOperatingSystem())
|
||||
{
|
||||
var bar = new Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
bar.Start();
|
||||
await ServerCom.DownloadFileNoProgressAsync(info[1], path);
|
||||
bar.Stop("Plugin Downloaded !");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Downloading plugin {name} from {info[1]} to {path}");
|
||||
await ServerCom.DownloadFileAsync(info[1], path, null);
|
||||
|
||||
Logger.WriteLine("\n");
|
||||
Console.WriteLine("\n");
|
||||
|
||||
// check requirements if any
|
||||
|
||||
if (info.Length == 3 && info[2] != string.Empty && info[2] != null)
|
||||
{
|
||||
Logger.WriteLine($"Downloading requirements for plugin : {name}");
|
||||
Console.WriteLine($"Downloading requirements for plugin : {name}");
|
||||
|
||||
var lines = await ServerCom.ReadTextFromURL(info[2]);
|
||||
|
||||
@@ -216,49 +239,41 @@ public class ConsoleCommandsHandler
|
||||
if (!(line.Length > 0 && line.Contains(",")))
|
||||
continue;
|
||||
var split = line.Split(',');
|
||||
Logger.WriteLine($"\nDownloading item: {split[1]}");
|
||||
Console.WriteLine($"\nDownloading item: {split[1]}");
|
||||
if (File.Exists("./" + split[1])) File.Delete("./" + split[1]);
|
||||
if (OperatingSystem.WINDOWS == Functions.GetOperatingSystem())
|
||||
{
|
||||
await ServerCom.DownloadFileAsync(split[0], "./" + split[1]);
|
||||
}
|
||||
else if (OperatingSystem.LINUX == Functions.GetOperatingSystem())
|
||||
{
|
||||
var bar = new Utilities.ProgressBar(ProgressBarType.NO_END);
|
||||
bar.Start();
|
||||
await ServerCom.DownloadFileNoProgressAsync(split[0], "./" + split[1]);
|
||||
bar.Stop("Item downloaded !");
|
||||
}
|
||||
await ServerCom.DownloadFileAsync(split[0], "./" + split[1], null);
|
||||
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine("Item " + split[1] + " downloaded !");
|
||||
|
||||
Console.WriteLine();
|
||||
if (split[0].EndsWith(".pak"))
|
||||
{
|
||||
File.Move("./" + split[1], "./Data/PAKS/" + split[1], true);
|
||||
}
|
||||
else if (split[0].EndsWith(".zip") || split[0].EndsWith(".pkg"))
|
||||
{
|
||||
Logger.WriteLine($"Extracting {split[1]} ...");
|
||||
var bar = new Utilities.ProgressBar(
|
||||
ProgressBarType.NO_END);
|
||||
bar.Start();
|
||||
Console.WriteLine($"Extracting {split[1]} ...");
|
||||
await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
|
||||
UnzipProgressType.PercentageFromTotalSize);
|
||||
bar.Stop("Extracted");
|
||||
Logger.WriteLine("\n");
|
||||
UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE);
|
||||
Console.WriteLine("\n");
|
||||
File.Delete("./" + split[1]);
|
||||
}
|
||||
}
|
||||
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine();
|
||||
}
|
||||
spinner.Stop();
|
||||
|
||||
var ver = await ServerCom.GetVersionOfPackageFromWeb(name);
|
||||
var ver = await manager.GetVersionOfPackageFromWeb(name);
|
||||
if (ver is null) throw new Exception("Incorrect version");
|
||||
await Config.Plugins.SetVersionAsync(name, ver);
|
||||
Config.Plugins[name] = ver.ToShortString();
|
||||
|
||||
isDownloading = false;
|
||||
|
||||
await ExecuteCommad("localload " + name);
|
||||
|
||||
Config.Logger.Log("Plugin installed !", this, LogLevel.INFO);
|
||||
|
||||
//await ExecuteCommad("localload " + name);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -267,15 +282,15 @@ public class ConsoleCommandsHandler
|
||||
{
|
||||
if (args.Length != 2)
|
||||
return;
|
||||
if (!Config.Variables.Exists(args[1]))
|
||||
if (!Config.Data.ContainsKey(args[1]))
|
||||
return;
|
||||
|
||||
var data = Config.Variables.GetValue(args[1]);
|
||||
Logger.WriteLine($"{args[1]} => {data}");
|
||||
var data = Config.Data[args[1]];
|
||||
Console.WriteLine($"{args[1]} => {data}");
|
||||
}
|
||||
);
|
||||
|
||||
AddCommand("add", "add variable to the system variables", "add [key] [value] [isReadOnly=true/false]", args =>
|
||||
AddCommand("add", "add variable to the system variables", "add [key] [value]", args =>
|
||||
{
|
||||
if (args.Length < 4)
|
||||
return;
|
||||
@@ -285,12 +300,12 @@ public class ConsoleCommandsHandler
|
||||
|
||||
try
|
||||
{
|
||||
Config.Variables.Add(key, value, isReadOnly);
|
||||
Logger.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
|
||||
Config.Data[key] = value;
|
||||
Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -299,7 +314,7 @@ public class ConsoleCommandsHandler
|
||||
{
|
||||
if (args.Length < 2)
|
||||
return;
|
||||
Config.Variables.RemoveKey(args[1]);
|
||||
Config.Data.Remove(args[1]);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -308,9 +323,12 @@ public class ConsoleCommandsHandler
|
||||
if (client is null)
|
||||
return;
|
||||
|
||||
Settings.sqlDatabase.Stop();
|
||||
await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data);
|
||||
await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins);
|
||||
await client.StopAsync();
|
||||
await client.DisposeAsync();
|
||||
|
||||
Config.Logger.SaveToFile();
|
||||
await Task.Delay(1000);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
@@ -338,7 +356,7 @@ public class ConsoleCommandsHandler
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.Message);
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -354,156 +372,46 @@ public class ConsoleCommandsHandler
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.Message);
|
||||
Logger.WriteErrFile(ex);
|
||||
Console.WriteLine(ex.Message);
|
||||
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||
}
|
||||
});
|
||||
|
||||
AddCommand("remplug", "Remove a plugin", "remplug [plugName]", async args =>
|
||||
{
|
||||
if (args.Length <= 1) return;
|
||||
|
||||
isDownloading = true;
|
||||
var plugName = string.Join(' ', args, 1, args.Length - 1);
|
||||
if (pluginsLoaded)
|
||||
{
|
||||
if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS)
|
||||
{
|
||||
Process.Start("DiscordBot.exe", $"/remplug {plugName}");
|
||||
await Task.Delay(100);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Process.Start("./DiscordBot", $"/remplug {plugName}");
|
||||
await Task.Delay(100);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
isDownloading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var location = $"./Data/Plugins/{plugName}.dll";
|
||||
|
||||
if (!File.Exists(location))
|
||||
{
|
||||
Logger.WriteLine("The plugin does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
File.Delete(location);
|
||||
|
||||
Logger.WriteLine("Removed the plugin DLL. Checking for other files ...");
|
||||
|
||||
var info = await manager.GetPluginLinkByName(plugName);
|
||||
if (info[2] != string.Empty)
|
||||
{
|
||||
var lines = await ServerCom.ReadTextFromURL(info[2]);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (!(line.Length > 0 && line.Contains(",")))
|
||||
continue;
|
||||
var split = line.Split(',');
|
||||
if (File.Exists("./" + split[1]))
|
||||
File.Delete("./" + split[1]);
|
||||
|
||||
|
||||
Logger.WriteLine("Removed: " + split[1]);
|
||||
}
|
||||
|
||||
if (Directory.Exists($"./Data/Plugins/{plugName}"))
|
||||
Directory.Delete($"./Data/Plugins/{plugName}", true);
|
||||
|
||||
if (Directory.Exists(plugName))
|
||||
Directory.Delete(plugName, true);
|
||||
}
|
||||
|
||||
isDownloading = false;
|
||||
Logger.WriteLine(plugName + " has been successfully deleted !");
|
||||
});
|
||||
|
||||
AddCommand("reload", "Reload the bot with all plugins", () =>
|
||||
{
|
||||
|
||||
if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS)
|
||||
{
|
||||
Process.Start("DiscordBot.exe", "lp");
|
||||
HandleCommand("sd");
|
||||
}
|
||||
else
|
||||
{
|
||||
//HandleCommand("sd");
|
||||
Console.WriteLine("This command can not be used outside of Windows yet. Please restart the bot using the manual way");
|
||||
}
|
||||
});
|
||||
|
||||
//AddCommand("");
|
||||
|
||||
//Sort the commands by name
|
||||
commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName));
|
||||
}
|
||||
|
||||
public static void AddCommand(string command, string description, string usage, Action<string[]> action)
|
||||
public void AddCommand(string command, string description, string usage, Action<string[]> action)
|
||||
{
|
||||
Console.WriteLine($"Adding command {command} ...");
|
||||
commandList.Add(new ConsoleCommand
|
||||
{ CommandName = command, Description = description, Action = action, Usage = usage });
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Utilities.WriteColorText($"Command &r{command} &cadded to the list of commands");
|
||||
}
|
||||
|
||||
public static void AddCommand(string command, string description, Action action)
|
||||
public void AddCommand(string command, string description, Action action)
|
||||
{
|
||||
AddCommand(command, description, command, args => action());
|
||||
}
|
||||
|
||||
public static void RemoveCommand(string command)
|
||||
public void RemoveCommand(string command)
|
||||
{
|
||||
commandList.RemoveAll(x => x.CommandName == command);
|
||||
}
|
||||
|
||||
public static bool CommandExists(string command)
|
||||
public bool CommandExists(string command)
|
||||
{
|
||||
return GetCommand(command) is not null;
|
||||
}
|
||||
|
||||
public static ConsoleCommand? GetCommand(string command)
|
||||
public ConsoleCommand? GetCommand(string command)
|
||||
{
|
||||
return commandList.FirstOrDefault(t => t.CommandName == command);
|
||||
}
|
||||
|
||||
/* public static async Task ExecuteSpecialCommand(string command)
|
||||
{
|
||||
if (!command.StartsWith("_")) return;
|
||||
|
||||
string[] args = command.Split(' ');
|
||||
foreach (var item in commandList)
|
||||
if (item.CommandName == args[0])
|
||||
{
|
||||
Logger.WriteLine();
|
||||
item.Action(args);
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
public static async Task ExecuteCommad(string command)
|
||||
{
|
||||
if (!Logger.isConsole)
|
||||
throw new Exception("Can not use console based commands on non console based application !");
|
||||
var args = command.Split(' ');
|
||||
foreach (var item in commandList.ToList())
|
||||
if (item.CommandName == args[0])
|
||||
{
|
||||
item.Action.Invoke(args);
|
||||
while (isDownloading) await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HandleCommand(string command, bool removeCommandExecution = true)
|
||||
{
|
||||
if (!Logger.isConsole)
|
||||
throw new Exception("Can not use console based commands on non console based application !");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
var args = command.Split(' ');
|
||||
foreach (var item in commandList.ToList())
|
||||
@@ -512,22 +420,20 @@ public class ConsoleCommandsHandler
|
||||
if (args[0].StartsWith("_"))
|
||||
throw new Exception("This command is reserved for internal worker and can not be executed manually !");
|
||||
|
||||
if (Logger.isConsole)
|
||||
if (removeCommandExecution)
|
||||
{
|
||||
Console.SetCursorPosition(0, Console.CursorTop - 1);
|
||||
for (var i = 0; i < command.Length + 30; i++)
|
||||
Logger.Write(" ");
|
||||
Console.Write(" ");
|
||||
Console.SetCursorPosition(0, Console.CursorTop);
|
||||
}
|
||||
|
||||
Logger.WriteLine();
|
||||
Console.WriteLine();
|
||||
item.Action(args);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
//Logger.WriteLine($"Executing: {args[0]} with the following parameters: {args.MergeStrings(1)}");
|
||||
}
|
||||
}
|
||||
21
DiscordBot/Utilities/Enums.cs
Normal file
21
DiscordBot/Utilities/Enums.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiscordBot.Utilities
|
||||
{
|
||||
public enum TableFormat
|
||||
{
|
||||
CENTER_EACH_COLUMN_BASED,
|
||||
CENTER_OVERALL_LENGTH,
|
||||
DEFAULT
|
||||
}
|
||||
|
||||
public enum ProgressBarType
|
||||
{
|
||||
NORMAL,
|
||||
NO_END
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
using System;
|
||||
using System.Net.Mime;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager;
|
||||
namespace PluginManager.Bot;
|
||||
|
||||
namespace DiscordBot.Discord.Core;
|
||||
|
||||
internal class Boot
|
||||
public class Boot
|
||||
{
|
||||
/// <summary>
|
||||
/// The bot prefix
|
||||
@@ -57,19 +56,21 @@ internal class Boot
|
||||
/// <summary>
|
||||
/// The start method for the bot. This method is used to load the bot
|
||||
/// </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>
|
||||
/// <returns>Task</returns>
|
||||
public async Task Awake()
|
||||
public async Task Awake(DiscordSocketConfig? config = null)
|
||||
{
|
||||
var config = new DiscordSocketConfig
|
||||
{
|
||||
if (config is null)
|
||||
config = new DiscordSocketConfig
|
||||
{
|
||||
|
||||
AlwaysDownloadUsers = true,
|
||||
AlwaysDownloadUsers = true,
|
||||
|
||||
//Disable system clock checkup (for responses at slash commands)
|
||||
UseInteractionSnowflakeDate = false,
|
||||
//Disable system clock checkup (for responses at slash commands)
|
||||
UseInteractionSnowflakeDate = false,
|
||||
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
|
||||
client = new DiscordSocketClient(config);
|
||||
service = new CommandService();
|
||||
@@ -100,37 +101,32 @@ internal class Boot
|
||||
client.Disconnected += Client_Disconnected;
|
||||
}
|
||||
|
||||
private Task Client_Disconnected(Exception arg)
|
||||
private async Task Client_Disconnected(Exception arg)
|
||||
{
|
||||
if (arg.Message.Contains("401"))
|
||||
{
|
||||
Config.Variables.RemoveKey("token");
|
||||
Program.GenerateStartUI("The token is invalid");
|
||||
Config.Data.Remove("token");
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR);
|
||||
await Task.Delay(4000);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
Logger.WriteErrFile(arg);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task Client_LoggedOut()
|
||||
{
|
||||
Logger.WriteLine("Successfully Logged Out");
|
||||
Config.Logger.Log("Successfully Logged Out", this);
|
||||
await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
|
||||
}
|
||||
|
||||
private Task Ready()
|
||||
{
|
||||
Console.Title = "ONLINE";
|
||||
isReady = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task LoggedIn()
|
||||
{
|
||||
Console.Title = "CONNECTED";
|
||||
Logger.WriteLine("The bot has been logged in at " + DateTime.Now.ToShortDateString() + " (" +
|
||||
DateTime.Now.ToShortTimeString() + ")"
|
||||
);
|
||||
Config.Logger.Log("Successfully Logged In", this);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -140,21 +136,13 @@ internal class Boot
|
||||
{
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Critical:
|
||||
Logger.WriteErrFile(message.Message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("[ERROR] " + message.Message);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR);
|
||||
|
||||
break;
|
||||
|
||||
case LogSeverity.Info:
|
||||
case LogSeverity.Debug:
|
||||
Logger.WriteLogFile(message.Message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.WriteLine("[INFO] " + message.Message);
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Config.Logger.Log(message.Message, this);
|
||||
|
||||
|
||||
break;
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
@@ -10,9 +10,7 @@ using PluginManager.Loaders;
|
||||
using PluginManager.Others;
|
||||
using PluginManager.Others.Permissions;
|
||||
|
||||
using static PluginManager.Logger;
|
||||
|
||||
namespace DiscordBot.Discord.Core;
|
||||
namespace PluginManager.Bot;
|
||||
|
||||
internal class CommandHandler
|
||||
{
|
||||
@@ -63,9 +61,7 @@ internal class CommandHandler
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Console.WriteLine(ex.ToString());
|
||||
ex.WriteErrFile();
|
||||
Config.Logger.Log(ex.Message, "CommandHandler", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
@@ -110,15 +106,11 @@ internal class CommandHandler
|
||||
string mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
||||
|
||||
plugin = PluginLoader.Commands!
|
||||
.Where
|
||||
(
|
||||
plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
|
||||
(
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
|
||||
)
|
||||
)
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
|
||||
(
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
|
||||
));
|
||||
|
||||
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
|
||||
}
|
||||
@@ -126,16 +118,14 @@ internal class CommandHandler
|
||||
else
|
||||
{
|
||||
plugin = PluginLoader.Commands!
|
||||
.Where(
|
||||
p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||
(p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0].Substring(botPrefix.Length))))
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||
(p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0].Substring(botPrefix.Length))));
|
||||
cleanMessage = message.Content.Substring(botPrefix.Length);
|
||||
}
|
||||
if (plugin is null)
|
||||
throw new Exception($"Failed to run command ! " + message.CleanContent);
|
||||
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())
|
||||
return;
|
||||
@@ -146,12 +136,7 @@ internal class CommandHandler
|
||||
if(split.Length > 1)
|
||||
argsClean = string.Join(' ', split, 1, split.Length-1).Split(' ');
|
||||
|
||||
CmdArgs cmd = new() {
|
||||
context = context,
|
||||
cleanContent = cleanMessage,
|
||||
commandUsed = split[0],
|
||||
arguments = argsClean
|
||||
};
|
||||
DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean);
|
||||
|
||||
if (context.Channel is SocketDMChannel)
|
||||
plugin.ExecuteDM(cmd);
|
||||
@@ -159,9 +144,7 @@ internal class CommandHandler
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.WriteErrFile();
|
||||
|
||||
Console.WriteLine(ex.ToString());
|
||||
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,202 +1,167 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using PluginManager.Others;
|
||||
using System.Collections;
|
||||
using PluginManager.Online.Helpers;
|
||||
using PluginManager.Others.Logger;
|
||||
|
||||
namespace PluginManager;
|
||||
|
||||
public static class Config
|
||||
{
|
||||
private static bool IsLoaded = false;
|
||||
|
||||
public static DBLogger Logger;
|
||||
public static Json<string, string> Data;
|
||||
public static Json<string, string> Plugins;
|
||||
|
||||
public static async Task Initialize()
|
||||
{
|
||||
if (IsLoaded)
|
||||
return;
|
||||
|
||||
if (!await Settings.sqlDatabase.TableExistsAsync("Plugins"))
|
||||
await Settings.sqlDatabase.CreateTableAsync("Plugins", "PluginName", "Version");
|
||||
if (!await Settings.sqlDatabase.TableExistsAsync("Variables"))
|
||||
await Settings.sqlDatabase.CreateTableAsync("Variables", "VarName", "Value", "ReadOnly");
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
Directory.CreateDirectory("./Data/Plugins");
|
||||
Directory.CreateDirectory("./Data/PAKS");
|
||||
Directory.CreateDirectory("./Data/Logs/Logs");
|
||||
Directory.CreateDirectory("./Data/Logs/Errors");
|
||||
|
||||
Data = new Json<string, string>("./Data/Resources/config.json");
|
||||
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
|
||||
|
||||
Config.Data["LogFolder"] = "./Data/Logs/Logs";
|
||||
Config.Data["ErrorFolder"] = "./Data/Logs/Errors";
|
||||
|
||||
Logger = new DBLogger();
|
||||
|
||||
ArchiveManager.Initialize();
|
||||
|
||||
IsLoaded = true;
|
||||
|
||||
Logger.Log("Config initialized", LogLevel.INFO);
|
||||
}
|
||||
|
||||
public static class Variables
|
||||
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
public static async Task<string> GetValueAsync(string VarName)
|
||||
protected IDictionary<TKey, TValue> _dictionary;
|
||||
private readonly string _file = "";
|
||||
|
||||
public Json(string file)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return await Settings.sqlDatabase.GetValueAsync("Variables", "VarName", VarName, "Value");
|
||||
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
|
||||
this._file = file;
|
||||
}
|
||||
|
||||
public static string GetValue(string VarName)
|
||||
public async void Save()
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return Settings.sqlDatabase.GetValue("Variables", "VarName", VarName, "Value");
|
||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||
}
|
||||
|
||||
|
||||
public static async Task SetValueAsync(string VarName, string Value)
|
||||
public virtual void Add(TKey key, TValue value)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
|
||||
if (await IsReadOnlyAsync(VarName))
|
||||
throw new Exception($"Variable ({VarName}) is read only and can not be changed to {Value}");
|
||||
|
||||
await Settings.sqlDatabase.SetValueAsync("Variables", "VarName", VarName, "Value", Value);
|
||||
_dictionary.Add(key, value);
|
||||
}
|
||||
|
||||
public static void SetValue(string VarName, string Value)
|
||||
public void Clear() { _dictionary.Clear(); }
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
if (IsReadOnly(VarName))
|
||||
throw new Exception($"Variable ({VarName}) is read only and can not be changed to {Value}");
|
||||
Settings.sqlDatabase.SetValue("Variables", "VarName", VarName, "Value", Value);
|
||||
if (_dictionary == null)
|
||||
throw new Exception("Dictionary is null");
|
||||
|
||||
return _dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public virtual ICollection<TKey> Keys => _dictionary.Keys;
|
||||
|
||||
public static async Task<bool> IsReadOnlyAsync(string VarName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return (await Settings.sqlDatabase.GetValueAsync("Variables", "VarName", VarName, "ReadOnly")).Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
public virtual ICollection<TValue> Values => _dictionary.Values;
|
||||
|
||||
public static bool IsReadOnly(string VarName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return (Settings.sqlDatabase.GetValue("Variables", "VarName", VarName, "ReadOnly")).Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
public int Count => _dictionary.Count;
|
||||
|
||||
public static async Task SetReadOnlyAsync(string VarName, bool ReadOnly)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
await Settings.sqlDatabase.SetValueAsync("Variables", "VarName", VarName, "ReadOnly", ReadOnly ? "true" : "false");
|
||||
}
|
||||
public bool IsReadOnly => _dictionary.IsReadOnly;
|
||||
|
||||
public static void SetReadOnly(string VarName, bool ReadOnly)
|
||||
public virtual TValue this[TKey key]
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
Settings.sqlDatabase.SetValue("Variables", "VarName", VarName, "ReadOnly", ReadOnly ? "true" : "false");
|
||||
}
|
||||
|
||||
public static async Task<bool> ExistsAsync(string VarName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return await Settings.sqlDatabase.KeyExistsAsync("Variables", "VarName", VarName);
|
||||
}
|
||||
|
||||
public static bool Exists(string VarName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
return Settings.sqlDatabase.KeyExists("Variables", "VarName", VarName);
|
||||
}
|
||||
|
||||
public static async Task AddAsync(string VarName, string Value, bool ReadOnly = false)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
if (await ExistsAsync(VarName))
|
||||
get
|
||||
{
|
||||
await SetValueAsync(VarName, Value);
|
||||
await SetReadOnlyAsync(VarName, ReadOnly);
|
||||
return;
|
||||
}
|
||||
await Settings.sqlDatabase.InsertAsync("Variables", VarName, Value, ReadOnly ? "true" : "false");
|
||||
}
|
||||
if (_dictionary.TryGetValue(key, out TValue value)) return value;
|
||||
throw new Exception("Key not found in dictionary " + key.ToString() + " (Json )" + this.GetType().Name + ")");
|
||||
|
||||
public static void Add(string VarName, string Value, bool ReadOnly = false)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
if (Exists(VarName))
|
||||
}
|
||||
set
|
||||
{
|
||||
if (GetValue(VarName) == Value)
|
||||
return;
|
||||
|
||||
SetValue(VarName, Value);
|
||||
SetReadOnly(VarName, ReadOnly);
|
||||
return;
|
||||
if (_dictionary.ContainsKey(key))
|
||||
_dictionary[key] = value;
|
||||
else _dictionary.Add(key, value);
|
||||
}
|
||||
Settings.sqlDatabase.Insert("Variables", VarName, Value, ReadOnly ? "true" : "false");
|
||||
}
|
||||
|
||||
public static async Task RemoveKeyAsync(string VarName)
|
||||
public virtual bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
await Settings.sqlDatabase.RemoveKeyAsync("Variables", "VarName", VarName);
|
||||
return _dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public static void RemoveKey(string VarName)
|
||||
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded");
|
||||
Settings.sqlDatabase.RemoveKey("Variables", "VarName", VarName);
|
||||
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)
|
||||
{
|
||||
return _dictionary.Remove(key);
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
_dictionary.Add(item);
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return _dictionary.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
_dictionary.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return _dictionary.Remove(item);
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return _dictionary.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable)_dictionary).GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Plugins
|
||||
{
|
||||
public static async Task<string> GetVersionAsync(string pluginName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded yet");
|
||||
|
||||
string result = await Settings.sqlDatabase.GetValueAsync("Plugins", "PluginName", pluginName, "Version");
|
||||
if (result is null)
|
||||
return "0.0.0";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetVersion(string pluginName)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded yet");
|
||||
|
||||
string result = Settings.sqlDatabase.GetValue("Plugins", "PluginName", pluginName, "Version");
|
||||
if (result is null)
|
||||
return "0.0.0";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async Task SetVersionAsync(string pluginName, VersionString version)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded yet");
|
||||
|
||||
if (!await Settings.sqlDatabase.KeyExistsAsync("Plugins", "PluginName", pluginName))
|
||||
{
|
||||
await Settings.sqlDatabase.InsertAsync("Plugins", pluginName, version.ToShortString());
|
||||
return;
|
||||
}
|
||||
await Settings.sqlDatabase.SetValueAsync("Plugins", "PluginName", pluginName, "Version", version.ToShortString());
|
||||
}
|
||||
|
||||
public static void SetVersion(string pluginName, VersionString version)
|
||||
{
|
||||
if (!IsLoaded)
|
||||
throw new Exception("Config is not loaded yet");
|
||||
|
||||
if (!Settings.sqlDatabase.KeyExists("Plugins", "PluginName", pluginName))
|
||||
{
|
||||
Settings.sqlDatabase.Insert("Plugins", pluginName, version.ToShortString());
|
||||
return;
|
||||
}
|
||||
|
||||
Settings.sqlDatabase.SetValue("Plugins", "PluginName", pluginName, "Version", version.ToShortString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,10 @@ namespace PluginManager.Database
|
||||
private string ConnectionString;
|
||||
private SQLiteConnection Connection;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a SQL connection by specifing its private path
|
||||
/// </summary>
|
||||
/// <param name="fileName">The path to the database (it is starting from ./Data/Resources/)</param>
|
||||
public SqlDatabase(string fileName)
|
||||
{
|
||||
if (!fileName.StartsWith("./Data/Resources/"))
|
||||
@@ -21,13 +25,23 @@ namespace PluginManager.Database
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Open the SQL Connection. To close use the Stop() method
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task Open()
|
||||
{
|
||||
await Connection.OpenAsync();
|
||||
|
||||
//Console.WriteLine("Opened database successfully");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public async Task InsertAsync(string tableName, params string[] values)
|
||||
{
|
||||
string query = $"INSERT INTO {tableName} VALUES (";
|
||||
@@ -44,6 +58,14 @@ namespace PluginManager.Database
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public void Insert(string tableName, params string[] values)
|
||||
{
|
||||
string query = $"INSERT INTO {tableName} VALUES (";
|
||||
@@ -60,6 +82,13 @@ namespace PluginManager.Database
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
@@ -68,6 +97,13 @@ namespace PluginManager.Database
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public void RemoveKey(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
@@ -76,7 +112,13 @@ namespace PluginManager.Database
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> KeyExistsAsync(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
@@ -87,6 +129,13 @@ namespace PluginManager.Database
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public bool KeyExists(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
@@ -97,6 +146,14 @@ namespace PluginManager.Database
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</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="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
|
||||
public async Task SetValueAsync(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
@@ -108,6 +165,15 @@ namespace PluginManager.Database
|
||||
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</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="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
|
||||
public void SetValue(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
{
|
||||
@@ -117,7 +183,14 @@ namespace PluginManager.Database
|
||||
Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</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>
|
||||
public async Task<string?> GetValueAsync(string tableName, string keyName, string KeyValue,
|
||||
string ResultColumnName)
|
||||
{
|
||||
@@ -127,6 +200,15 @@ namespace PluginManager.Database
|
||||
return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</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>
|
||||
|
||||
public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName)
|
||||
{
|
||||
if (!TableExists(tableName))
|
||||
@@ -135,12 +217,23 @@ namespace PluginManager.Database
|
||||
return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop the connection to the SQL Database
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async void Stop()
|
||||
{
|
||||
await Connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async Task AddColumnsToTableAsync(string tableName, string[] columns)
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
public async Task AddColumnsToTableAsync(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
@@ -153,13 +246,21 @@ namespace PluginManager.Database
|
||||
{
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} TEXT";
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddColumnsToTable(string tableName, string[] columns)
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
@@ -172,12 +273,17 @@ namespace PluginManager.Database
|
||||
{
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} TEXT";
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public async Task<bool> TableExistsAsync(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
@@ -189,6 +295,11 @@ namespace PluginManager.Database
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public bool TableExists(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
@@ -200,6 +311,13 @@ namespace PluginManager.Database
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task CreateTableAsync(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
@@ -207,6 +325,13 @@ namespace PluginManager.Database
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public void CreateTable(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
@@ -214,6 +339,11 @@ namespace PluginManager.Database
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public async Task<int> ExecuteAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -223,6 +353,11 @@ namespace PluginManager.Database
|
||||
return answer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public int Execute(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -233,6 +368,11 @@ namespace PluginManager.Database
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public async Task<string?> ReadDataAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -250,6 +390,11 @@ namespace PluginManager.Database
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public string? ReadData(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -267,6 +412,11 @@ namespace PluginManager.Database
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public async Task<object[]?> ReadDataArrayAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -284,6 +434,35 @@ namespace PluginManager.Database
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public object[]? ReadDataArray(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <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
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>A list of string arrays representing the values that the query returns</returns>
|
||||
|
||||
public async Task<List<string[]>?> ReadAllRowsAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
@@ -306,22 +485,5 @@ namespace PluginManager.Database
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
public object[]? ReadDataArray(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Discord.Commands;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Interfaces;
|
||||
@@ -37,16 +35,16 @@ public interface DBCommand
|
||||
/// <summary>
|
||||
/// The main body of the command. This is what is executed when user calls the command in Server
|
||||
/// </summary>
|
||||
/// <param name="context">The disocrd Context</param>
|
||||
void ExecuteServer(CmdArgs args)
|
||||
/// <param name="args">The disocrd Context</param>
|
||||
void ExecuteServer(DBCommandExecutingArguments args)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The main body of the command. This is what is executed when user calls the command in DM
|
||||
/// </summary>
|
||||
/// <param name="context">The disocrd Context</param>
|
||||
void ExecuteDM(CmdArgs args)
|
||||
/// <param name="args">The disocrd Context</param>
|
||||
void ExecuteDM(DBCommandExecutingArguments args)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,114 +1,142 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace PluginManager.Loaders;
|
||||
using PluginManager.Interfaces;
|
||||
|
||||
internal class LoaderArgs : EventArgs
|
||||
namespace PluginManager.Loaders
|
||||
{
|
||||
internal string? PluginName { get; init; }
|
||||
internal string? TypeName { get; init; }
|
||||
internal bool IsLoaded { get; init; }
|
||||
internal Exception? Exception { get; init; }
|
||||
internal object? Plugin { get; init; }
|
||||
}
|
||||
|
||||
internal class Loader<T>
|
||||
{
|
||||
internal Loader(string path, string extension)
|
||||
internal class LoaderArgs : EventArgs
|
||||
{
|
||||
this.path = path;
|
||||
this.extension = extension;
|
||||
internal string? PluginName { get; init; }
|
||||
internal string? TypeName { get; init; }
|
||||
internal bool IsLoaded { get; init; }
|
||||
internal Exception? Exception { get; init; }
|
||||
internal object? Plugin { get; init; }
|
||||
}
|
||||
|
||||
|
||||
private string path { get; }
|
||||
private string extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
internal List<T>? Load()
|
||||
internal class Loader
|
||||
{
|
||||
var list = new List<T>();
|
||||
if (!Directory.Exists(path))
|
||||
internal Loader(string path, string extension)
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
return null;
|
||||
this.path = path;
|
||||
this.extension = extension;
|
||||
}
|
||||
|
||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
|
||||
private string path { get; }
|
||||
private string extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
|
||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
if (FileLoaded != null)
|
||||
|
||||
List<DBEvent> events = new();
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
List<DBCommand> commands = new();
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = nameof(T),
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
Directory.CreateDirectory(path);
|
||||
return (null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(T);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
|
||||
list.Clear();
|
||||
foreach (var type in types)
|
||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T),
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
});
|
||||
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.LogLevel.ERROR);
|
||||
continue;
|
||||
}
|
||||
if (FileLoaded != null)
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = null,
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
internal List<T> LoadItems<T>()
|
||||
{
|
||||
Logger.WriteErrFile(ex.ToString());
|
||||
List<T> list = new();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(T);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
|
||||
list.Clear();
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : typeof(T) == typeof(DBEvent) ? "DBEvent" : "DBSlashCommand",
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using PluginManager.Interfaces;
|
||||
|
||||
namespace PluginManager.Loaders
|
||||
{
|
||||
internal class LoaderV2
|
||||
{
|
||||
internal LoaderV2(string path, string extension)
|
||||
{
|
||||
this.path = path;
|
||||
this.extension = extension;
|
||||
}
|
||||
|
||||
|
||||
private string path { get; }
|
||||
private string extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
|
||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||
{
|
||||
|
||||
List<DBEvent> events = new();
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
List<DBCommand> commands = new();
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
return (null, null, null);
|
||||
}
|
||||
|
||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.Message);
|
||||
Logger.WriteLine("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded");
|
||||
|
||||
continue;
|
||||
}
|
||||
if (FileLoaded != null)
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = null,
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||
}
|
||||
|
||||
internal List<T> LoadItems<T>()
|
||||
{
|
||||
List<T> list = new();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(T);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
|
||||
list.Clear();
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : typeof(T) == typeof(DBEvent) ? "DBEvent" : "DBSlashCommand",
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteErrFile(ex.ToString());
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ using Discord.WebSocket;
|
||||
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Online.Updates;
|
||||
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
@@ -66,39 +65,32 @@ public class PluginLoader
|
||||
/// </summary>
|
||||
public static List<DBSlashCommand>? SlashCommands { get; set; }
|
||||
|
||||
public static int PluginsLoaded { get {
|
||||
var count = 0;
|
||||
if (Commands is not null)
|
||||
count += Commands.Count;
|
||||
if (Events is not null)
|
||||
count += Events.Count;
|
||||
if (SlashCommands is not null)
|
||||
count += SlashCommands.Count;
|
||||
return count;
|
||||
}}
|
||||
|
||||
/// <summary>
|
||||
/// The main mathod that is called to load all events
|
||||
/// </summary>
|
||||
public async void LoadPlugins()
|
||||
{
|
||||
//Check for updates in commands
|
||||
foreach (var file in Directory.GetFiles("./Data/Plugins/", $"*.{pluginExtension}",
|
||||
SearchOption.AllDirectories))
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
var name = new FileInfo(file).Name.Split('.')[0];
|
||||
var version = await ServerCom.GetVersionOfPackageFromWeb(name);
|
||||
if (version is null)
|
||||
return;
|
||||
if (Config.Plugins.GetVersion(name) is not null)
|
||||
Config.Plugins.SetVersion(name, version);
|
||||
|
||||
if (await PluginUpdater.CheckForUpdates(name))
|
||||
await PluginUpdater.Download(name);
|
||||
});
|
||||
|
||||
|
||||
//Load all plugins
|
||||
|
||||
Commands = new List<DBCommand>();
|
||||
Events = new List<DBEvent>();
|
||||
SlashCommands = new List<DBSlashCommand>();
|
||||
|
||||
Logger.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
|
||||
Logger.WriteLine("Loading plugins");
|
||||
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.LogLevel.INFO);
|
||||
|
||||
var loader = new LoaderV2("./Data/Plugins", "dll");
|
||||
loader.FileLoaded += (args) => Logger.WriteLogFile($"{args.PluginName} file Loaded");
|
||||
var loader = new Loader("./Data/Plugins", "dll");
|
||||
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.LogLevel.INFO);
|
||||
loader.PluginLoaded += Loader_PluginLoaded;
|
||||
var res = loader.Load();
|
||||
Events = res.Item1;
|
||||
@@ -124,10 +116,7 @@ public class PluginLoader
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine(ex.ToString());
|
||||
Logger.WriteLine("Plugin: " + args.PluginName);
|
||||
Logger.WriteLine("Type: " + args.TypeName);
|
||||
Logger.WriteLine("IsLoaded: " + args.IsLoaded);
|
||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
||||
}
|
||||
break;
|
||||
case "DBSlashCommand":
|
||||
@@ -157,13 +146,13 @@ public class PluginLoader
|
||||
var instance = (DBEvent)Activator.CreateInstance(type);
|
||||
instance.Start(client);
|
||||
PluginLoader.Events.Add(instance);
|
||||
Logger.WriteLine($"[EVENT] Loaded external {type.FullName}!");
|
||||
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.LogLevel.INFO);
|
||||
}
|
||||
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBCommand)Activator.CreateInstance(type);
|
||||
PluginLoader.Commands.Add(instance);
|
||||
Logger.WriteLine($"[CMD] Instance: {type.FullName} loaded !");
|
||||
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
||||
}
|
||||
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
|
||||
{
|
||||
@@ -176,7 +165,7 @@ public class PluginLoader
|
||||
|
||||
await client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
PluginLoader.SlashCommands.Add(instance);
|
||||
Logger.WriteLine($"[SLASH] Instance: {type.FullName} loaded !");
|
||||
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Discord;
|
||||
|
||||
namespace PluginManager
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
|
||||
public static bool isConsole { get; private set; }
|
||||
private static bool isInitialized;
|
||||
|
||||
private static string? logFolder;
|
||||
private static string? errFolder;
|
||||
|
||||
public static void Initialize(bool console)
|
||||
{
|
||||
|
||||
if (isInitialized) throw new Exception("Logger is already initialized");
|
||||
|
||||
if (!Config.Variables.Exists("LogFolder"))
|
||||
Config.Variables.Add("LogFolder", "./Data/Output/Logs/");
|
||||
|
||||
if (!Config.Variables.Exists("ErrorFolder"))
|
||||
Config.Variables.Add("ErrorFolder", "./Data/Output/Errors/");
|
||||
|
||||
isInitialized = true;
|
||||
logFolder = Config.Variables.GetValue("LogFolder");
|
||||
errFolder = Config.Variables.GetValue("ErrorFolder");
|
||||
isConsole = console;
|
||||
}
|
||||
|
||||
|
||||
public delegate void LogEventHandler(string Message);
|
||||
public static event LogEventHandler LogEvent;
|
||||
|
||||
public static void Log(string Message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(Message);
|
||||
}
|
||||
|
||||
public static void Log(string Message, params object[] Args)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(string.Format(Message, Args));
|
||||
}
|
||||
|
||||
public static void Log(IMessage message, bool newLine)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(message.Content);
|
||||
if (newLine)
|
||||
LogEvent?.Invoke("\n");
|
||||
}
|
||||
|
||||
public static void WriteLine(string? message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
if (message is not null)
|
||||
LogEvent?.Invoke(message + '\n');
|
||||
}
|
||||
|
||||
public static void LogError(System.Exception ex)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
string message = "[ERROR]" + ex.Message;
|
||||
LogEvent?.Invoke(message + '\n');
|
||||
}
|
||||
|
||||
public static void LogError(string? message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
if (message is not null)
|
||||
LogEvent?.Invoke("[ERROR]" + message + '\n');
|
||||
}
|
||||
|
||||
|
||||
public static void WriteLine()
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke("\n");
|
||||
}
|
||||
|
||||
public static void Write(string message)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke(message);
|
||||
}
|
||||
|
||||
|
||||
public static void Write<T>(T c)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
LogEvent?.Invoke($"{c}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write logs to file
|
||||
/// </summary>
|
||||
/// <param name="LogMessage">The message to be wrote</param>
|
||||
public static void WriteLogFile(string LogMessage)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
var logsPath = logFolder + $"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Log.txt";
|
||||
Directory.CreateDirectory(logFolder);
|
||||
File.AppendAllTextAsync(logsPath, LogMessage + " \n").Wait();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write error to file
|
||||
/// </summary>
|
||||
/// <param name="ErrMessage">The message to be wrote</param>
|
||||
public static void WriteErrFile(string ErrMessage)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
var errPath = errFolder +
|
||||
$"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Error.txt";
|
||||
Directory.CreateDirectory(errFolder);
|
||||
File.AppendAllText(errPath, ErrMessage + " \n");
|
||||
}
|
||||
|
||||
public static void WriteErrFile(this Exception ex)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||
WriteErrFile(ex.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,22 +14,25 @@ public class PluginsManager
|
||||
/// <summary>
|
||||
/// The Plugin Manager constructor
|
||||
/// </summary>
|
||||
/// <param name="link">The link to the file where all plugins are stored</param>
|
||||
public PluginsManager(string link)
|
||||
/// <param name="plink">The link to the file where all plugins are stored</param>
|
||||
/// <param name="vlink">The link to the file where all plugin versions are stored</param>
|
||||
public PluginsManager(string plink, string vlink)
|
||||
{
|
||||
PluginsLink = link;
|
||||
PluginsLink = plink;
|
||||
VersionsLink = vlink;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The URL of the server
|
||||
/// </summary>
|
||||
public string PluginsLink { get; }
|
||||
public string VersionsLink {get; }
|
||||
|
||||
/// <summary>
|
||||
/// The method to load all plugins
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task ListAvailablePlugins()
|
||||
public async Task<List<string[]>> GetAvailablePlugins()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -40,16 +43,12 @@ public class PluginsManager
|
||||
var op = Functions.GetOperatingSystem();
|
||||
|
||||
var len = lines.Length;
|
||||
string[] titles = { "Name", "Description", "Type", "Version" };
|
||||
data.Add(new[] { "-", "-", "-", "-" });
|
||||
data.Add(titles);
|
||||
data.Add(new[] { "-", "-", "-", "-" });
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
if (lines[i].Length <= 2)
|
||||
continue;
|
||||
var content = lines[i].Split(',');
|
||||
var display = new string[titles.Length];
|
||||
var display = new string[4]; // 4 columns
|
||||
if (op == OperatingSystem.WINDOWS)
|
||||
{
|
||||
if (content[4].Contains("Windows"))
|
||||
@@ -58,7 +57,7 @@ public class PluginsManager
|
||||
display[1] = content[1];
|
||||
display[2] = content[2];
|
||||
display[3] =
|
||||
(await ServerCom.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
(await GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
.ToShortString();
|
||||
data.Add(display);
|
||||
}
|
||||
@@ -71,7 +70,7 @@ public class PluginsManager
|
||||
display[1] = content[1];
|
||||
display[2] = content[2];
|
||||
display[3] =
|
||||
(await ServerCom.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
(await GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
.ToShortString();
|
||||
data.Add(display);
|
||||
}
|
||||
@@ -80,13 +79,33 @@ public class PluginsManager
|
||||
|
||||
data.Add(new[] { "-", "-", "-", "-" });
|
||||
|
||||
Utilities.FormatAndAlignTable(data, TableFormat.CENTER_EACH_COLUMN_BASED);
|
||||
return data;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
||||
Logger.WriteErrFile(exception.ToString());
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<VersionString?> GetVersionOfPackageFromWeb(string pakName)
|
||||
{
|
||||
var data = await ServerCom.ReadTextFromURL(VersionsLink);
|
||||
foreach (var item in data)
|
||||
{
|
||||
if (item.StartsWith("#"))
|
||||
continue;
|
||||
|
||||
string[] split = item.Split(',');
|
||||
if (split[0] == pakName)
|
||||
{
|
||||
Console.WriteLine("Searched for " + pakName + " and found " + split[1] + " as version.\nUsed url: " + VersionsLink);
|
||||
return new VersionString(split[1]);
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -116,8 +135,7 @@ public class PluginsManager
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
||||
Logger.WriteErrFile(exception.ToString());
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
||||
}
|
||||
|
||||
return new string[] { null!, null!, null! };
|
||||
|
||||
@@ -33,7 +33,7 @@ public static class ServerCom
|
||||
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
|
||||
/// <returns></returns>
|
||||
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> progress,
|
||||
IProgress<long>? downloadedBytes = null)
|
||||
IProgress<long>? downloadedBytes)
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
@@ -46,69 +46,8 @@ public static class ServerCom
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Download file from url
|
||||
/// </summary>
|
||||
/// <param name="URL">The url to the file</param>
|
||||
/// <param name="location">The location where to store the downloaded data</param>
|
||||
/// <returns></returns>
|
||||
public static async Task DownloadFileAsync(string URL, string location)
|
||||
public static async Task DownloadFileAsync(string URl, string location, IProgress<float> progress)
|
||||
{
|
||||
var isDownloading = true;
|
||||
float c_progress = 0;
|
||||
|
||||
var pbar = new Utilities.ProgressBar(ProgressBarType.NORMAL) { Max = 100f, NoColor = true };
|
||||
|
||||
IProgress<float> progress = new Progress<float>(percent => { c_progress = percent; });
|
||||
|
||||
|
||||
var updateProgressBarTask = new Task(() =>
|
||||
{
|
||||
while (isDownloading)
|
||||
{
|
||||
pbar.Update(c_progress);
|
||||
if (c_progress == 100f)
|
||||
break;
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
new Thread(updateProgressBarTask.Start).Start();
|
||||
await DownloadFileAsync(URL, location, progress);
|
||||
|
||||
|
||||
c_progress = pbar.Max;
|
||||
pbar.Update(100f);
|
||||
isDownloading = false;
|
||||
}
|
||||
|
||||
public static async Task DownloadFileNoProgressAsync(string URL, string location)
|
||||
{
|
||||
IProgress<float> progress = new Progress<float>();
|
||||
await DownloadFileAsync(URL, location, progress);
|
||||
}
|
||||
|
||||
public static VersionString? GetVersionOfPackage(string pakName)
|
||||
{
|
||||
if (Config.Plugins.GetVersion(pakName) is null)
|
||||
return null;
|
||||
return new VersionString(Config.Plugins.GetVersion(pakName));
|
||||
}
|
||||
|
||||
public static async Task<VersionString?> GetVersionOfPackageFromWeb(string pakName)
|
||||
{
|
||||
var url = "https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/Versions";
|
||||
var data = await ReadTextFromURL(url);
|
||||
foreach (var item in data)
|
||||
{
|
||||
if (item.StartsWith("#"))
|
||||
continue;
|
||||
|
||||
string[] split = item.Split(',');
|
||||
if (split[0] == pakName)
|
||||
return new VersionString(split[1]);
|
||||
}
|
||||
return null;
|
||||
await DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using PluginManager.Items;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Online.Updates;
|
||||
|
||||
public class PluginUpdater
|
||||
{
|
||||
public static async Task<bool> CheckForUpdates(string pakName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var webV = await ServerCom.GetVersionOfPackageFromWeb(pakName);
|
||||
var local = ServerCom.GetVersionOfPackage(pakName);
|
||||
|
||||
if (local is null) return true;
|
||||
if (webV is null) return false;
|
||||
|
||||
if (webV == local) return false;
|
||||
if (webV > local) return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static async Task<List<string>> GetInfo(string pakName)
|
||||
{
|
||||
|
||||
Utilities.WriteColorText("An update was found for &g" + pakName + "&c. Version: &r" +
|
||||
(await ServerCom.GetVersionOfPackageFromWeb(pakName))?.ToShortString() +
|
||||
"&c. Current Version: &y" +
|
||||
ServerCom.GetVersionOfPackage(pakName)?.ToShortString());
|
||||
|
||||
List<string> fileInfo = await ServerCom.ReadTextFromURL("");
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
public static async Task Download(string pakName)
|
||||
{
|
||||
var pakUpdateInfo = await GetInfo(pakName);
|
||||
Logger.Log(string.Join("\n", pakUpdateInfo));
|
||||
await ConsoleCommandsHandler.ExecuteCommad("dwplug " + pakName);
|
||||
}
|
||||
}
|
||||
51
PluginManager/Others/Actions/ActionManager.cs
Normal file
51
PluginManager/Others/Actions/ActionManager.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Actions
|
||||
{
|
||||
public class ActionManager
|
||||
{
|
||||
public List<InternalAction> Actions { get; private set; }
|
||||
|
||||
private bool _isInitialized = false;
|
||||
|
||||
public ActionManager()
|
||||
{
|
||||
if(_isInitialized) return;
|
||||
|
||||
Actions = new List<InternalAction>();
|
||||
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
public bool ActionExists(string name)
|
||||
{
|
||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
||||
return Actions.Any(x => x.Name == name);
|
||||
}
|
||||
|
||||
public void AddAction(InternalAction action)
|
||||
{
|
||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
||||
Actions.Add(action);
|
||||
}
|
||||
|
||||
public void ExecuteAction(string name, string[] args)
|
||||
{
|
||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
||||
var action = Actions.FirstOrDefault(x => x.Name == name);
|
||||
if(action == null) throw new Exception($"Action {name} not found");
|
||||
action.Invoke(args);
|
||||
}
|
||||
|
||||
public async Task ExecuteActionAsync(string name, string[] args)
|
||||
{
|
||||
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
|
||||
var action = Actions.FirstOrDefault(x => x.Name == name);
|
||||
if(action == null) throw new Exception($"Action {name} not found");
|
||||
await action.InvokeAsync(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
PluginManager/Others/Actions/InternalAction.cs
Normal file
40
PluginManager/Others/Actions/InternalAction.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Actions
|
||||
{
|
||||
public class InternalAction
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public Action<string[]> Action { get; init; }
|
||||
|
||||
public InternalAction(string name, Action<string[]> action)
|
||||
{
|
||||
Name = name;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
public InternalAction(string name, Action action)
|
||||
{
|
||||
Name = name;
|
||||
Action = (o) =>
|
||||
{
|
||||
action();
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
public void Invoke(string[] args)
|
||||
{
|
||||
Action(args);
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(string[] args)
|
||||
{
|
||||
await Task.Run(() => Action(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,18 @@ namespace PluginManager.Others
|
||||
public static class ArchiveManager
|
||||
{
|
||||
public static bool isInitialized { get; private set; }
|
||||
private static string archiveFolder;
|
||||
private static string? archiveFolder;
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
|
||||
|
||||
if (!Config.Variables.Exists("ArchiveFolder"))
|
||||
Config.Variables.Add("ArchiveFolder", "./Data/PAKS/");
|
||||
if (!Config.Data.ContainsKey("ArchiveFolder"))
|
||||
Config.Data["ArchiveFolder"] = "./Data/PAKS/";
|
||||
|
||||
archiveFolder = Config.Data["ArchiveFolder"];
|
||||
|
||||
isInitialized = true;
|
||||
archiveFolder = Config.Variables.GetValue("ArchiveFolder");
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
@@ -28,7 +29,7 @@ namespace PluginManager.Others
|
||||
/// <param name="FileName">The file name that is inside the archive or its full path</param>
|
||||
/// <param name="archFile">The archive location from the PAKs folder</param>
|
||||
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
|
||||
public static async Task<string> ReadFromPakAsync(string FileName, string archFile)
|
||||
public static async Task<string?> ReadFromPakAsync(string FileName, string archFile)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||
archFile = archiveFolder + archFile;
|
||||
@@ -37,7 +38,7 @@ namespace PluginManager.Others
|
||||
|
||||
try
|
||||
{
|
||||
string textValue = null;
|
||||
string? textValue = null;
|
||||
using (var fs = new FileStream(archFile, FileMode.Open))
|
||||
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
|
||||
{
|
||||
@@ -55,8 +56,9 @@ namespace PluginManager.Others
|
||||
|
||||
return textValue;
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, "Archive Manager", LogLevel.ERROR); // Write the error to a file
|
||||
await Task.Delay(100);
|
||||
return await ReadFromPakAsync(FileName, archFile);
|
||||
}
|
||||
@@ -77,7 +79,7 @@ namespace PluginManager.Others
|
||||
Directory.CreateDirectory(folder);
|
||||
using (var archive = ZipFile.OpenRead(zip))
|
||||
{
|
||||
if (type == UnzipProgressType.PercentageFromNumberOfFiles)
|
||||
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
|
||||
{
|
||||
var totalZIPFiles = archive.Entries.Count();
|
||||
var currentZIPFile = 0;
|
||||
@@ -93,7 +95,7 @@ namespace PluginManager.Others
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
currentZIPFile++;
|
||||
@@ -102,7 +104,7 @@ namespace PluginManager.Others
|
||||
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
|
||||
}
|
||||
}
|
||||
else if (type == UnzipProgressType.PercentageFromTotalSize)
|
||||
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
|
||||
{
|
||||
ulong zipSize = 0;
|
||||
|
||||
@@ -125,7 +127,7 @@ namespace PluginManager.Others
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
await Task.Delay(10);
|
||||
|
||||
@@ -8,12 +8,20 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others
|
||||
{
|
||||
public class CmdArgs
|
||||
public class DBCommandExecutingArguments
|
||||
{
|
||||
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.cleanContent = cleanContent;
|
||||
this.commandUsed = commandUsed;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,23 +11,10 @@ public enum OperatingSystem
|
||||
UNKNOWN
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A list with all errors
|
||||
/// </summary>
|
||||
public enum Error
|
||||
{
|
||||
UNKNOWN_ERROR,
|
||||
GUILD_NOT_FOUND,
|
||||
STREAM_NOT_FOUND,
|
||||
INVALID_USER,
|
||||
INVALID_CHANNEL,
|
||||
INVALID_PERMISSIONS
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The output log type
|
||||
/// </summary>
|
||||
public enum OutputLogLevel
|
||||
public enum LogLevel
|
||||
{
|
||||
NONE,
|
||||
INFO,
|
||||
@@ -38,15 +25,8 @@ public enum OutputLogLevel
|
||||
|
||||
public enum UnzipProgressType
|
||||
{
|
||||
PercentageFromNumberOfFiles,
|
||||
PercentageFromTotalSize
|
||||
}
|
||||
|
||||
public enum TableFormat
|
||||
{
|
||||
CENTER_EACH_COLUMN_BASED,
|
||||
CENTER_OVERALL_LENGTH,
|
||||
DEFAULT
|
||||
PERCENTAGE_FROM_NUMBER_OF_FILES,
|
||||
PERCENTAGE_FROM_TOTAL_SIZE
|
||||
}
|
||||
|
||||
public enum SaveType
|
||||
@@ -55,8 +35,3 @@ public enum SaveType
|
||||
BACKUP
|
||||
}
|
||||
|
||||
public enum ProgressBarType
|
||||
{
|
||||
NORMAL,
|
||||
NO_END
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
@@ -7,11 +6,6 @@ using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager.Items;
|
||||
|
||||
|
||||
namespace PluginManager.Others;
|
||||
|
||||
/// <summary>
|
||||
@@ -94,6 +88,7 @@ public static class Functions
|
||||
/// <returns></returns>
|
||||
public static async Task<T> ConvertFromJson<T>(string input)
|
||||
{
|
||||
Console.WriteLine(input);
|
||||
Stream text;
|
||||
if (File.Exists(input))
|
||||
text = new MemoryStream(await File.ReadAllBytesAsync(input));
|
||||
|
||||
52
PluginManager/Others/Logger/DBLogger.cs
Normal file
52
PluginManager/Others/Logger/DBLogger.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Logger
|
||||
{
|
||||
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 event LogHandler LogEvent;
|
||||
|
||||
private string _logFolder;
|
||||
private string _errFolder;
|
||||
|
||||
public DBLogger()
|
||||
{
|
||||
_logFolder = Config.Data["LogFolder"];
|
||||
_errFolder = Config.Data["ErrorFolder"];
|
||||
}
|
||||
|
||||
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
|
||||
|
||||
public void Log(LogMessage message)
|
||||
{
|
||||
if(LogEvent is not null)
|
||||
LogEvent?.Invoke(message.Message, message.Type);
|
||||
|
||||
if (message.Type != LogLevel.NONE)
|
||||
LogHistory.Add(message);
|
||||
else
|
||||
ErrorHistory.Add(message);
|
||||
}
|
||||
|
||||
public void Log(string message, object sender, LogLevel type = LogLevel.NONE) => Log(message, sender.GetType().Name, type);
|
||||
|
||||
public async void SaveToFile()
|
||||
{
|
||||
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", LogHistory);
|
||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", ErrorHistory);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
PluginManager/Others/Logger/LogMessage.cs
Normal file
47
PluginManager/Others/Logger/LogMessage.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Logger
|
||||
{
|
||||
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)
|
||||
{
|
||||
Message = message;
|
||||
Type = type;
|
||||
Time = DateTime.Now.ToString("HH:mm:ss");
|
||||
}
|
||||
|
||||
public LogMessage(string message, LogLevel type, string sender) : this(message, type)
|
||||
{
|
||||
Sender = sender;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Time}] {Message}";
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage(string message)
|
||||
{
|
||||
return new LogMessage(message, LogLevel.INFO);
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage((string message, LogLevel type) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type);
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage((string message, LogLevel type, string sender) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type, tuple.sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<DebugType>none</DebugType>
|
||||
<DebugSymbols>false</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="BlankWindow1.xaml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.8.1" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
|
||||
<PackageReference Include="Terminal.Gui" Version="1.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<DebugType>none</DebugType>
|
||||
<DebugSymbols>false</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="BlankWindow1.xaml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.8.1" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.117" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,17 +0,0 @@
|
||||
using PluginManager.Database;
|
||||
|
||||
namespace PluginManager
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
|
||||
public static class Variables
|
||||
{
|
||||
public static string WebsiteURL = "https://wizzy69.github.io/SethDiscordBot";
|
||||
public static string UpdaterURL = "https://github.com/Wizzy69/installer/releases/download/release-1-discordbot/Updater.zip";
|
||||
}
|
||||
|
||||
public static SqlDatabase sqlDatabase;
|
||||
}
|
||||
|
||||
}
|
||||
29
README.md
29
README.md
@@ -19,12 +19,12 @@ Plugin Types:
|
||||
|
||||
### How to create a plugin
|
||||
|
||||
First of all, Create a new project (class library) in Visual Studio.
|
||||
Then import the PluginManager.dll as project to your project.
|
||||
First of all, create a new project (class library) in Visual Studio.
|
||||
Then import the PluginManager as reference to your project.
|
||||
|
||||
## 1. Commands
|
||||
|
||||
Commands are loaded when all plugins are loaded into memory. When an user executes the command, only then the Execute function is called.
|
||||
Commands are loaded when all plugins are loaded into memory. The Execute method is called whenever any user (that respects the `requireAdmin` propery) calls the command using the bot prefix and the `Command`.
|
||||
Commands are plugins that allow users to interact with them.
|
||||
Here is an example:
|
||||
```cs
|
||||
@@ -41,14 +41,15 @@ internal class LevelCommand : DBCommand
|
||||
|
||||
public List<string> Aliases => new() { "lvl" };
|
||||
|
||||
public string Description => "Display tour current level";
|
||||
public string Description => "Display your current level";
|
||||
|
||||
public string Usage => "level";
|
||||
|
||||
public bool requireAdmin => false;
|
||||
|
||||
public async void ExecuteServer(SocketCommandContext context)
|
||||
public async void ExecuteServer(CmdArgs context)
|
||||
{
|
||||
//Variables.database is a sql connection that is defined in an auxiliary file in the same napespace as this class
|
||||
object[] user = await Variables.database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID='{context.Message.Author.Id}'");
|
||||
if (user is null)
|
||||
{
|
||||
@@ -69,6 +70,11 @@ internal class LevelCommand : DBCommand
|
||||
builder.WithAuthor(context.Message.Author.Mention);
|
||||
await context.Channel.SendMessageAsync(embed: builder.Build());
|
||||
}
|
||||
|
||||
//Optional method (tell the bot what should it do if the command is executed from a DM channel)
|
||||
//public async void ExecuteDM(CmdArgs context) {
|
||||
//
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +91,7 @@ internal class LevelCommand : DBCommand
|
||||
- context - the command context
|
||||
|
||||
From here on, start coding. When your plugin is done, build it as any DLL project then add it to the following path
|
||||
`{bot_executable}/Data/Plugins/<optional subfolder>/[your dll name].dll`
|
||||
`{bot_executable}/Data/Plugins/<optional subfolder>/[plugin name].dll`
|
||||
Then, reload bot and execute command `lp` in bot's console. The plugin should be loaded into memory or an error is thrown if not. If an error is thrown, then
|
||||
there is something wrong in your command's code.
|
||||
|
||||
@@ -107,8 +113,6 @@ public class OnUserJoin : DBEvent
|
||||
|
||||
public async void Start(Discord.WebSocket.DiscordSocketClient client)
|
||||
{
|
||||
Console.WriteLine($"Hello World from {name}");
|
||||
|
||||
client.UserJoined += async (user) => {
|
||||
await (await user.CreateDMChannelAsync()).SendMessageAsync("Welcome to server !");
|
||||
};
|
||||
@@ -148,7 +152,7 @@ namespace SlashCommands
|
||||
public List<SlashCommandOptionBuilder> Options => new List<SlashCommandOptionBuilder>()
|
||||
{
|
||||
new SlashCommandOptionBuilder() {Name = "min-value", Description = "Minimum value", IsRequired=true, Type = ApplicationCommandOptionType.Integer, MinValue = 0, MaxValue = int.MaxValue-1},
|
||||
new SlashCommandOptionBuilder() {Name="max-value", Description = "Maximum value", IsRequired=true, Type=ApplicationCommandOptionType.Integer,MinValue = 0, MaxValue = int.MaxValue-1}
|
||||
new SlashCommandOptionBuilder() {Name = "max-value", Description = "Maximum value", IsRequired=true, Type=ApplicationCommandOptionType.Integer,MinValue = 0, MaxValue = int.MaxValue-1}
|
||||
};
|
||||
|
||||
public async void ExecuteServer(SocketSlashCommand command)
|
||||
@@ -180,3 +184,10 @@ namespace SlashCommands
|
||||
- context - the command context
|
||||
- ExecuteDM() - this function will be called if the command is invoked in a DM channel (optional)
|
||||
- context - the command context
|
||||
|
||||
|
||||
## Note:
|
||||
You can create multiple commands, events and slash commands into one single plugin (class library). The PluginManager will detect the classes and load them individualy. If there are more commands (normal commands, events or slash commands) into a single project (class library) they can use the same resources (a class for example) that is contained within the plugin.
|
||||
|
||||
|
||||
> Updated: 7.04.2023
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.1.32421.90
|
||||
@@ -7,16 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicLibrary", "..\DiscordBotItems\Plugins\MusicLibrary\MusicLibrary.csproj", "{878DFE01-4596-4EBC-9651-0679598CE794}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlashCommands", "..\DiscordBotItems\Plugins\SlashCommands\SlashCommands.csproj", "{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LevelingSystem", "..\DiscordBotItems\Plugins\LevelingSystem\LevelingSystem.csproj", "{0138F343-BBB9-4D5F-B499-D9C2978BE9AA}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Roles", "..\DiscordBotItems\Roles\Roles.csproj", "{0900B4CB-B531-4A8D-98D8-E709A7C2E098}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBEconomy", "..\DiscordBotItems\Plugins\DBEconomy\DBEconomy.csproj", "{0321365B-4ADC-4B1D-BD98-F573D36E83B2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -31,30 +21,12 @@ Global
|
||||
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{878DFE01-4596-4EBC-9651-0679598CE794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{878DFE01-4596-4EBC-9651-0679598CE794}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{878DFE01-4596-4EBC-9651-0679598CE794}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{878DFE01-4596-4EBC-9651-0679598CE794}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C2D73BE8-997B-4A4A-8EA5-989BE33EE1DD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0138F343-BBB9-4D5F-B499-D9C2978BE9AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0138F343-BBB9-4D5F-B499-D9C2978BE9AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0138F343-BBB9-4D5F-B499-D9C2978BE9AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0138F343-BBB9-4D5F-B499-D9C2978BE9AA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0900B4CB-B531-4A8D-98D8-E709A7C2E098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0900B4CB-B531-4A8D-98D8-E709A7C2E098}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0900B4CB-B531-4A8D-98D8-E709A7C2E098}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0900B4CB-B531-4A8D-98D8-E709A7C2E098}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0321365B-4ADC-4B1D-BD98-F573D36E83B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0321365B-4ADC-4B1D-BD98-F573D36E83B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0321365B-4ADC-4B1D-BD98-F573D36E83B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0321365B-4ADC-4B1D-BD98-F573D36E83B2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF}
|
||||
EndGlobalSection
|
||||
|
||||
36
builder.sh
Executable file
36
builder.sh
Executable file
@@ -0,0 +1,36 @@
|
||||
# All files in this directory will be copied to the root of the container
|
||||
|
||||
echo "Building..."
|
||||
|
||||
echo "Building linux-x64 not self-contained"
|
||||
dotnet publish -r linux-x64 -p:PublishSingleFile=false --self-contained false -c Release -o ./publish/linux-x64
|
||||
|
||||
echo "Building win-x64 not self-contained"
|
||||
dotnet publish -r win-x64 -p:PublishSingleFile=false --self-contained false -c Release -o ./publish/win-x64
|
||||
|
||||
echo "Building osx-x64 not self-contained"
|
||||
dotnet publish -r osx-x64 -p:PublishSingleFile=false --self-contained false -c Release -o ./publish/osx-x64
|
||||
|
||||
#One file per platform
|
||||
echo "Building linux-x64 self-contained"
|
||||
dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./publish/linux-x64-selfcontained
|
||||
|
||||
echo "Building win-x64 self-contained"
|
||||
dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./publish/win-x64-selfcontained
|
||||
|
||||
echo "Building osx-x64 self-contained"
|
||||
dotnet publish -r osx-x64 -p:PublishSingleFile=true --self-contained true -c Release -o ./publish/osx-x64-selfcontained
|
||||
|
||||
echo "Zipping..."
|
||||
mkdir ./publish/zip
|
||||
|
||||
|
||||
zip -r ./publish/zip/linux-x64.zip ./publish/linux-x64
|
||||
zip -r ./publish/zip/win-x64.zip ./publish/win-x64
|
||||
zip -r ./publish/zip/osx-x64.zip ./publish/osx-x64
|
||||
|
||||
zip -r ./publish/zip/linux-x64-selfcontained.zip ./publish/linux-x64-selfcontained
|
||||
zip -r ./publish/zip/win-x64-selfcontained.zip ./publish/win-x64-selfcontained
|
||||
zip -r ./publish/zip/osx-x64-selfcontained.zip ./publish/osx-x64-selfcontained
|
||||
|
||||
echo "Done!"
|
||||
Reference in New Issue
Block a user