mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-04-20 00:43:36 +03:00
Add error logging to slash command preparation
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
a58a32980a
commit
0eb17a977d
3 changed files with 63 additions and 35 deletions
|
@ -69,6 +69,7 @@ public class Boyfriend {
|
|||
services.AddTransient<IConfigurationBuilder, ConfigurationBuilder>()
|
||||
.AddDiscordCaching()
|
||||
.AddDiscordCommands(true)
|
||||
.AddPreparationErrorEvent<ErrorLoggingPreparationErrorEvent>()
|
||||
.AddPostExecutionEvent<ErrorLoggingPostExecutionEvent>()
|
||||
.AddInteractivity()
|
||||
.AddInteractionGroup<InteractionResponders>()
|
||||
|
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Handles error logging for slash command groups.
|
||||
/// </summary>
|
||||
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent {
|
||||
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
||||
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger) {
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a warning using the injected <see cref="ILogger" /> if the <paramref name="commandResult" /> has not
|
||||
/// succeeded.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the slash command. Unused.</param>
|
||||
/// <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(
|
||||
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());
|
||||
}
|
||||
}
|
62
Commands/ErrorLoggingPreparationErrorEvent.cs
Normal file
62
Commands/ErrorLoggingPreparationErrorEvent.cs
Normal file
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Handles error logging for slash commands that couldn't be successfully prepared.
|
||||
/// </summary>
|
||||
public class ErrorLoggingPreparationErrorEvent : IPreparationErrorEvent {
|
||||
private readonly ILogger<ErrorLoggingPreparationErrorEvent> _logger;
|
||||
|
||||
public ErrorLoggingPreparationErrorEvent(ILogger<ErrorLoggingPreparationErrorEvent> logger) {
|
||||
_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(
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles error logging for slash command groups.
|
||||
/// </summary>
|
||||
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent {
|
||||
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
||||
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger) {
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs a warning using the injected <see cref="ILogger" /> if the <paramref name="commandResult" /> has not
|
||||
/// succeeded.
|
||||
/// </summary>
|
||||
/// <param name="context">The context of the slash command. Unused.</param>
|
||||
/// <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(
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue