diff --git a/DiscordBot/Bot/Actions/Clear.cs b/DiscordBot/Bot/Actions/Clear.cs
index dfc8188..f24bc4a 100644
--- a/DiscordBot/Bot/Actions/Clear.cs
+++ b/DiscordBot/Bot/Actions/Clear.cs
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
-using System.Linq;
+
using System.Threading.Tasks;
using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
diff --git a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs
index 936f387..c66609e 100644
--- a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs
+++ b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using DiscordBot.Utilities;
using DiscordBotCore;
-using DiscordBotCore.Interfaces.PluginManager;
using DiscordBotCore.Loaders;
using DiscordBotCore.Online;
using DiscordBotCore.Others;
@@ -22,8 +20,6 @@ internal static class PluginMethods
internal static async Task List()
{
-
-
Console.WriteLine($"Fetching plugin list from branch {Application.CurrentApplication.PluginManager.Branch} ...");
var data = await ConsoleUtilities.ExecuteWithProgressBar(Application.CurrentApplication.PluginManager.GetPluginsList(), "Reading remote database");
diff --git a/DiscordBot/Bot/Actions/Help.cs b/DiscordBot/Bot/Actions/Help.cs
index 07c92e3..8d25716 100644
--- a/DiscordBot/Bot/Actions/Help.cs
+++ b/DiscordBot/Bot/Actions/Help.cs
@@ -9,7 +9,6 @@ using DiscordBotCore.Interfaces;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
using Spectre.Console;
-using Spectre.Console.Rendering;
namespace DiscordBot.Bot.Actions;
diff --git a/DiscordBot/Bot/Actions/Module.cs b/DiscordBot/Bot/Actions/Module.cs
index ec2d688..f054c0c 100644
--- a/DiscordBot/Bot/Actions/Module.cs
+++ b/DiscordBot/Bot/Actions/Module.cs
@@ -1,10 +1,7 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.Threading.Tasks;
using DiscordBotCore;
using DiscordBotCore.Interfaces;
-using DiscordBotCore.Interfaces.Modules;
-using DiscordBotCore.Modules;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
@@ -44,7 +41,7 @@ namespace DiscordBot.Bot.Actions
private void ListLoadedModules()
{
- var modules = DiscordBotCore.Application.CurrentApplication.GetLoadedCoreModules();
+ var modules = Application.CurrentApplication.GetLoadedCoreModules();
foreach (var module in modules)
{
Application.Logger.Log("Module: " + module.Key.ModuleName, this, LogType.Info);
diff --git a/DiscordBot/Entry.cs b/DiscordBot/Entry.cs
index 8c139f4..e0cdbaa 100644
--- a/DiscordBot/Entry.cs
+++ b/DiscordBot/Entry.cs
@@ -39,12 +39,6 @@ public static class Entry
}
Directory.Delete("temp");
- }),
-
- new StartupAction("--module-install", (args) => {
- ModuleDownloader moduleDownloader = new ModuleDownloader(args[0]);
-
- ConsoleUtilities.ExecuteTaskWithBuiltInProgress(moduleDownloader.DownloadModule, "Downloading logger module").Wait();
})
];
diff --git a/DiscordBotCore/Application.cs b/DiscordBotCore/Application.cs
index 897a3d7..e746e00 100644
--- a/DiscordBotCore/Application.cs
+++ b/DiscordBotCore/Application.cs
@@ -1,20 +1,23 @@
-using DiscordBotCore.Interfaces.PluginManager;
-using DiscordBotCore.Online;
-using DiscordBotCore.Others;
-using DiscordBotCore.Others.Actions;
-using DiscordBotCore.Plugin;
-
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
+using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
-using DiscordBotCore.Others.Exceptions;
-using DiscordBotCore.Modules;
-using DiscordBotCore.Interfaces.Modules;
+
+using DiscordBotCore.Online;
using DiscordBotCore.Online.Helpers;
+
+using DiscordBotCore.Others;
+using DiscordBotCore.Others.Actions;
+using DiscordBotCore.Others.Exceptions;
using DiscordBotCore.Others.Settings;
+using DiscordBotCore.Modules;
+using DiscordBotCore.Plugin;
+
+using DiscordBotCore.Interfaces.PluginManager;
+using DiscordBotCore.Interfaces.Modules;
namespace DiscordBotCore
{
@@ -23,6 +26,9 @@ namespace DiscordBotCore
///
public sealed class Application
{
+ ///
+ /// Defines the current application. This is a singleton class
+ ///
public static Application CurrentApplication { get; private set; } = null!;
private static readonly string _ConfigFile = "./Data/Resources/config.json";
@@ -34,13 +40,13 @@ namespace DiscordBotCore
public List ServerIDs => ApplicationEnvironmentVariables.GetList("ServerID", new List());
public string PluginDatabase => ApplicationEnvironmentVariables.Get("PluginDatabase", _PluginsDatabaseFile);
-
- private ModuleManager _ModuleManager;
-
- public CustomSettingsDictionary ApplicationEnvironmentVariables { get; private set; }
- public InternalActionManager InternalActionManager { get; private set; }
- public IPluginManager PluginManager { get; private set; }
- public Bot.App DiscordBotClient { get; internal set; }
+
+ private ModuleManager _ModuleManager = null!;
+
+ public CustomSettingsDictionary ApplicationEnvironmentVariables { get; private set; } = null!;
+ public InternalActionManager InternalActionManager { get; private set; } = null!;
+ public IPluginManager PluginManager { get; private set; } = null!;
+ public Bot.App DiscordBotClient { get; internal set; } = null!;
public static async Task CreateApplication()
{
@@ -50,12 +56,12 @@ namespace DiscordBotCore
Console.WriteLine("No internet connection detected. Exiting ...");
Environment.Exit(0);
}
-
+
if (CurrentApplication is not null)
return;
CurrentApplication = new Application();
-
+
Directory.CreateDirectory(_ResourcesFolder);
Directory.CreateDirectory(_PluginsFolder);
Directory.CreateDirectory(_LogsFolder);
@@ -65,18 +71,19 @@ namespace DiscordBotCore
CurrentApplication.ApplicationEnvironmentVariables.Add("PluginFolder", _PluginsFolder);
CurrentApplication.ApplicationEnvironmentVariables.Add("ResourceFolder", _ResourcesFolder);
CurrentApplication.ApplicationEnvironmentVariables.Add("LogsFolder", _LogsFolder);
-
-
+
CurrentApplication._ModuleManager = new ModuleManager();
await CurrentApplication._ModuleManager.LoadModules();
-
+ var requirements = await CurrentApplication._ModuleManager.CheckRequiredModules();
+ await CurrentApplication._ModuleManager.SolveRequirementIssues(requirements);
+
if (!File.Exists(_PluginsDatabaseFile))
{
List plugins = new();
await JsonManager.SaveToJsonFile(_PluginsDatabaseFile, plugins);
}
-
+
#if DEBUG
CurrentApplication.PluginManager = new PluginManager("tests");
#else
@@ -88,51 +95,59 @@ namespace DiscordBotCore
CurrentApplication.InternalActionManager = new InternalActionManager();
await CurrentApplication.InternalActionManager.Initialize();
-
}
+ ///
+ /// A special class that is designed to log messages. It is a wrapper around the Logger module
+ /// The logger module is required to have this specific methods:
+ ///
+ /// BaseLogException(Exception ex, object sender, bool fullStackTrace)
+ /// BaseLog(string message)
+ /// LogWithTypeAndFormat(string message, LogType logType, string format)
+ /// LogWithType(string message, LogType logType)
+ /// LogWithSender(string message, object sender)
+ /// LogWithTypeAndSender(string message, object sender, LogType type)
+ /// SetPrintFunction(Action[in string] outFunction)
+ ///
+ /// If your custom logger does not have the following methods mapped, the application might crash.
+ /// Please check modules.json file for the mapping or refer to the official repository for the logger module.
+ ///
public static class Logger
{
+ private static readonly KeyValuePair _LoggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
public static async void LogException(Exception ex, object sender, bool fullStackTrace = false)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["BaseLogException"], [ex, sender, fullStackTrace]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["BaseLogException"], [ex, sender, fullStackTrace]);
}
public static async void Log(string message)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["BaseLog"], [message]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["BaseLog"], [message]);
}
public static async void Log(string message, LogType logType, string format)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["LogWithTypeAndFormat"], [message, logType, format]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["LogWithTypeAndFormat"], [message, logType, format]);
}
public static async void Log(string message, LogType logType)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["LogWithType"], [message, logType]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["LogWithType"], [message, logType]);
}
public static async void Log(string message, object sender)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["LogWithSender"], [message, sender]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["LogWithSender"], [message, sender]);
}
public static async void Log(string message, object sender, LogType type)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["LogWithTypeAndSender"], [message, sender, type]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["LogWithTypeAndSender"], [message, sender, type]);
}
-
+
public static async void SetOutFunction(Action outFunction)
{
- var loggerModule = CurrentApplication._ModuleManager.GetModule(ModuleType.Logger);
- await CurrentApplication._ModuleManager.InvokeMethod(loggerModule.Value, loggerModule.Key.MethodMapping["SetPrintFunction"], [outFunction]);
+ await CurrentApplication._ModuleManager.InvokeMethod(_LoggerModule.Value, _LoggerModule.Value.MethodMapping["SetPrintFunction"], [outFunction]);
}
}
@@ -157,15 +172,33 @@ namespace DiscordBotCore
public static async Task GetPluginDependencyPath(string dependencyName, string? pluginName = null)
{
string? dependencyLocation;
- if(pluginName is null)
+ if (pluginName is null)
dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName);
else
dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName, pluginName);
-
- if(dependencyLocation is null)
+
+ if (dependencyLocation is null)
throw new DependencyNotFoundException($"Dependency {dependencyName} not found", pluginName);
-
+
return dependencyLocation;
}
+
+ ///
+ /// Invokes a method from a module
+ ///
+ public static async Task InvokeModuleMethod(string moduleName, string methodName, object[] parameters)
+ {
+ KeyValuePair module = CurrentApplication._ModuleManager.GetModule(moduleName);
+ await CurrentApplication._ModuleManager.InvokeMethod(module.Value, module.Value.MethodMapping[methodName], parameters);
+ }
+
+ ///
+ /// Invokes a method from a module and returns the result
+ ///
+ public static async Task