From 503fa755a011c2fa52ccc76bce74ad712c949aff Mon Sep 17 00:00:00 2001
From: SKProCH <29896317+SKProCH@users.noreply.github.com>
Date: Tue, 2 Aug 2022 12:19:31 +0300
Subject: [PATCH] feat: Add async callbacks for IModuleBase (#2370)
---
.../Builders/ModuleClassBuilder.cs | 2 ++
src/Discord.Net.Commands/IModuleBase.cs | 13 +++++++++++++
src/Discord.Net.Commands/ModuleBase.cs | 12 ++++++++++++
3 files changed, 27 insertions(+)
diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
index 22c58f5c7..f98c81abd 100644
--- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
+++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
@@ -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();
}
diff --git a/src/Discord.Net.Commands/IModuleBase.cs b/src/Discord.Net.Commands/IModuleBase.cs
index 8b021f4de..7a953b47b 100644
--- a/src/Discord.Net.Commands/IModuleBase.cs
+++ b/src/Discord.Net.Commands/IModuleBase.cs
@@ -1,4 +1,5 @@
using Discord.Commands.Builders;
+using System.Threading.Tasks;
namespace Discord.Commands
{
@@ -13,12 +14,24 @@ namespace Discord.Commands
/// The context to set.
void SetContext(ICommandContext context);
+ ///
+ /// Executed asynchronously before a command is run in this module base.
+ ///
+ /// The command thats about to run.
+ Task BeforeExecuteAsync(CommandInfo command);
+
///
/// Executed before a command is run in this module base.
///
/// The command thats about to run.
void BeforeExecute(CommandInfo command);
+ ///
+ /// Executed asynchronously after a command is run in this module base.
+ ///
+ /// The command thats about to run.
+ Task AfterExecuteAsync(CommandInfo command);
+
///
/// Executed after a command is ran in this module base.
///
diff --git a/src/Discord.Net.Commands/ModuleBase.cs b/src/Discord.Net.Commands/ModuleBase.cs
index 5008cca35..b2d6ba119 100644
--- a/src/Discord.Net.Commands/ModuleBase.cs
+++ b/src/Discord.Net.Commands/ModuleBase.cs
@@ -46,6 +46,11 @@ namespace Discord.Commands
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds).ConfigureAwait(false);
}
///
+ /// The method to execute asynchronously before executing the command.
+ ///
+ /// The of the command to be executed.
+ protected virtual Task BeforeExecuteAsync(CommandInfo command) => Task.CompletedTask;
+ ///
/// The method to execute before executing the command.
///
/// The of the command to be executed.
@@ -53,6 +58,11 @@ namespace Discord.Commands
{
}
///
+ /// The method to execute asynchronously after executing the command.
+ ///
+ /// The of the command to be executed.
+ protected virtual Task AfterExecuteAsync(CommandInfo command) => Task.CompletedTask;
+ ///
/// The method to execute after executing the command.
///
/// The of the command to be executed.
@@ -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