Fixed Help command loading when using the new context

This commit is contained in:
2025-05-22 18:53:22 +03:00
parent a0177ce145
commit 14ca51b18a
5 changed files with 58 additions and 37 deletions

View File

@@ -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.
/// </summary>
/// <param name="key">The key</param>
/// <param name="defaulobject">The default value to be returned if the searched value is not found</param>
/// <param name="defaultObject">The default value to be returned if the searched value is not found</param>
/// <typeparam name="T">The type of the returned value</typeparam>
/// <returns></returns>
T Get<T>(string key, T defaulobject);
T Get<T>(string key, T defaultObject);
/// <summary>
/// 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
/// </summary>
/// <param name="key">The key</param>
/// <param name="defaulobject">The default list to be returned if nothing is found</param>
/// <param name="defaultObject">The default list to be returned if nothing is found</param>
/// <typeparam name="T">The type of the returned value</typeparam>
/// <returns></returns>
List<T> GetList<T>(string key, List<T> defaulobject);
List<T> GetList<T>(string key, List<T> defaultObject);
/// <summary>
/// Remove a key from the custom settings dictionary

View File

@@ -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<IDbEvent>();
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<IDbCommand>(helpCommandType);
}
}
LoadEverythingOfType<IDbCommand>();
LoadEverythingOfType<IDbSlashCommand>();
_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<T>(type);
}
}
private void InitializeType<T>(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}");
}
}

View File

@@ -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);
}
}

View File

@@ -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
/// <param name="commandService">The discord bot command service</param>
/// <param name="botPrefix">The prefix to watch for</param>
/// <param name="logger">The logger</param>
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<string>("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)

View File

@@ -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<string>("prefix", _DefaultPrefix));
_CommandServiceHandler = new CommandHandler(_Logger, _pluginLoader, _Configuration, _Service);
await _CommandServiceHandler.InstallCommandsAsync(client);