2023-07-18 15:25:02 +03:00
|
|
|
using JetBrains.Annotations;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-12-17 21:35:09 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Objects;
|
2023-10-26 17:54:15 +03:00
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
2023-12-17 21:35:09 +03:00
|
|
|
using Remora.Discord.API.Objects;
|
2023-07-09 16:32:14 +03:00
|
|
|
using Remora.Discord.Commands.Contexts;
|
2023-12-17 21:35:09 +03:00
|
|
|
using Remora.Discord.Commands.Feedback.Messages;
|
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;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Extensions;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.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-11-04 21:28:22 +03:00
|
|
|
private readonly IFeedbackService _feedback;
|
2024-03-21 18:55:34 +03:00
|
|
|
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
2023-10-26 17:54:15 +03:00
|
|
|
private readonly IDiscordRestUserAPI _userApi;
|
2023-07-09 16:32:14 +03:00
|
|
|
|
2023-11-04 21:28:22 +03:00
|
|
|
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger, IFeedbackService feedback,
|
2023-10-26 17:54:15 +03:00
|
|
|
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)
|
|
|
|
{
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-10-26 17:54:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var botResult = await _userApi.GetCurrentUserAsync(ct);
|
|
|
|
if (!botResult.IsDefined(out var bot))
|
|
|
|
{
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(botResult);
|
2023-10-26 17:54:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var embed = new EmbedBuilder().WithSmallTitle(Messages.CommandExecutionFailed, bot)
|
|
|
|
.WithDescription(Markdown.InlineCode(result.Error.Message))
|
|
|
|
.WithFooter(Messages.ContactDevelopers)
|
|
|
|
.WithColour(ColorsList.Red)
|
|
|
|
.Build();
|
|
|
|
|
2023-12-17 21:35:09 +03:00
|
|
|
var issuesButton = new ButtonComponent(
|
|
|
|
ButtonComponentStyle.Link,
|
2024-03-20 15:59:25 +03:00
|
|
|
BuildInfo.IsDirty
|
|
|
|
? Messages.ButtonDirty
|
|
|
|
: Messages.ButtonReportIssue,
|
2024-04-01 15:57:49 +03:00
|
|
|
new PartialEmoji(Name: "\u26a0\ufe0f"), // 'WARNING SIGN' (U+26A0)
|
2024-03-20 15:59:25 +03:00
|
|
|
URL: BuildInfo.IssuesUrl,
|
|
|
|
IsDisabled: BuildInfo.IsDirty
|
2023-12-17 21:35:09 +03:00
|
|
|
);
|
|
|
|
|
2024-03-20 21:08:16 +03:00
|
|
|
return ResultExtensions.FromError(await _feedback.SendContextualEmbedResultAsync(embed,
|
2023-12-17 21:35:09 +03:00
|
|
|
new FeedbackMessageOptions(MessageComponents: new[]
|
|
|
|
{
|
|
|
|
new ActionRowComponent(new[] { issuesButton })
|
2024-03-20 21:08:16 +03:00
|
|
|
}), ct)
|
|
|
|
);
|
2023-07-09 16:32:14 +03:00
|
|
|
}
|
|
|
|
}
|