feat: handle CataloggerError results in interaction responder

This commit is contained in:
sam 2025-03-20 15:24:46 +01:00
parent 8a4e3ff184
commit 24f6aee57d
Signed by: sam
GPG key ID: 5F3C3C1B3166639D

View file

@ -19,17 +19,21 @@ using Remora.Commands.Services;
using Remora.Commands.Tokenization;
using Remora.Commands.Trees;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Responders;
using Remora.Discord.Commands.Services;
using Remora.Discord.Gateway.Responders;
using Remora.Rest.Core;
using Remora.Results;
using Serilog.Context;
namespace Catalogger.Backend.Bot.Responders;
/// <summary>
/// Wrapper for Remora.Discord's default interaction responder, that ignores all events if test mode is enabled.
/// Wrapper for Remora.Discord's default interaction responder, that ignores all events if test mode is enabled,
/// and handles <see cref="CataloggerError" /> results returned by commands.
/// </summary>
public class CustomInteractionResponder(
Config config,
@ -87,6 +91,38 @@ public class CustomInteractionResponder(
true
);
return await _inner.RespondAsync(evt, ct);
var result = await _inner.RespondAsync(evt, ct);
if (result.Error is not CataloggerError cataloggerError)
return result;
return await interactionAPI.CreateInteractionResponseAsync(
evt.ID,
evt.Token,
new InteractionResponse(
Type: InteractionCallbackType.ChannelMessageWithSource,
Data: new Optional<OneOf.OneOf<
IInteractionMessageCallbackData,
IInteractionAutocompleteCallbackData,
IInteractionModalCallbackData
>>(
new InteractionMessageCallbackData(
Embeds: new Optional<IReadOnlyList<IEmbed>>(
[
new Embed(
Colour: DiscordUtils.Red,
Title: "Something went wrong",
Description: $"""
Something went wrong while running this command.
> {cataloggerError.Message}
Please try again later.
"""
),
]
)
)
)
),
ct: ct
);
}
}