Browse Source

feat: Add async callbacks for IModuleBase (#2370)

tags/3.8.0
SKProCH GitHub 2 years ago
parent
commit
503fa755a0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions
  1. +2
    -0
      src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
  2. +13
    -0
      src/Discord.Net.Commands/IModuleBase.cs
  3. +12
    -0
      src/Discord.Net.Commands/ModuleBase.cs

+ 2
- 0
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs View File

@@ -206,6 +206,7 @@ namespace Discord.Commands

try
{
await instance.BeforeExecuteAsync(cmd).ConfigureAwait(false);
instance.BeforeExecute(cmd);

var task = method.Invoke(instance, args) as Task ?? Task.Delay(0);
@@ -221,6 +222,7 @@ namespace Discord.Commands
}
finally
{
await instance.AfterExecuteAsync(cmd).ConfigureAwait(false);
instance.AfterExecute(cmd);
(instance as IDisposable)?.Dispose();
}


+ 13
- 0
src/Discord.Net.Commands/IModuleBase.cs View File

@@ -1,4 +1,5 @@
using Discord.Commands.Builders;
using System.Threading.Tasks;

namespace Discord.Commands
{
@@ -13,12 +14,24 @@ namespace Discord.Commands
/// <param name="context">The context to set.</param>
void SetContext(ICommandContext context);

/// <summary>
/// Executed asynchronously before a command is run in this module base.
/// </summary>
/// <param name="command">The command thats about to run.</param>
Task BeforeExecuteAsync(CommandInfo command);

/// <summary>
/// Executed before a command is run in this module base.
/// </summary>
/// <param name="command">The command thats about to run.</param>
void BeforeExecute(CommandInfo command);

/// <summary>
/// Executed asynchronously after a command is run in this module base.
/// </summary>
/// <param name="command">The command thats about to run.</param>
Task AfterExecuteAsync(CommandInfo command);

/// <summary>
/// Executed after a command is ran in this module base.
/// </summary>


+ 12
- 0
src/Discord.Net.Commands/ModuleBase.cs View File

@@ -46,6 +46,11 @@ namespace Discord.Commands
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds).ConfigureAwait(false);
}
/// <summary>
/// The method to execute asynchronously before executing the command.
/// </summary>
/// <param name="command">The <see cref="CommandInfo"/> of the command to be executed.</param>
protected virtual Task BeforeExecuteAsync(CommandInfo command) => Task.CompletedTask;
/// <summary>
/// The method to execute before executing the command.
/// </summary>
/// <param name="command">The <see cref="CommandInfo"/> of the command to be executed.</param>
@@ -53,6 +58,11 @@ namespace Discord.Commands
{
}
/// <summary>
/// The method to execute asynchronously after executing the command.
/// </summary>
/// <param name="command">The <see cref="CommandInfo"/> of the command to be executed.</param>
protected virtual Task AfterExecuteAsync(CommandInfo command) => Task.CompletedTask;
/// <summary>
/// The method to execute after executing the command.
/// </summary>
/// <param name="command">The <see cref="CommandInfo"/> of the command to be executed.</param>
@@ -76,7 +86,9 @@ namespace Discord.Commands
var newValue = context as T;
Context = newValue ?? throw new InvalidOperationException($"Invalid context type. Expected {typeof(T).Name}, got {context.GetType().Name}.");
}
Task IModuleBase.BeforeExecuteAsync(CommandInfo command) => BeforeExecuteAsync(command);
void IModuleBase.BeforeExecute(CommandInfo command) => BeforeExecute(command);
Task IModuleBase.AfterExecuteAsync(CommandInfo command) => AfterExecuteAsync(command);
void IModuleBase.AfterExecute(CommandInfo command) => AfterExecute(command);
void IModuleBase.OnModuleBuilding(CommandService commandService, ModuleBuilder builder) => OnModuleBuilding(commandService, builder);
#endregion


Loading…
Cancel
Save