Renamed PluginManager to DiscordBotCore.

Revamped the Logger
This commit is contained in:
2024-05-12 20:10:52 +03:00
parent 413d465d7f
commit 17147d920d
66 changed files with 529 additions and 556 deletions

View File

@@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Interfaces;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
namespace DiscordBot.Bot.Actions;

View File

@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
namespace DiscordBot.Bot.Actions;
@@ -24,8 +24,8 @@ public class Exit: ICommandAction
{
if (args is null || args.Length == 0)
{
Config.Logger.Log("Exiting...", typeof(ICommandAction), LogType.WARNING);
await Config.AppSettings.SaveToFile();
Application.CurrentApplication.Logger.Log("Exiting...", typeof(ICommandAction), LogType.WARNING);
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
Environment.Exit(0);
}
else
@@ -40,7 +40,7 @@ public class Exit: ICommandAction
case "-f":
case "force":
Config.Logger.Log("Exiting (FORCE)...", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", typeof(ICommandAction), LogType.WARNING);
Environment.Exit(0);
break;

View File

@@ -6,11 +6,12 @@ using System.Threading.Tasks;
using DiscordBot.Utilities;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Loaders;
using PluginManager.Online;
using PluginManager.Others;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Loaders;
using DiscordBotCore.Online;
using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
using Spectre.Console;
@@ -18,7 +19,7 @@ namespace DiscordBot.Bot.Actions.Extra;
internal static class PluginMethods
{
internal static async Task List(PluginsManager manager)
internal static async Task List(PluginManager manager)
{
var data = await ConsoleUtilities.ExecuteWithProgressBar(manager.GetPluginsList(), "Reading remote database");
@@ -38,11 +39,11 @@ internal static class PluginMethods
internal static async Task RefreshPlugins(bool quiet)
{
await Program.internalActionManager.Execute("plugin", "load", quiet ? "-q" : string.Empty);
await Program.internalActionManager.Refresh();
await Application.CurrentApplication.InternalActionManager.Execute("plugin", "load", quiet ? "-q" : string.Empty);
await Application.CurrentApplication.InternalActionManager.Refresh();
}
internal static async Task DownloadPlugin(PluginsManager manager, string pluginName)
internal static async Task DownloadPlugin(PluginManager manager, string pluginName)
{
var pluginData = await manager.GetPluginDataByName(pluginName);
if (pluginData is null)
@@ -66,7 +67,7 @@ internal static class PluginMethods
IProgress<float> progress = new Progress<float>(p => { downloadTask.Value = p; });
await ServerCom.DownloadFileAsync(pluginLink, $"{Config.AppSettings["PluginFolder"]}/{pluginName}.dll", progress);
await ServerCom.DownloadFileAsync(pluginLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginName}.dll", progress);
downloadTask.Increment(100);
@@ -76,7 +77,7 @@ internal static class PluginMethods
if (!pluginData.HasDependencies)
{
await manager.AppendPluginToDatabase(new PluginManager.Plugin.PluginInfo(pluginName, pluginData.Version, []));
await manager.AppendPluginToDatabase(new PluginInfo(pluginName, pluginData.Version, []));
Console.WriteLine("Finished installing " + pluginName + " successfully");
await RefreshPlugins(false);
return;
@@ -108,8 +109,8 @@ internal static class PluginMethods
int maxParallelDownloads = 5;
if (Config.AppSettings.ContainsKey("MaxParallelDownloads"))
maxParallelDownloads = int.Parse(Config.AppSettings["MaxParallelDownloads"]);
if (Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("MaxParallelDownloads"))
maxParallelDownloads = int.Parse(Application.CurrentApplication.ApplicationEnvironmentVariables["MaxParallelDownloads"]);
var options = new ParallelOptions()
{
@@ -129,13 +130,13 @@ internal static class PluginMethods
}
);
await manager.AppendPluginToDatabase(new PluginManager.Plugin.PluginInfo(pluginName, pluginData.Version, pluginData.Dependencies.Select(sep => sep.DownloadLocation).ToList()));
await manager.AppendPluginToDatabase(new PluginInfo(pluginName, pluginData.Version, pluginData.Dependencies.Select(sep => sep.DownloadLocation).ToList()));
await RefreshPlugins(false);
}
internal static async Task<bool> LoadPlugins(string[] args)
{
var loader = new PluginLoader(Config.DiscordBot.Client);
var loader = new PluginLoader(Application.CurrentApplication.DiscordBotClient.Client);
if (args.Length == 2 && args[1] == "-q")
{
await loader.LoadPlugins();
@@ -147,14 +148,14 @@ internal static class PluginMethods
{
if (data.IsSuccess)
{
Config.Logger.Log("Successfully loaded command : " + data.PluginName, typeof(ICommandAction),
Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
}
else
{
Config.Logger.Log("Failed to load command : " + data.PluginName + " because " + data.ErrorMessage,
Application.CurrentApplication.Logger.Log("Failed to load command : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR
);
}
@@ -165,13 +166,13 @@ internal static class PluginMethods
{
if (data.IsSuccess)
{
Config.Logger.Log("Successfully loaded event : " + data.PluginName, typeof(ICommandAction),
Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
}
else
{
Config.Logger.Log("Failed to load event : " + data.PluginName + " because " + data.ErrorMessage,
Application.CurrentApplication.Logger.Log("Failed to load event : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR
);
}
@@ -183,13 +184,13 @@ internal static class PluginMethods
{
if (data.IsSuccess)
{
Config.Logger.Log("Successfully loaded slash command : " + data.PluginName, typeof(ICommandAction),
Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
}
else
{
Config.Logger.Log("Failed to load slash command : " + data.PluginName + " because " + data.ErrorMessage,
Application.CurrentApplication.Logger.Log("Failed to load slash command : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR
);
}

View File

@@ -1,6 +1,5 @@
using System.Linq;
using PluginManager;
using PluginManager.Loaders;
using DiscordBotCore;
namespace DiscordBot.Bot.Actions.Extra;
@@ -13,21 +12,21 @@ internal static class SettingsConfigExtra
if (value is null) return;
if (!Config.AppSettings.ContainsKey(key))
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey(key))
return;
Config.AppSettings[key] = string.Join(' ', value);
// Config.AppSettings.SaveToFile().Wait();
Application.CurrentApplication.ApplicationEnvironmentVariables[key] = string.Join(' ', value);
// Config.Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile().Wait();
}
internal static void RemoveSettings(string key)
{
if (key is null) return;
if (!Config.AppSettings.ContainsKey(key))
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey(key))
return;
Config.AppSettings.Remove(key);
Application.CurrentApplication.ApplicationEnvironmentVariables.Remove(key);
}
internal static void AddSettings(string key, params string[] value)
@@ -36,10 +35,10 @@ internal static class SettingsConfigExtra
if (value is null) return;
if (Config.AppSettings.ContainsKey(key))
if (Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey(key))
return;
Config.AppSettings.Add(key, string.Join(' ', value));
// Config.AppSettings.SaveToFile().Wait();
Application.CurrentApplication.ApplicationEnvironmentVariables.Add(key, string.Join(' ', value));
// Config.Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile().Wait();
}
}

View File

@@ -3,9 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiscordBot.Utilities;
using PluginManager.Interfaces;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
using Spectre.Console;
namespace DiscordBot.Bot.Actions;
@@ -30,7 +32,7 @@ public class Help: ICommandAction
tableData.Columns = ["Command", "Usage", "Description", "Options"];
foreach (var a in Program.internalActionManager.Actions)
foreach (var a in Application.CurrentApplication.InternalActionManager.Actions)
{
Markup actionName = new Markup($"[bold]{a.Key}[/]");
Markup usage = new Markup($"[italic]{a.Value.Usage}[/]");
@@ -68,13 +70,13 @@ public class Help: ICommandAction
return;
}
if (!Program.internalActionManager.Actions.ContainsKey(args[0]))
if (!Application.CurrentApplication.InternalActionManager.Actions.ContainsKey(args[0]))
{
Console.WriteLine("Command not found");
return;
}
var action = Program.internalActionManager.Actions[args[0]];
var action = Application.CurrentApplication.InternalActionManager.Actions[args[0]];
tableData.Columns = ["Command", "Usage", "Description"];
tableData.AddRow([action.ActionName, action.Usage, action.Description]);

View File

@@ -2,10 +2,10 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DiscordBot.Bot.Actions.Extra;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
namespace DiscordBot.Bot.Actions;
@@ -50,24 +50,24 @@ public class Plugin: ICommandAction
case "uninstall":
string plugName = string.Join(' ', args, 1, args.Length-1);
bool result = await Config.PluginsManager.MarkPluginToUninstall(plugName);
bool result = await Application.CurrentApplication.PluginManager.MarkPluginToUninstall(plugName);
if(result)
Console.WriteLine($"Marked to uninstall plugin {plugName}. Please restart the bot");
break;
case "list":
await PluginMethods.List(Config.PluginsManager);
await PluginMethods.List(Application.CurrentApplication.PluginManager);
break;
case "load":
if (pluginsLoaded)
{
Config.Logger.Log("Plugins already loaded", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("Plugins already loaded", typeof(ICommandAction), LogType.WARNING);
break;
}
if (Config.DiscordBot is null)
if (Application.CurrentApplication.DiscordBotClient is null)
{
Config.Logger.Log("DiscordBot is null", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("DiscordBot is null", typeof(ICommandAction), LogType.WARNING);
break;
}
@@ -88,7 +88,7 @@ public class Plugin: ICommandAction
}
}
await PluginMethods.DownloadPlugin(Config.PluginsManager, pluginName);
await PluginMethods.DownloadPlugin(Application.CurrentApplication.PluginManager, pluginName);
break;
}
}

View File

@@ -2,10 +2,10 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DiscordBot.Bot.Actions.Extra;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
namespace DiscordBot.Bot.Actions;
@@ -26,7 +26,7 @@ public class SettingsConfig: ICommandAction
{
if (args is null)
{
foreach (var settings in Config.AppSettings)
foreach (var settings in Application.CurrentApplication.ApplicationEnvironmentVariables)
Console.WriteLine(settings.Key + ": " + settings.Value);
return Task.CompletedTask;

View File

@@ -1,9 +1,9 @@
using System.Collections.Generic;
using Discord;
using PluginManager;
using PluginManager.Interfaces;
using PluginManager.Loaders;
using PluginManager.Others;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Loaders;
using DiscordBotCore.Others;
namespace DiscordBot.Bot.Commands;
@@ -79,7 +79,7 @@ internal class Help: DBCommand
);
if (cmd == null) return null;
embedBuilder.AddField("Usage", Config.AppSettings["prefix"] + cmd.Usage);
embedBuilder.AddField("Usage", Application.CurrentApplication.ApplicationEnvironmentVariables["prefix"] + cmd.Usage);
embedBuilder.AddField("Description", cmd.Description);
if (cmd.Aliases is null)
return embedBuilder;

View File

@@ -2,9 +2,9 @@ using System.Collections.Generic;
using System.Linq;
using Discord;
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Loaders;
using PluginManager.Others;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Loaders;
using DiscordBotCore.Others;
namespace DiscordBot.Bot.Commands.SlashCommands;

View File

@@ -7,15 +7,15 @@
<StartupObject />
<SignAssembly>False</SignAssembly>
<IsPublishable>True</IsPublishable>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<PublishAot>False</PublishAot>
<FileVersion>1.0.2.0</FileVersion>
<FileVersion>1.0.4.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>none</DebugType>
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Data\**" />
@@ -38,6 +38,6 @@
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
<ProjectReference Include="..\DiscordBotCore\DiscordBotCore.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,5 +1,5 @@
using System;
using PluginManager;
using DiscordBotCore;
using System.Threading.Tasks;
using Spectre.Console;
@@ -18,22 +18,22 @@ public static class Installer
Environment.Exit(-20);
}
Config.AppSettings.Add(key, value);
Application.CurrentApplication.ApplicationEnvironmentVariables.Add(key, value);
}
public static async Task GenerateStartupConfig()
{
if(!Config.AppSettings.ContainsKey("token"))
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token"))
await AskForConfig("token", "Token:");
if(!Config.AppSettings.ContainsKey("prefix"))
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix"))
await AskForConfig("prefix", "Prefix:");
if(!Config.AppSettings.ContainsKey("ServerID"))
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID"))
await AskForConfig("ServerID", "Server ID:");
await Config.AppSettings.SaveToFile();
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
Config.Logger.Log("Config Saved", typeof(Installer));
Application.CurrentApplication.Logger.Log("Config Saved", typeof(Installer));
}
}

View File

@@ -3,19 +3,19 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DiscordBot.Utilities;
using PluginManager.Bot;
using PluginManager.Others;
using PluginManager.Others.Actions;
using DiscordBotCore;
using DiscordBotCore.Bot;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
using DiscordBotCore.Updater.Application;
using Spectre.Console;
using static PluginManager.Config;
namespace DiscordBot;
public class Program
{
public static InternalActionManager internalActionManager;
/// <summary>
/// The main entry point for the application.
/// </summary>
@@ -23,9 +23,14 @@ public class Program
{
PreLoadComponents(args).Wait();
if (!AppSettings.ContainsKey("ServerID") || !AppSettings.ContainsKey("token") || !AppSettings.ContainsKey("prefix"))
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix")
)
Installer.GenerateStartupConfig().Wait();
HandleInput().Wait();
}
@@ -34,9 +39,7 @@ public class Program
/// </summary>
private static void NoGUI()
{
internalActionManager.Initialize().Wait();
internalActionManager.Execute("plugin", "load").Wait();
internalActionManager.Refresh().Wait();
Application.CurrentApplication.InternalActionManager.Execute("plugin", "load").Wait();
while (true)
{
@@ -47,7 +50,7 @@ public class Program
if (args.Length == 0)
args = null;
internalActionManager.Execute(command, args).Wait(); // Execute the command
Application.CurrentApplication.InternalActionManager.Execute(command, args).Wait();
}
}
@@ -58,7 +61,7 @@ public class Program
private static async Task StartNoGui()
{
AnsiConsole.MarkupLine($"[yellow]Running on version: {AppSettings["Version"]}[/]");
AnsiConsole.MarkupLine($"[yellow]Running on version: {Application.CurrentApplication.ApplicationEnvironmentVariables["Version"]}[/]");
AnsiConsole.MarkupLine("[yellow]Git SethBot: https://github.com/andreitdr/SethDiscordBot [/]");
AnsiConsole.MarkupLine("[yellow]Git Plugins: https://github.com/andreitdr/SethPlugins [/]");
@@ -69,14 +72,14 @@ public class Program
try
{
var token = AppSettings["token"];
var prefix = AppSettings["prefix"];
var token = Application.CurrentApplication.ApplicationEnvironmentVariables["token"];
var prefix = Application.CurrentApplication.ApplicationEnvironmentVariables["prefix"];
var discordbooter = new Boot(token, prefix);
await discordbooter.Awake();
}
catch (Exception ex)
{
Logger.Log(ex.ToString(), typeof(Program), LogType.CRITICAL);
Application.CurrentApplication.Logger.Log(ex.ToString(), typeof(Program), LogType.CRITICAL);
}
}
@@ -88,14 +91,13 @@ public class Program
await StartNoGui();
try
{
internalActionManager = new InternalActionManager(AppSettings["PluginFolder"], "*.dll");
NoGUI();
}
catch (IOException ex)
{
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
{
Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" +
Application.CurrentApplication.Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" +
"There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
typeof(Program), LogType.ERROR
);
@@ -105,14 +107,14 @@ public class Program
private static async Task PreLoadComponents(string[] args)
{
await Initialize();
await Application.CreateApplication();
AppSettings["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Application.CurrentApplication.ApplicationEnvironmentVariables["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();
PluginManager.Updater.Application.AppUpdater updater = new();
AppUpdater updater = new();
var update = await updater.CheckForUpdates();
if (update != PluginManager.Updater.Application.Update.None)
if (update != Update.None)
{
Console.WriteLine($"New update available: {update.UpdateVersion}");
Console.WriteLine($"Download link: {update.UpdateUrl}");
@@ -122,15 +124,16 @@ public class Program
await Task.Delay(5000);
}
Logger.OnLog += (sender, logMessage) =>
Application.CurrentApplication.Logger.OnFormattedLog += async (sender, logMessage) =>
{
await File.AppendAllTextAsync(Application.CurrentApplication.LogFile, logMessage.Message + "\n");
var messageColor = logMessage.Type switch
{
LogType.INFO => "[green]",
LogType.WARNING => "[yellow]",
LogType.ERROR => "[red]",
LogType.INFO => "[green]",
LogType.WARNING => "[yellow]",
LogType.ERROR => "[red]",
LogType.CRITICAL => "[red]",
_ => "[white]"
_ => "[white]"
};
if (logMessage.Message.Contains('['))
@@ -139,7 +142,8 @@ public class Program
return;
}
AnsiConsole.MarkupLine($"{messageColor}{logMessage.ThrowTime} {logMessage.Message} [/]");
string messageToPrint = $"{messageColor}{logMessage.Message}[/]";
AnsiConsole.MarkupLine(messageToPrint);
};
}
}

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using PluginManager.Others;
using DiscordBotCore.Others;
using Spectre.Console;
using Spectre.Console.Rendering;