2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Remora.Discord.Commands.Contexts;
|
|
|
|
using Remora.Discord.Commands.Services;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2023-07-20 00:01:53 +03:00
|
|
|
namespace Boyfriend.Commands.Events;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles error logging for slash command groups.
|
|
|
|
/// </summary>
|
2023-07-18 15:25:02 +03:00
|
|
|
[UsedImplicitly]
|
2023-08-02 23:51:16 +03:00
|
|
|
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger)
|
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a warning using the injected <see cref="ILogger" /> if the <paramref name="commandResult" /> has not
|
|
|
|
/// succeeded.
|
|
|
|
/// </summary>
|
2023-08-05 21:02:40 +03:00
|
|
|
/// <param name="context">The context of the slash command.</param>
|
2023-07-09 16:32:14 +03:00
|
|
|
/// <param name="commandResult">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> AfterExecutionAsync(
|
2023-08-02 23:51:16 +03:00
|
|
|
ICommandContext context, IResult commandResult, CancellationToken ct = default)
|
|
|
|
{
|
2023-08-05 21:02:40 +03:00
|
|
|
_logger.LogResult(commandResult, $"Error in slash command execution for /{context.Command.Command.Node.Key}.");
|
2023-07-09 16:32:14 +03:00
|
|
|
|
|
|
|
return Task.FromResult(Result.FromSuccess());
|
|
|
|
}
|
|
|
|
}
|