2023-07-20 00:01:53 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles error logging for slash commands that couldn't be successfully prepared.
|
|
|
|
/// </summary>
|
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class LoggingPreparationErrorEvent : IPreparationErrorEvent
|
|
|
|
{
|
2023-07-20 00:01:53 +03:00
|
|
|
private readonly ILogger<LoggingPreparationErrorEvent> _logger;
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public LoggingPreparationErrorEvent(ILogger<LoggingPreparationErrorEvent> logger)
|
|
|
|
{
|
2023-07-20 00:01:53 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a warning using the injected <see cref="ILogger" /> if the <paramref name="preparationResult" /> has not
|
|
|
|
/// succeeded.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">The context of the slash command. Unused.</param>
|
|
|
|
/// <param name="preparationResult">The result whose success is checked.</param>
|
|
|
|
/// <param name="ct">The cancellation token for this operation. Unused.</param>
|
|
|
|
/// <returns>A result which has succeeded.</returns>
|
|
|
|
public Task<Result> PreparationFailed(
|
2023-08-02 23:51:16 +03:00
|
|
|
IOperationContext context, IResult preparationResult, CancellationToken ct = default)
|
|
|
|
{
|
|
|
|
if (!preparationResult.IsSuccess && !preparationResult.Error.IsUserOrEnvironmentError())
|
|
|
|
{
|
2023-07-20 00:01:53 +03:00
|
|
|
_logger.LogWarning("Error in slash command preparation.\n{ErrorMessage}", preparationResult.Error.Message);
|
|
|
|
if (preparationResult.Error is ExceptionError exerr)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-20 00:01:53 +03:00
|
|
|
_logger.LogError(exerr.Exception, "An exception has been thrown");
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-20 00:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult(Result.FromSuccess());
|
|
|
|
}
|
|
|
|
}
|