From 4ed4718e18d3f94fdd4ebc9fb4ae937e3f6c4099 Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Tue, 11 Jan 2022 09:52:46 +0300 Subject: [PATCH] Add Construct Method to InteractionModuleBase and Fix NRE on User-Built Module Creation (#2016) --- .../Builders/ModuleBuilder.cs | 27 +++++++++++-------- .../IInteractionModuleBase.cs | 9 ++++++- .../InteractionModuleBase.cs | 3 +++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs index c396ab5f4..036964778 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs @@ -329,19 +329,24 @@ namespace Discord.Interactions.Builders internal ModuleInfo Build (InteractionService interactionService, IServiceProvider services, ModuleInfo parent = null) { - var moduleInfo = new ModuleInfo(this, interactionService, services, parent); - - IInteractionModuleBase instance = ReflectionUtils.CreateObject(TypeInfo, interactionService, services); - try - { - instance.OnModuleBuilding(interactionService, moduleInfo); - } - finally + if (TypeInfo is not null && ModuleClassBuilder.IsValidModuleDefinition(TypeInfo)) { - ( instance as IDisposable )?.Dispose(); + var instance = ReflectionUtils.CreateObject(TypeInfo, interactionService, services); + + try + { + instance.Construct(this, interactionService); + var moduleInfo = new ModuleInfo(this, interactionService, services, parent); + instance.OnModuleBuilding(interactionService, moduleInfo); + return moduleInfo; + } + finally + { + (instance as IDisposable)?.Dispose(); + } } - - return moduleInfo; + else + return new(this, interactionService, services, parent); } } } diff --git a/src/Discord.Net.Interactions/IInteractionModuleBase.cs b/src/Discord.Net.Interactions/IInteractionModuleBase.cs index bf5530fe0..cc944a558 100644 --- a/src/Discord.Net.Interactions/IInteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/IInteractionModuleBase.cs @@ -38,10 +38,17 @@ namespace Discord.Interactions void AfterExecute (ICommandInfo command); /// - /// Method body to be executed before the derived module is built. + /// Method body to be executed when is called. /// /// Command Service instance that built this module. /// Info class of this module. void OnModuleBuilding (InteractionService commandService, ModuleInfo module); + + /// + /// Method body to be executed after the automated module creation is completed and before is called. + /// + /// Builder class of this module. + /// Command Service instance that is building this method. + void Construct(Builders.ModuleBuilder builder, InteractionService commandService); } } diff --git a/src/Discord.Net.Interactions/InteractionModuleBase.cs b/src/Discord.Net.Interactions/InteractionModuleBase.cs index 739f1da3a..95e64916f 100644 --- a/src/Discord.Net.Interactions/InteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/InteractionModuleBase.cs @@ -29,6 +29,9 @@ namespace Discord.Interactions /// public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { } + /// + public virtual void Construct (Builders.ModuleBuilder builder, InteractionService commandService) { } + internal void SetContext (IInteractionContext context) { var newValue = context as T;