diff --git a/src/Core/Interactions/IInteractionBase.cs b/src/Core/Interactions/IInteractionBase.cs index dd37bdc..0ebedb5 100644 --- a/src/Core/Interactions/IInteractionBase.cs +++ b/src/Core/Interactions/IInteractionBase.cs @@ -1,9 +1,13 @@ +using System; +using Geekbot.Core.Interactions.Response; + namespace Geekbot.Core.Interactions { public interface IInteractionBase { void BeforeExecute(); void AfterExecute(); - void OnException(); + void OnException(Exception e); + InteractionResponse GetExceptionResponse(); } } \ No newline at end of file diff --git a/src/Core/Interactions/InteractionBase.cs b/src/Core/Interactions/InteractionBase.cs index 114cc78..bfc3d6b 100644 --- a/src/Core/Interactions/InteractionBase.cs +++ b/src/Core/Interactions/InteractionBase.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using Geekbot.Core.Interactions.ApplicationCommand; using Geekbot.Core.Interactions.Request; @@ -7,25 +8,41 @@ namespace Geekbot.Core.Interactions { public abstract class InteractionBase : IInteractionBase { - protected virtual void BeforeExecute() + public InteractionBase() {} + + public virtual void BeforeExecute() { + } - protected virtual void AfterExecute() + public virtual void AfterExecute() { } - protected virtual void OnException() + public virtual void OnException(Exception exception) { } + public virtual InteractionResponse GetExceptionResponse() + { + return new InteractionResponse() + { + Type = InteractionResponseType.ChannelMessageWithSource, + Data = new() + { + Content = "Something went wrong :confused:" + } + }; + } + public abstract Command GetCommandInfo(); public abstract Task Exec(Interaction interaction); void IInteractionBase.BeforeExecute() => this.BeforeExecute(); void IInteractionBase.AfterExecute() => this.AfterExecute(); - void IInteractionBase.OnException() => this.OnException(); + void IInteractionBase.OnException(Exception e) => this.OnException(e); + InteractionResponse IInteractionBase.GetExceptionResponse() => this.GetExceptionResponse(); } } \ No newline at end of file diff --git a/src/Core/Interactions/InteractionCommandManager.cs b/src/Core/Interactions/InteractionCommandManager.cs index 502eba2..88285ed 100644 --- a/src/Core/Interactions/InteractionCommandManager.cs +++ b/src/Core/Interactions/InteractionCommandManager.cs @@ -40,8 +40,23 @@ namespace Geekbot.Core.Interactions { var type = _commands[interaction.Data.Name]; var command = (InteractionBase)Activator.CreateInstance(type); - - return await command.Exec(interaction); + + InteractionResponse response; + command.BeforeExecute(); + try + { + response = await command.Exec(interaction); + } + catch (Exception e) + { + command.OnException(e); + response = command.GetExceptionResponse(); + } + finally + { + command.AfterExecute(); + } + return response; } } } \ No newline at end of file