From d9aeccb83bbdefe4d4f158d4397e87c94d660558 Mon Sep 17 00:00:00 2001 From: runebaas Date: Mon, 31 Aug 2020 22:24:05 +0200 Subject: [PATCH] Add a OnException hook to IModuleBase --- .../Builders/ModuleClassBuilder.cs | 5 +++++ src/Discord.Net.Commands/IModuleBase.cs | 3 +++ src/Discord.Net.Commands/ModuleBase.cs | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs index 28037b0fa..c5b7c8242 100644 --- a/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs @@ -220,6 +220,11 @@ namespace Discord.Commands return ExecuteResult.FromSuccess(); } } + catch (Exception exception) + { + instance.OnException(cmd, exception); + return ExecuteResult.FromError(exception); + } finally { instance.AfterExecute(cmd); diff --git a/src/Discord.Net.Commands/IModuleBase.cs b/src/Discord.Net.Commands/IModuleBase.cs index 3b641ec5f..3ca4f46e0 100644 --- a/src/Discord.Net.Commands/IModuleBase.cs +++ b/src/Discord.Net.Commands/IModuleBase.cs @@ -1,3 +1,4 @@ +using System; using Discord.Commands.Builders; namespace Discord.Commands @@ -10,6 +11,8 @@ namespace Discord.Commands void AfterExecute(CommandInfo command); + void OnException(CommandInfo command, Exception exception); + void OnModuleBuilding(CommandService commandService, ModuleBuilder builder); } } diff --git a/src/Discord.Net.Commands/ModuleBase.cs b/src/Discord.Net.Commands/ModuleBase.cs index ec1722c70..e03b3de4c 100644 --- a/src/Discord.Net.Commands/ModuleBase.cs +++ b/src/Discord.Net.Commands/ModuleBase.cs @@ -54,6 +54,15 @@ namespace Discord.Commands { } + /// + /// The method to execute when an exception is thrown + /// + /// + /// + protected virtual void OnException(CommandInfo command, Exception exception) + { + } + /// /// The method to execute when building the module. /// @@ -71,6 +80,7 @@ namespace Discord.Commands } void IModuleBase.BeforeExecute(CommandInfo command) => BeforeExecute(command); void IModuleBase.AfterExecute(CommandInfo command) => AfterExecute(command); + void IModuleBase.OnException(CommandInfo command, Exception exception) => OnException(command, exception); void IModuleBase.OnModuleBuilding(CommandService commandService, ModuleBuilder builder) => OnModuleBuilding(commandService, builder); } }