From 9f124b25ab3cc2e64ffbac3aeb81247ebd4d6ca5 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Fri, 24 Dec 2021 10:37:36 -0400 Subject: [PATCH] Add before and after execute async (#1998) --- .../Builders/ModuleClassBuilder.cs | 2 ++ .../IInteractionModuleBase.cs | 16 +++++++++++++++- .../InteractionModuleBase.cs | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs index 58b00c5f8..071c68349 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs @@ -311,6 +311,7 @@ namespace Discord.Interactions.Builders try { + await instance.BeforeExecuteAsync(commandInfo).ConfigureAwait(false); instance.BeforeExecute(commandInfo); var task = commandInvoker(instance, args) ?? Task.Delay(0); @@ -332,6 +333,7 @@ namespace Discord.Interactions.Builders } finally { + await instance.AfterExecuteAsync(commandInfo).ConfigureAwait(false); instance.AfterExecute(commandInfo); ( instance as IDisposable )?.Dispose(); } diff --git a/src/Discord.Net.Interactions/IInteractionModuleBase.cs b/src/Discord.Net.Interactions/IInteractionModuleBase.cs index e840e6a0f..bf5530fe0 100644 --- a/src/Discord.Net.Interactions/IInteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/IInteractionModuleBase.cs @@ -1,3 +1,5 @@ +using System.Threading.Tasks; + namespace Discord.Interactions { /// @@ -11,11 +13,23 @@ namespace Discord.Interactions /// void SetContext (IInteractionContext context); + /// + /// Method body to be executed asynchronously before executing an application command. + /// + /// Command information related to the Discord Application Command. + Task BeforeExecuteAsync(ICommandInfo command); + /// /// Method body to be executed before executing an application command. /// /// Command information related to the Discord Application Command. - void BeforeExecute (ICommandInfo command); + void BeforeExecute(ICommandInfo command); + + /// + /// Method body to be executed asynchronously after an application command execution. + /// + /// Command information related to the Discord Application Command. + Task AfterExecuteAsync(ICommandInfo command); /// /// Method body to be executed after an application command execution. diff --git a/src/Discord.Net.Interactions/InteractionModuleBase.cs b/src/Discord.Net.Interactions/InteractionModuleBase.cs index 56ef11d05..739f1da3a 100644 --- a/src/Discord.Net.Interactions/InteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/InteractionModuleBase.cs @@ -20,6 +20,12 @@ namespace Discord.Interactions /// public virtual void BeforeExecute (ICommandInfo command) { } + /// + public virtual Task BeforeExecuteAsync(ICommandInfo command) => Task.CompletedTask; + + /// + public virtual Task AfterExecuteAsync(ICommandInfo command) => Task.CompletedTask; + /// public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }