From 14ca51b18a81d695e8d284e9961bc84e60ed8ef7 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Thu, 22 May 2025 18:53:22 +0300 Subject: [PATCH] Fixed Help command loading when using the new context --- .../IConfiguration.cs | 8 +-- .../PluginLoader.cs | 65 ++++++++++++------- .../PluginLoaderContext.cs | 2 +- DiscordBotCore/Bot/CommandHandler.cs | 16 +++-- DiscordBotCore/Bot/DiscordBotApplication.cs | 4 +- 5 files changed, 58 insertions(+), 37 deletions(-) diff --git a/DiscordBotCore.Configuration/IConfiguration.cs b/DiscordBotCore.Configuration/IConfiguration.cs index c71f1f1..44a2b52 100644 --- a/DiscordBotCore.Configuration/IConfiguration.cs +++ b/DiscordBotCore.Configuration/IConfiguration.cs @@ -20,10 +20,10 @@ public interface IConfiguration /// Gets the value of a key in the custom settings dictionary. If the T type is different then the object type, it will try to convert it. /// /// The key - /// The default value to be returned if the searched value is not found + /// The default value to be returned if the searched value is not found /// The type of the returned value /// - T Get(string key, T defaulobject); + T Get(string key, T defaultObject); /// /// Gets the value of a key in the custom settings dictionary. If the T type is different then the object type, it will try to convert it. @@ -37,10 +37,10 @@ public interface IConfiguration /// Get a list of values from the custom settings dictionary /// /// The key - /// The default list to be returned if nothing is found + /// The default list to be returned if nothing is found /// The type of the returned value /// - List GetList(string key, List defaulobject); + List GetList(string key, List defaultObject); /// /// Remove a key from the custom settings dictionary diff --git a/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs b/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs index d60ea63..9615fbd 100644 --- a/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs +++ b/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs @@ -12,6 +12,8 @@ namespace DiscordBotCore.PluginManagement.Loading; public class PluginLoader : IPluginLoader { + private static readonly string _HelpCommandNamespaceFullName = "DiscordBotCore.Commands.HelpCommand"; + private readonly IPluginManager _PluginManager; private readonly ILogger _Logger; private readonly IConfiguration _Configuration; @@ -62,11 +64,27 @@ public class PluginLoader : IPluginLoader _SlashCommands.Clear(); await LoadPluginFiles(); - + LoadEverythingOfType(); + var helpCommand = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(assembly => assembly.DefinedTypes.Any(type => type.FullName == _HelpCommandNamespaceFullName) + && assembly.FullName != null + && assembly.FullName.StartsWith("DiscordBotCore")); + + if (helpCommand is not null) + { + var helpCommandType = helpCommand.DefinedTypes.FirstOrDefault(type => type.FullName == _HelpCommandNamespaceFullName && + typeof(IDbCommand).IsAssignableFrom(type)); + if (helpCommandType is not null) + { + InitializeType(helpCommandType); + } + } + LoadEverythingOfType(); LoadEverythingOfType(); + _Logger.Log("Loaded plugins", this); } @@ -153,30 +171,33 @@ public class PluginLoader : IPluginLoader .SelectMany(s => s.GetTypes()) .Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface); - foreach (var type in types) { - T? plugin = (T?)Activator.CreateInstance(type); - if (plugin is null) - { - _Logger.Log($"Failed to create instance of plugin with type {type.FullName} [{type.Assembly}]", this, LogType.Error); - continue; - } + InitializeType(type); + } + } + + private void InitializeType(Type type) + { + T? plugin = (T?)Activator.CreateInstance(type); + if (plugin is null) + { + _Logger.Log($"Failed to create instance of plugin with type {type.FullName} [{type.Assembly}]", this, LogType.Error); + } - switch (plugin) - { - case IDbEvent dbEvent: - InitializeEvent(dbEvent); - break; - case IDbCommand dbCommand: - InitializeDbCommand(dbCommand); - break; - case IDbSlashCommand dbSlashCommand: - InitializeSlashCommand(dbSlashCommand); - break; - default: - throw new PluginNotFoundException($"Unknown plugin type {plugin.GetType().FullName}"); - } + switch (plugin) + { + case IDbEvent dbEvent: + InitializeEvent(dbEvent); + break; + case IDbCommand dbCommand: + InitializeDbCommand(dbCommand); + break; + case IDbSlashCommand dbSlashCommand: + InitializeSlashCommand(dbSlashCommand); + break; + default: + throw new PluginNotFoundException($"Unknown plugin type {plugin.GetType().FullName}"); } } diff --git a/DiscordBotCore.PluginManagement.Loading/PluginLoaderContext.cs b/DiscordBotCore.PluginManagement.Loading/PluginLoaderContext.cs index 238921a..3f82d12 100644 --- a/DiscordBotCore.PluginManagement.Loading/PluginLoaderContext.cs +++ b/DiscordBotCore.PluginManagement.Loading/PluginLoaderContext.cs @@ -15,7 +15,7 @@ public class PluginLoaderContext : AssemblyLoadContext protected override Assembly? Load(AssemblyName assemblyName) { - _logger.Log("Assembly load requested: " + assemblyName.Name, this); + //_logger.Log("Assembly load requested: " + assemblyName.Name, this); return base.Load(assemblyName); } } \ No newline at end of file diff --git a/DiscordBotCore/Bot/CommandHandler.cs b/DiscordBotCore/Bot/CommandHandler.cs index 8524af0..d6e1add 100644 --- a/DiscordBotCore/Bot/CommandHandler.cs +++ b/DiscordBotCore/Bot/CommandHandler.cs @@ -18,7 +18,8 @@ namespace DiscordBotCore.Bot; internal class CommandHandler : ICommandHandler { - private readonly string _botPrefix; + private static readonly string _DefaultPrefix = ";"; + private readonly CommandService _commandService; private readonly ILogger _logger; private readonly IPluginLoader _pluginLoader; @@ -31,10 +32,9 @@ internal class CommandHandler : ICommandHandler /// The discord bot command service /// The prefix to watch for /// The logger - public CommandHandler(ILogger logger, IPluginLoader pluginLoader, IConfiguration configuration, CommandService commandService, string botPrefix) + public CommandHandler(ILogger logger, IPluginLoader pluginLoader, IConfiguration configuration, CommandService commandService) { _commandService = commandService; - _botPrefix = botPrefix; _logger = logger; _pluginLoader = pluginLoader; _configuration = configuration; @@ -94,7 +94,9 @@ internal class CommandHandler : ICommandHandler var argPos = 0; - if (!message.Content.StartsWith(_botPrefix) && !message.HasMentionPrefix(socketClient.CurrentUser, ref argPos)) + string botPrefix = this._configuration.Get("prefix", _DefaultPrefix); + + if (!message.Content.StartsWith(botPrefix) && !message.HasMentionPrefix(socketClient.CurrentUser, ref argPos)) return; var context = new SocketCommandContext(socketClient, message); @@ -125,13 +127,13 @@ internal class CommandHandler : ICommandHandler { plugin = _pluginLoader.Commands! .FirstOrDefault(p => p.Command == - message.Content.Split(' ')[0].Substring(_botPrefix.Length) || + message.Content.Split(' ')[0].Substring(botPrefix.Length) || p.Aliases.Contains( message.Content.Split(' ')[0] - .Substring(_botPrefix.Length) + .Substring(botPrefix.Length) ) ); - cleanMessage = message.Content.Substring(_botPrefix.Length); + cleanMessage = message.Content.Substring(botPrefix.Length); } if (plugin is null) diff --git a/DiscordBotCore/Bot/DiscordBotApplication.cs b/DiscordBotCore/Bot/DiscordBotApplication.cs index 4ca394f..125dc93 100644 --- a/DiscordBotCore/Bot/DiscordBotApplication.cs +++ b/DiscordBotCore/Bot/DiscordBotApplication.cs @@ -14,8 +14,6 @@ public class DiscordBotApplication : IDiscordBotApplication { internal static IPluginLoader _InternalPluginLoader; - private static readonly string _DefaultPrefix = ";"; - private CommandHandler _CommandServiceHandler; private CommandService _Service; private readonly ILogger _Logger; @@ -88,7 +86,7 @@ public class DiscordBotApplication : IDiscordBotApplication await client.StartAsync(); - _CommandServiceHandler = new CommandHandler(_Logger, _pluginLoader, _Configuration, _Service, _Configuration.Get("prefix", _DefaultPrefix)); + _CommandServiceHandler = new CommandHandler(_Logger, _pluginLoader, _Configuration, _Service); await _CommandServiceHandler.InstallCommandsAsync(client);