fix(IsLoadableModule): return false for abstract classes (#3148)
* fix(IsLoadableModule): return false for abstract classes IsLoadableModule did not check the property TypeInfo.IsAbstract. Therefore, an unnecessary warning log stated that it could not be loaded. Now, if a class is abstract, it will be ignored safely and all the inheritors will continue to load any commands. * fix: handle other attributes Co-authored-by: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> * refactor: simplify attribute logic * fix: correct terminology in debug message --------- Co-authored-by: Cenk Ergen <57065323+Cenngo@users.noreply.github.com>
This commit is contained in:
@@ -13,9 +13,9 @@ namespace Discord.Commands
|
|||||||
|
|
||||||
public static async Task<IReadOnlyList<TypeInfo>> SearchAsync(Assembly assembly, CommandService service)
|
public static async Task<IReadOnlyList<TypeInfo>> SearchAsync(Assembly assembly, CommandService service)
|
||||||
{
|
{
|
||||||
bool IsLoadableModule(TypeInfo info)
|
static bool IsLoadableModule(TypeInfo info)
|
||||||
{
|
{
|
||||||
return info.DeclaredMethods.Any(x => x.GetCustomAttribute<CommandAttribute>() != null) &&
|
return !info.IsAbstract && info.DeclaredMethods.Any(x => x.GetCustomAttribute<CommandAttribute>() != null) &&
|
||||||
info.GetCustomAttribute<DontAutoLoadAttribute>() == null;
|
info.GetCustomAttribute<DontAutoLoadAttribute>() == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,15 @@ namespace Discord.Interactions.Builders
|
|||||||
|
|
||||||
public static async Task<IEnumerable<TypeInfo>> SearchAsync(Assembly assembly, InteractionService commandService)
|
public static async Task<IEnumerable<TypeInfo>> SearchAsync(Assembly assembly, InteractionService commandService)
|
||||||
{
|
{
|
||||||
static bool IsLoadableModule(TypeInfo info)
|
static bool IsLoadableModule(TypeInfo info) =>
|
||||||
{
|
!info.IsAbstract &&
|
||||||
return info.DeclaredMethods.Any(x => x.GetCustomAttribute<SlashCommandAttribute>() != null);
|
info.DeclaredMethods
|
||||||
}
|
.SelectMany(x => x.GetCustomAttributes())
|
||||||
|
.Any(x => x is SlashCommandAttribute
|
||||||
|
or ComponentInteractionAttribute
|
||||||
|
or ContextCommandAttribute
|
||||||
|
or AutocompleteCommandAttribute
|
||||||
|
or ModalInteractionAttribute);
|
||||||
|
|
||||||
var result = new List<TypeInfo>();
|
var result = new List<TypeInfo>();
|
||||||
|
|
||||||
@@ -58,7 +63,7 @@ namespace Discord.Interactions.Builders
|
|||||||
result.Add(type.AsType(), moduleInfo);
|
result.Add(type.AsType(), moduleInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
await commandService._cmdLogger.DebugAsync($"Successfully built {built.Count} Slash Command modules.").ConfigureAwait(false);
|
await commandService._cmdLogger.DebugAsync($"Successfully built {built.Count} interaction modules.").ConfigureAwait(false);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user