Add Construct Method to InteractionModuleBase and Fix NRE on User-Built Module Creation (#2016)
This commit is contained in:
@@ -329,19 +329,24 @@ namespace Discord.Interactions.Builders
|
|||||||
|
|
||||||
internal ModuleInfo Build (InteractionService interactionService, IServiceProvider services, ModuleInfo parent = null)
|
internal ModuleInfo Build (InteractionService interactionService, IServiceProvider services, ModuleInfo parent = null)
|
||||||
{
|
{
|
||||||
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
|
if (TypeInfo is not null && ModuleClassBuilder.IsValidModuleDefinition(TypeInfo))
|
||||||
|
|
||||||
IInteractionModuleBase instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
instance.OnModuleBuilding(interactionService, moduleInfo);
|
var instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
( instance as IDisposable )?.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
return moduleInfo;
|
try
|
||||||
|
{
|
||||||
|
instance.Construct(this, interactionService);
|
||||||
|
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
|
||||||
|
instance.OnModuleBuilding(interactionService, moduleInfo);
|
||||||
|
return moduleInfo;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
(instance as IDisposable)?.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return new(this, interactionService, services, parent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,10 +38,17 @@ namespace Discord.Interactions
|
|||||||
void AfterExecute (ICommandInfo command);
|
void AfterExecute (ICommandInfo command);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Method body to be executed before the derived module is built.
|
/// Method body to be executed when <see cref="Builders.ModuleBuilder.Build(InteractionService, System.IServiceProvider, ModuleInfo)"/> is called.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="commandService">Command Service instance that built this module.</param>
|
/// <param name="commandService">Command Service instance that built this module.</param>
|
||||||
/// <param name="module">Info class of this module.</param>
|
/// <param name="module">Info class of this module.</param>
|
||||||
void OnModuleBuilding (InteractionService commandService, ModuleInfo module);
|
void OnModuleBuilding (InteractionService commandService, ModuleInfo module);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">Builder class of this module.</param>
|
||||||
|
/// <param name="commandService">Command Service instance that is building this method.</param>
|
||||||
|
void Construct(Builders.ModuleBuilder builder, InteractionService commandService);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ namespace Discord.Interactions
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }
|
public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public virtual void Construct (Builders.ModuleBuilder builder, InteractionService commandService) { }
|
||||||
|
|
||||||
internal void SetContext (IInteractionContext context)
|
internal void SetContext (IInteractionContext context)
|
||||||
{
|
{
|
||||||
var newValue = context as T;
|
var newValue = context as T;
|
||||||
|
|||||||
Reference in New Issue
Block a user