diff --git a/Boyfriend.cs b/Boyfriend.cs index 8b63b52..3ba6a3f 100644 --- a/Boyfriend.cs +++ b/Boyfriend.cs @@ -69,6 +69,7 @@ public class Boyfriend { services.AddTransient() .AddDiscordCaching() .AddDiscordCommands(true) + .AddPostExecutionEvent() .AddInteractivity() .AddInteractionGroup() .AddSingleton() diff --git a/Commands/ErrorLoggingPostExecutionEvent.cs b/Commands/ErrorLoggingPostExecutionEvent.cs new file mode 100644 index 0000000..65604bd --- /dev/null +++ b/Commands/ErrorLoggingPostExecutionEvent.cs @@ -0,0 +1,24 @@ +using Microsoft.Extensions.Logging; +using Remora.Discord.Commands.Contexts; +using Remora.Discord.Commands.Services; +using Remora.Results; + +// ReSharper disable ClassNeverInstantiated.Global + +namespace Boyfriend.Commands; + +public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent { + private readonly ILogger _logger; + + public ErrorLoggingPostExecutionEvent(ILogger logger) { + _logger = logger; + } + + public Task AfterExecutionAsync( + ICommandContext context, IResult commandResult, CancellationToken ct = default) { + if (!commandResult.IsSuccess) + _logger.LogWarning("Error in slash command handler.\n{ErrorMessage}", commandResult.Error.Message); + + return Task.FromResult(Result.FromSuccess()); + } +}