forked from TeamInklings/Octobot
Notify user when a command execution error occurs (#171)
With this PR, whenever command execution fails, the user will get an error message with details of the error that can be passed on to developers An unrelated minor change: errors caused by task cancellations will no longer be logged --------- Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com> Signed-off-by: mctaylors <mctaylxrs@outlook.com>
This commit is contained in:
parent
5b84c8d8d0
commit
fb3aebb1e0
6 changed files with 74 additions and 3 deletions
|
@ -570,4 +570,10 @@
|
|||
<data name="MessagesClearedFiltered" xml:space="preserve">
|
||||
<value>Cleared {0} messages from {1}</value>
|
||||
</data>
|
||||
<data name="CommandExecutionFailed" xml:space="preserve">
|
||||
<value>An error occurred during command execution, try again later.</value>
|
||||
</data>
|
||||
<data name="ContactDevelopers" xml:space="preserve">
|
||||
<value>Contact the developers if the problem occurs again.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -570,4 +570,10 @@
|
|||
<data name="MessagesClearedFiltered" xml:space="preserve">
|
||||
<value>Очищено {0} сообщений от {1}</value>
|
||||
</data>
|
||||
<data name="CommandExecutionFailed" xml:space="preserve">
|
||||
<value>Произошла ошибка при выполнении команды, повтори попытку позже.</value>
|
||||
</data>
|
||||
<data name="ContactDevelopers" xml:space="preserve">
|
||||
<value>Обратись к разработчикам, если проблема возникнет снова.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -570,4 +570,10 @@
|
|||
<data name="MessagesClearedFiltered" xml:space="preserve">
|
||||
<value>вырезано {0} забавных сообщений от {1}</value>
|
||||
</data>
|
||||
<data name="CommandExecutionFailed" xml:space="preserve">
|
||||
<value>произошёл тотальный разнос в команде, удачи.</value>
|
||||
</data>
|
||||
<data name="ContactDevelopers" xml:space="preserve">
|
||||
<value>если ты это читаешь второй раз за сегодня, пиши разрабам</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
using JetBrains.Annotations;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Octobot.Extensions;
|
||||
using Remora.Discord.API.Abstractions.Rest;
|
||||
using Remora.Discord.Commands.Contexts;
|
||||
using Remora.Discord.Commands.Feedback.Services;
|
||||
using Remora.Discord.Commands.Services;
|
||||
using Remora.Discord.Extensions.Embeds;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Octobot.Commands.Events;
|
||||
|
@ -14,10 +18,15 @@ namespace Octobot.Commands.Events;
|
|||
public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
|
||||
{
|
||||
private readonly ILogger<ErrorLoggingPostExecutionEvent> _logger;
|
||||
private readonly FeedbackService _feedback;
|
||||
private readonly IDiscordRestUserAPI _userApi;
|
||||
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger)
|
||||
public ErrorLoggingPostExecutionEvent(ILogger<ErrorLoggingPostExecutionEvent> logger, FeedbackService feedback,
|
||||
IDiscordRestUserAPI userApi)
|
||||
{
|
||||
_logger = logger;
|
||||
_feedback = feedback;
|
||||
_userApi = userApi;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -28,11 +37,34 @@ public class ErrorLoggingPostExecutionEvent : IPostExecutionEvent
|
|||
/// <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(
|
||||
public async Task<Result> AfterExecutionAsync(
|
||||
ICommandContext context, IResult commandResult, CancellationToken ct = default)
|
||||
{
|
||||
_logger.LogResult(commandResult, $"Error in slash command execution for /{context.Command.Command.Node.Key}.");
|
||||
|
||||
return Task.FromResult(Result.FromSuccess());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,11 @@ public static class LoggerExtensions
|
|||
|
||||
if (result.Error is ExceptionError exe)
|
||||
{
|
||||
if (exe.Exception is TaskCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogError(exe.Exception, "{ErrorMessage}", message);
|
||||
return;
|
||||
}
|
||||
|
|
16
src/Messages.Designer.cs
generated
16
src/Messages.Designer.cs
generated
|
@ -996,5 +996,21 @@ namespace Octobot {
|
|||
return ResourceManager.GetString("MessagesClearedFiltered", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string CommandExecutionFailed
|
||||
{
|
||||
get
|
||||
{
|
||||
return ResourceManager.GetString("CommandExecutionFailed", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string ContactDevelopers
|
||||
{
|
||||
get
|
||||
{
|
||||
return ResourceManager.GetString("ContactDevelopers", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue