From c825848d7eb321743f858ea1812a279af4f35ca6 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Thu, 20 Jul 2023 02:01:53 +0500 Subject: [PATCH] Split error logging events into separate files (#51) This PR splits `LoggingPreparationErrorEvent` and `ErrorLoggingPostExecutionEvent` classes into separate files and puts these files in a separate namespace: `Boyfriend.Commands.Events`. This makes these classes easier to find and distinguish from commands groups. Signed-off-by: Octol1ttle --- src/Boyfriend.cs | 3 +- .../ErrorLoggingPostExecutionEvent.cs} | 33 +--------------- .../Events/LoggingPreparationErrorEvent.cs | 39 +++++++++++++++++++ 3 files changed, 42 insertions(+), 33 deletions(-) rename src/Commands/{ErrorLoggingEvents.cs => Events/ErrorLoggingPostExecutionEvent.cs} (51%) create mode 100644 src/Commands/Events/LoggingPreparationErrorEvent.cs diff --git a/src/Boyfriend.cs b/src/Boyfriend.cs index 0731ccd..d1c4f7a 100644 --- a/src/Boyfriend.cs +++ b/src/Boyfriend.cs @@ -1,4 +1,5 @@ using Boyfriend.Commands; +using Boyfriend.Commands.Events; using Boyfriend.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -71,7 +72,7 @@ public class Boyfriend { .AddInteractivity() .AddInteractionGroup() // Slash command event handlers - .AddPreparationErrorEvent() + .AddPreparationErrorEvent() .AddPostExecutionEvent() // Services .AddSingleton() diff --git a/src/Commands/ErrorLoggingEvents.cs b/src/Commands/Events/ErrorLoggingPostExecutionEvent.cs similarity index 51% rename from src/Commands/ErrorLoggingEvents.cs rename to src/Commands/Events/ErrorLoggingPostExecutionEvent.cs index c5eba21..51c2a8d 100644 --- a/src/Commands/ErrorLoggingEvents.cs +++ b/src/Commands/Events/ErrorLoggingPostExecutionEvent.cs @@ -5,38 +5,7 @@ using Remora.Discord.Commands.Extensions; using Remora.Discord.Commands.Services; using Remora.Results; -namespace Boyfriend.Commands; - -/// -/// Handles error logging for slash commands that couldn't be successfully prepared. -/// -[UsedImplicitly] -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 && !preparationResult.Error.IsUserOrEnvironmentError()) { - _logger.LogWarning("Error in slash command preparation.\n{ErrorMessage}", preparationResult.Error.Message); - if (preparationResult.Error is ExceptionError exerr) - _logger.LogError(exerr.Exception, "An exception has been thrown"); - } - - return Task.FromResult(Result.FromSuccess()); - } -} +namespace Boyfriend.Commands.Events; /// /// Handles error logging for slash command groups. diff --git a/src/Commands/Events/LoggingPreparationErrorEvent.cs b/src/Commands/Events/LoggingPreparationErrorEvent.cs new file mode 100644 index 0000000..7e8b2bb --- /dev/null +++ b/src/Commands/Events/LoggingPreparationErrorEvent.cs @@ -0,0 +1,39 @@ +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +using Remora.Discord.Commands.Contexts; +using Remora.Discord.Commands.Extensions; +using Remora.Discord.Commands.Services; +using Remora.Results; + +namespace Boyfriend.Commands.Events; + +/// +/// Handles error logging for slash commands that couldn't be successfully prepared. +/// +[UsedImplicitly] +public class LoggingPreparationErrorEvent : IPreparationErrorEvent { + private readonly ILogger _logger; + + public LoggingPreparationErrorEvent(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 && !preparationResult.Error.IsUserOrEnvironmentError()) { + _logger.LogWarning("Error in slash command preparation.\n{ErrorMessage}", preparationResult.Error.Message); + if (preparationResult.Error is ExceptionError exerr) + _logger.LogError(exerr.Exception, "An exception has been thrown"); + } + + return Task.FromResult(Result.FromSuccess()); + } +}