Respond to interaction with error feedback on command handler fail

This commit is contained in:
2024-01-28 04:23:29 -06:00
parent 5347c07b0c
commit dcf51986a7
2 changed files with 11 additions and 3 deletions

View File

@@ -307,18 +307,19 @@ func DumpResponse(res *http.Response) {
// ResponseError responds to an interaction with an error message // ResponseError responds to an interaction with an error message
// TODO: Improve with a proper embed and colors // TODO: Improve with a proper embed and colors
func RespondError(session *discordgo.Session, interaction *discordgo.Interaction, message string, err error) { func RespondError(session *discordgo.Session, interaction *discordgo.Interaction, message string, err error) error {
// Optional: log the error // Optional: log the error
if err != nil { if err != nil {
log.Err(err).Msg(message) log.Err(err).Msg(message)
} }
session.InteractionRespond(interaction, &discordgo.InteractionResponse{ return session.InteractionRespond(interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: message, Content: message,
}, },
}) })
} }
func GetFooter(time time.Time) *discordgo.MessageEmbedFooter { func GetFooter(time time.Time) *discordgo.MessageEmbedFooter {

View File

@@ -158,11 +158,18 @@ func main() {
// Call handler // Call handler
err := handler(internalSession, interaction) err := handler(internalSession, interaction)
// Log error // Log & respond error
if err != nil { if err != nil {
// TODO: Find a way to merge the response with the handler's error // TODO: Find a way to merge the response with the handler's error
log.Error().Str("commandName", name).Err(err).Msg("Command Handler Error") log.Error().Str("commandName", name).Err(err).Msg("Command Handler Error")
// Respond with error
err = RespondError(internalSession, interaction.Interaction, fmt.Sprintf("Unexpected Error: %s", err.Error()), nil)
if err != nil {
log.Error().Str("commandName", name).Err(err).Msg("Failed to respond with error feedback")
} }
}
} else { } else {
log.Error().Str("commandName", name).Msg("Command Interaction Has No Handler") log.Error().Str("commandName", name).Msg("Command Interaction Has No Handler")