Browse Source

Add Construct Method to InteractionModuleBase and Fix NRE on User-Built Module Creation (#2016)

tags/3.2.0
Cenk Ergen GitHub 3 years ago
parent
commit
4ed4718e18
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 12 deletions
  1. +16
    -11
      src/Discord.Net.Interactions/Builders/ModuleBuilder.cs
  2. +8
    -1
      src/Discord.Net.Interactions/IInteractionModuleBase.cs
  3. +3
    -0
      src/Discord.Net.Interactions/InteractionModuleBase.cs

+ 16
- 11
src/Discord.Net.Interactions/Builders/ModuleBuilder.cs View File

@@ -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<IInteractionModuleBase>.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<IInteractionModuleBase>.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);
}
}
}

+ 8
- 1
src/Discord.Net.Interactions/IInteractionModuleBase.cs View File

@@ -38,10 +38,17 @@ namespace Discord.Interactions
void AfterExecute (ICommandInfo command);

/// <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>
/// <param name="commandService">Command Service instance that built this module.</param>
/// <param name="module">Info class of this module.</param>
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);
}
}

+ 3
- 0
src/Discord.Net.Interactions/InteractionModuleBase.cs View File

@@ -29,6 +29,9 @@ namespace Discord.Interactions
/// <inheritdoc/>
public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }

/// <inheritdoc/>
public virtual void Construct (Builders.ModuleBuilder builder, InteractionService commandService) { }

internal void SetContext (IInteractionContext context)
{
var newValue = context as T;


Loading…
Cancel
Save