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());
}
}