feat: Add async callbacks for IModuleBase (#2370)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user