2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-10-12 18:37:25 +03:00
|
|
|
using Octobot.Extensions;
|
2023-10-26 17:54:15 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Contexts;
|
2023-10-26 17:54:15 +03:00
|
|
|
using Remora.Discord.Commands.Feedback.Services;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Services;
|
2023-10-26 17:54:15 +03:00
|
|
|
using Remora.Discord.Extensions.Embeds;
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.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-10-26 17:54:15 +03:00
|
|
|
private readonly FeedbackService _feedback;
|
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-10-26 17:54:15 +03:00
|
|
|
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger, FeedbackService feedback,
|
|
|
|
IDiscordRestUserAPI userApi)
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-09 16:32:14 +03:00
|
|
|
_logger = logger;
|
2023-10-26 17:54:15 +03:00
|
|
|
_feedback = feedback;
|
|
|
|
_userApi = userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2023-10-26 17:54:15 +03:00
|
|
|
public async 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
|
|
|
|
2023-10-26 17:54:15 +03:00
|
|
|
var result = commandResult;
|
|
|
|
while (result.Inner is not null)
|
|
|
|
{
|
|
|
|
result = result.Inner;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
{
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(ct);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
|
|
|
{
|
|
|
|
return Result.FromError(botResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.CommandExecutionFailed, bot)
|
|
|
|
.WithDescription(Markdown.InlineCode(result.Error.Message))
|
|
|
|
.WithFooter(Messages.ContactDevelopers)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
return await _feedback.SendContextualEmbedResultAsync(embed, ct);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|