Fixed Help command loading when using the new context
This commit is contained in:
@@ -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.
|
/// 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>
|
/// </summary>
|
||||||
/// <param name="key">The key</param>
|
/// <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>
|
/// <typeparam name="T">The type of the returned value</typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
T Get<T>(string key, T defaulobject);
|
T Get<T>(string key, T defaultObject);
|
||||||
|
|
||||||
/// <summary>
|
/// <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.
|
/// 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
|
/// Get a list of values from the custom settings dictionary
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="key">The key</param>
|
/// <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>
|
/// <typeparam name="T">The type of the returned value</typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
List<T> GetList<T>(string key, List<T> defaulobject);
|
List<T> GetList<T>(string key, List<T> defaultObject);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove a key from the custom settings dictionary
|
/// Remove a key from the custom settings dictionary
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ namespace DiscordBotCore.PluginManagement.Loading;
|
|||||||
|
|
||||||
public class PluginLoader : IPluginLoader
|
public class PluginLoader : IPluginLoader
|
||||||
{
|
{
|
||||||
|
private static readonly string _HelpCommandNamespaceFullName = "DiscordBotCore.Commands.HelpCommand";
|
||||||
|
|
||||||
private readonly IPluginManager _PluginManager;
|
private readonly IPluginManager _PluginManager;
|
||||||
private readonly ILogger _Logger;
|
private readonly ILogger _Logger;
|
||||||
private readonly IConfiguration _Configuration;
|
private readonly IConfiguration _Configuration;
|
||||||
@@ -64,9 +66,25 @@ public class PluginLoader : IPluginLoader
|
|||||||
await LoadPluginFiles();
|
await LoadPluginFiles();
|
||||||
|
|
||||||
LoadEverythingOfType<IDbEvent>();
|
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<IDbCommand>();
|
||||||
LoadEverythingOfType<IDbSlashCommand>();
|
LoadEverythingOfType<IDbSlashCommand>();
|
||||||
|
|
||||||
|
|
||||||
_Logger.Log("Loaded plugins", this);
|
_Logger.Log("Loaded plugins", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,30 +171,33 @@ public class PluginLoader : IPluginLoader
|
|||||||
.SelectMany(s => s.GetTypes())
|
.SelectMany(s => s.GetTypes())
|
||||||
.Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface);
|
.Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface);
|
||||||
|
|
||||||
|
|
||||||
foreach (var type in types)
|
foreach (var type in types)
|
||||||
{
|
{
|
||||||
T? plugin = (T?)Activator.CreateInstance(type);
|
InitializeType<T>(type);
|
||||||
if (plugin is null)
|
}
|
||||||
{
|
}
|
||||||
_Logger.Log($"Failed to create instance of plugin with type {type.FullName} [{type.Assembly}]", this, LogType.Error);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (plugin)
|
private void InitializeType<T>(Type type)
|
||||||
{
|
{
|
||||||
case IDbEvent dbEvent:
|
T? plugin = (T?)Activator.CreateInstance(type);
|
||||||
InitializeEvent(dbEvent);
|
if (plugin is null)
|
||||||
break;
|
{
|
||||||
case IDbCommand dbCommand:
|
_Logger.Log($"Failed to create instance of plugin with type {type.FullName} [{type.Assembly}]", this, LogType.Error);
|
||||||
InitializeDbCommand(dbCommand);
|
}
|
||||||
break;
|
|
||||||
case IDbSlashCommand dbSlashCommand:
|
switch (plugin)
|
||||||
InitializeSlashCommand(dbSlashCommand);
|
{
|
||||||
break;
|
case IDbEvent dbEvent:
|
||||||
default:
|
InitializeEvent(dbEvent);
|
||||||
throw new PluginNotFoundException($"Unknown plugin type {plugin.GetType().FullName}");
|
break;
|
||||||
}
|
case IDbCommand dbCommand:
|
||||||
|
InitializeDbCommand(dbCommand);
|
||||||
|
break;
|
||||||
|
case IDbSlashCommand dbSlashCommand:
|
||||||
|
InitializeSlashCommand(dbSlashCommand);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new PluginNotFoundException($"Unknown plugin type {plugin.GetType().FullName}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public class PluginLoaderContext : AssemblyLoadContext
|
|||||||
|
|
||||||
protected override Assembly? Load(AssemblyName assemblyName)
|
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);
|
return base.Load(assemblyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,8 @@ namespace DiscordBotCore.Bot;
|
|||||||
|
|
||||||
internal class CommandHandler : ICommandHandler
|
internal class CommandHandler : ICommandHandler
|
||||||
{
|
{
|
||||||
private readonly string _botPrefix;
|
private static readonly string _DefaultPrefix = ";";
|
||||||
|
|
||||||
private readonly CommandService _commandService;
|
private readonly CommandService _commandService;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IPluginLoader _pluginLoader;
|
private readonly IPluginLoader _pluginLoader;
|
||||||
@@ -31,10 +32,9 @@ internal class CommandHandler : ICommandHandler
|
|||||||
/// <param name="commandService">The discord bot command service</param>
|
/// <param name="commandService">The discord bot command service</param>
|
||||||
/// <param name="botPrefix">The prefix to watch for</param>
|
/// <param name="botPrefix">The prefix to watch for</param>
|
||||||
/// <param name="logger">The logger</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;
|
_commandService = commandService;
|
||||||
_botPrefix = botPrefix;
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_pluginLoader = pluginLoader;
|
_pluginLoader = pluginLoader;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@@ -94,7 +94,9 @@ internal class CommandHandler : ICommandHandler
|
|||||||
|
|
||||||
var argPos = 0;
|
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;
|
return;
|
||||||
|
|
||||||
var context = new SocketCommandContext(socketClient, message);
|
var context = new SocketCommandContext(socketClient, message);
|
||||||
@@ -125,13 +127,13 @@ internal class CommandHandler : ICommandHandler
|
|||||||
{
|
{
|
||||||
plugin = _pluginLoader.Commands!
|
plugin = _pluginLoader.Commands!
|
||||||
.FirstOrDefault(p => p.Command ==
|
.FirstOrDefault(p => p.Command ==
|
||||||
message.Content.Split(' ')[0].Substring(_botPrefix.Length) ||
|
message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||||
p.Aliases.Contains(
|
p.Aliases.Contains(
|
||||||
message.Content.Split(' ')[0]
|
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)
|
if (plugin is null)
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ public class DiscordBotApplication : IDiscordBotApplication
|
|||||||
{
|
{
|
||||||
internal static IPluginLoader _InternalPluginLoader;
|
internal static IPluginLoader _InternalPluginLoader;
|
||||||
|
|
||||||
private static readonly string _DefaultPrefix = ";";
|
|
||||||
|
|
||||||
private CommandHandler _CommandServiceHandler;
|
private CommandHandler _CommandServiceHandler;
|
||||||
private CommandService _Service;
|
private CommandService _Service;
|
||||||
private readonly ILogger _Logger;
|
private readonly ILogger _Logger;
|
||||||
@@ -88,7 +86,7 @@ public class DiscordBotApplication : IDiscordBotApplication
|
|||||||
|
|
||||||
await client.StartAsync();
|
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);
|
await _CommandServiceHandler.InstallCommandsAsync(client);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user