Skip to content

Commit 4ed4718

Browse files
authored
Add Construct Method to InteractionModuleBase and Fix NRE on User-Built Module Creation (#2016)
1 parent 99d8ca4 commit 4ed4718

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/Discord.Net.Interactions/Builders/ModuleBuilder.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,24 @@ public ModuleBuilder AddModule (Action<ModuleBuilder> configure)
329329

330330
internal ModuleInfo Build (InteractionService interactionService, IServiceProvider services, ModuleInfo parent = null)
331331
{
332-
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
333-
334-
IInteractionModuleBase instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
335-
try
336-
{
337-
instance.OnModuleBuilding(interactionService, moduleInfo);
338-
}
339-
finally
332+
if (TypeInfo is not null && ModuleClassBuilder.IsValidModuleDefinition(TypeInfo))
340333
{
341-
( instance as IDisposable )?.Dispose();
334+
var instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
335+
336+
try
337+
{
338+
instance.Construct(this, interactionService);
339+
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
340+
instance.OnModuleBuilding(interactionService, moduleInfo);
341+
return moduleInfo;
342+
}
343+
finally
344+
{
345+
(instance as IDisposable)?.Dispose();
346+
}
342347
}
343-
344-
return moduleInfo;
348+
else
349+
return new(this, interactionService, services, parent);
345350
}
346351
}
347352
}

src/Discord.Net.Interactions/IInteractionModuleBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ public interface IInteractionModuleBase
3838
void AfterExecute (ICommandInfo command);
3939

4040
/// <summary>
41-
/// Method body to be executed before the derived module is built.
41+
/// Method body to be executed when <see cref="Builders.ModuleBuilder.Build(InteractionService, System.IServiceProvider, ModuleInfo)"/> is called.
4242
/// </summary>
4343
/// <param name="commandService">Command Service instance that built this module.</param>
4444
/// <param name="module">Info class of this module.</param>
4545
void OnModuleBuilding (InteractionService commandService, ModuleInfo module);
46+
47+
/// <summary>
48+
/// Method body to be executed after the automated module creation is completed and before <see cref="Builders.ModuleBuilder.Build(InteractionService, System.IServiceProvider, ModuleInfo)"/> is called.
49+
/// </summary>
50+
/// <param name="builder">Builder class of this module.</param>
51+
/// <param name="commandService">Command Service instance that is building this method.</param>
52+
void Construct(Builders.ModuleBuilder builder, InteractionService commandService);
4653
}
4754
}

src/Discord.Net.Interactions/InteractionModuleBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public virtual void BeforeExecute (ICommandInfo command) { }
2929
/// <inheritdoc/>
3030
public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }
3131

32+
/// <inheritdoc/>
33+
public virtual void Construct (Builders.ModuleBuilder builder, InteractionService commandService) { }
34+
3235
internal void SetContext (IInteractionContext context)
3336
{
3437
var newValue = context as T;

0 commit comments

Comments
 (0)