From 0eb17a977dbcd99819bd8da0805f5d237667339f Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Fri, 16 Jun 2023 16:32:40 +0500 Subject: [PATCH] Add error logging to slash command preparation Signed-off-by: Octol1ttle --- Boyfriend.cs | 1 + Commands/ErrorLoggingPostExecutionEvent.cs | 35 ----------- Commands/ErrorLoggingPreparationErrorEvent.cs | 62 +++++++++++++++++++ 3 files changed, 63 insertions(+), 35 deletions(-) delete mode 100644 Commands/ErrorLoggingPostExecutionEvent.cs create mode 100644 Commands/ErrorLoggingPreparationErrorEvent.cs diff --git a/Boyfriend.cs b/Boyfriend.cs index 2145eef..ada3cc0 100644 --- a/Boyfriend.cs +++ b/Boyfriend.cs @@ -69,6 +69,7 @@ public class Boyfriend { services.AddTransient() .AddDiscordCaching() .AddDiscordCommands(true) + .AddPreparationErrorEvent() .AddPostExecutionEvent() .AddInteractivity() .AddInteractionGroup() diff --git a/Commands/ErrorLoggingPostExecutionEvent.cs b/Commands/ErrorLoggingPostExecutionEvent.cs deleted file mode 100644 index a85f8a7..0000000 --- a/Commands/ErrorLoggingPostExecutionEvent.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.Extensions.Logging; -using Remora.Discord.Commands.Contexts; -using Remora.Discord.Commands.Services; -using Remora.Results; - -// ReSharper disable ClassNeverInstantiated.Global - -namespace Boyfriend.Commands; - -/// -/// Handles error logging for slash command groups. -/// -public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent { - private readonly ILogger _logger; - - public ErrorLoggingPostExecutionEvent(ILogger logger) { - _logger = logger; - } - - /// - /// Logs a warning using the injected if the has not - /// succeeded. - /// - /// The context of the slash command. Unused. - /// The result whose success is checked. - /// The cancellation token for this operation. Unused. - /// A result which has succeeded. - 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()); - } -} diff --git a/Commands/ErrorLoggingPreparationErrorEvent.cs b/Commands/ErrorLoggingPreparationErrorEvent.cs new file mode 100644 index 0000000..30869b4 --- /dev/null +++ b/Commands/ErrorLoggingPreparationErrorEvent.cs @@ -0,0 +1,62 @@ +using Microsoft.Extensions.Logging; +using Remora.Discord.Commands.Contexts; +using Remora.Discord.Commands.Services; +using Remora.Results; + +// ReSharper disable ClassNeverInstantiated.Global + +namespace Boyfriend.Commands; + +/// +/// Handles error logging for slash commands that couldn't be successfully prepared. +/// +public class ErrorLoggingPreparationErrorEvent : IPreparationErrorEvent { + private readonly ILogger _logger; + + public ErrorLoggingPreparationErrorEvent(ILogger logger) { + _logger = logger; + } + + /// + /// Logs a warning using the injected if the has not + /// succeeded. + /// + /// The context of the slash command. Unused. + /// The result whose success is checked. + /// The cancellation token for this operation. Unused. + /// A result which has succeeded. + public Task PreparationFailed( + IOperationContext context, IResult preparationResult, CancellationToken ct = default) { + if (!preparationResult.IsSuccess) + _logger.LogWarning("Error in slash command preparation.\n{ErrorMessage}", preparationResult.Error.Message); + + return Task.FromResult(Result.FromSuccess()); + } +} + +/// +/// Handles error logging for slash command groups. +/// +public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent { + private readonly ILogger _logger; + + public ErrorLoggingPostExecutionEvent(ILogger logger) { + _logger = logger; + } + + /// + /// Logs a warning using the injected if the has not + /// succeeded. + /// + /// The context of the slash command. Unused. + /// The result whose success is checked. + /// The cancellation token for this operation. Unused. + /// A result which has succeeded. + public Task AfterExecutionAsync( + ICommandContext context, IResult commandResult, CancellationToken ct = default) { + if (!commandResult.IsSuccess) + _logger.LogWarning("Error in slash command execution.\n{ErrorMessage}", commandResult.Error.Message); + + return Task.FromResult(Result.FromSuccess()); + } +}