Add and use interaction command hooks for BeforeExecute, AfterExecute, OnException and GetExceptionResponse
This commit is contained in:
parent
9a2bf84a05
commit
24749d9009
3 changed files with 43 additions and 7 deletions
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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<InteractionResponse> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue