From 978f999843e49eb10583ff71bca8a286548f8542 Mon Sep 17 00:00:00 2001 From: OverwrittenCode Date: Sun, 13 Jul 2025 21:22:32 +0100 Subject: [PATCH] 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> --- .../Builders/ModuleClassBuilder.cs | 4 ++-- .../Builders/ModuleClassBuilder.cs | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index ee9623e0..7b0a8bd3 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -13,9 +13,9 @@ namespace Discord.Commands public static async Task> SearchAsync(Assembly assembly, CommandService service) { - bool IsLoadableModule(TypeInfo info) + static bool IsLoadableModule(TypeInfo info) { - return info.DeclaredMethods.Any(x => x.GetCustomAttribute() != null) && + return !info.IsAbstract && info.DeclaredMethods.Any(x => x.GetCustomAttribute() != null) && info.GetCustomAttribute() == null; } diff --git a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs index e16f23cc..87aefa8f 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs @@ -16,10 +16,15 @@ namespace Discord.Interactions.Builders public static async Task> SearchAsync(Assembly assembly, InteractionService commandService) { - static bool IsLoadableModule(TypeInfo info) - { - return info.DeclaredMethods.Any(x => x.GetCustomAttribute() != null); - } + static bool IsLoadableModule(TypeInfo info) => + !info.IsAbstract && + info.DeclaredMethods + .SelectMany(x => x.GetCustomAttributes()) + .Any(x => x is SlashCommandAttribute + or ComponentInteractionAttribute + or ContextCommandAttribute + or AutocompleteCommandAttribute + or ModalInteractionAttribute); var result = new List(); @@ -58,7 +63,7 @@ namespace Discord.Interactions.Builders 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; }