Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: Updated handling of interaction errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 31, 2023
1 parent 8a0ade3 commit da919d1
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/handlers/interactionErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
EmbedBuilder,
Interaction,
InteractionReplyOptions,
InteractionType,
MessageComponentInteraction
} from 'discord.js';
import { Logger } from 'pino';
Expand Down Expand Up @@ -39,48 +40,53 @@ export const handleError = async (
]
};

logger.error({ error, interaction }, `Error handling interaction '${interactionIdentifier}'`);
logger.debug(error, `Error handling interaction '${interactionIdentifier}' with execution ID ${executionId}`);

if (interaction instanceof ChatInputCommandInteraction && interaction.deferred) {
if (interaction instanceof ChatInputCommandInteraction) {
switch (interaction.replied) {
case true:
logger.warn(error, `Interaction '${interaction.id}' threw an error but has already been replied to.`);
logger.warn(
error,
`Interaction '${interactionIdentifier}' threw an error but has already been replied to.`
);
return;
case false:
logger.debug('Responding with error embed');
return await interaction.editReply(errorReply);
}
} else if (interaction instanceof MessageComponentInteraction && interaction.deferred) {
} else if (interaction instanceof MessageComponentInteraction) {
switch (interaction.replied) {
case true:
logger.warn(error, `Interaction '${interaction.id}' threw an error but has already been replied to.`);
logger.warn(
error,
`Interaction '${interactionIdentifier}' threw an error but has already been replied to.`
);
return;
case false:
logger.debug('Responding with error embed');
return await interaction.editReply(errorReply);
}
} else {
logger.warn(
'Interaction threw an error but was not deferred or replied to, or was an autocomplete interaction. Cannot send error reply.'
logger.debug(
`${
InteractionType[interaction.type]
} interaction '${interactionIdentifier}' threw an error. Cannot send error reply.`
);

if (
error.code === 'InteractionCollectorError' ||
error.message === 'Collector received no interactions before ending with reason: time'
) {
logger.debug('Interaction response timed out.');
if (error.message === 'Unknown interaction') {
logger.debug('Interaction no longer exists, timed out or has already been responded to.');
return;
}

if (error.message === 'Unknown interaction') {
logger.debug('Interaction has already been responded to or does no longer exist.');
if (error.message === 'Collector received no interactions before ending with reason: time') {
logger.debug('Interaction collector response timed out.');
return;
}

logger.fatal(
error,
`Unhandled error while awaiting or handling component interaction. Execution ID: ${executionId}`
);
return;
}

logger.fatal(
error,
`Unhandled error while handling interaction '${interactionIdentifier}'. Execution ID: ${executionId}`
);
return;
};

0 comments on commit da919d1

Please sign in to comment.