fix: update interactionCreate event handling

This commit is contained in:
Marius Begby committed 2023-09-14 12:52:48 +02:00
1 parent 36f852c91c
commit 7902a8427c
1 file changed
+45 -43
+45 -43
View File
@@ -32,26 +32,46 @@ module.exports = {
logger.debug('Interaction received.');
let interactionIdentifier: string = 'Unknown';
if (
interaction.type === InteractionType.ApplicationCommand ||
interaction.type === InteractionType.ApplicationCommandAutocomplete
) {
interactionIdentifier = (interaction as ChatInputCommandInteraction).commandName;
} else if (interaction.type === InteractionType.MessageComponent) {
interactionIdentifier = (interaction as MessageComponentInteraction).customId;
}
const interactionIdentifier: string = getInteractionIdentifier(interaction);
try {
logger.debug('Started handling interaction.');
await handleInteraction(interaction, client, executionId, interactionIdentifier);
} catch (error) {
logger.debug('Error while handling received interaction.');
await handleError(interaction, error as CustomError, executionId, interactionIdentifier);
}
const executionTime: number = new Date().getTime() - inputTime;
const interactionType: string = InteractionType[interaction.type];
logger.info(
{
executionTime: executionTime,
interactionType: interactionType
},
`${interactionType} interaction '${interactionIdentifier}' successfully handled in ${executionTime} ms.`
);
if (executionTime > 10000) {
logger.warn(
`${interactionType} interaction '${interactionIdentifier}' took ${executionTime} ms to execute.`
);
}
return;
}
};
async function handleInteraction(
interaction: Interaction,
client: ExtendedClient,
executionId: string,
interactionIdentifier: string
) {
switch (interaction.type) {
case InteractionType.ApplicationCommand:
await handleCommand(
interaction as ChatInputCommandInteraction,
client,
executionId,
interactionIdentifier
);
await handleCommand(interaction as ChatInputCommandInteraction, client, executionId, interactionIdentifier);
break;
case InteractionType.ApplicationCommandAutocomplete:
@@ -73,36 +93,18 @@ module.exports = {
break;
default:
logger.error(
interaction,
`Interaction of type '${interaction.type}' was not handled, interaction attached as object.`
);
break;
throw new Error(`Interaction of type '${interaction.type}' was not handled`);
}
} catch (error) {
logger.debug('Error while handling received interaction.');
await handleError(interaction, error as CustomError, executionId, interactionIdentifier);
}
const outputTime: number = new Date().getTime();
const executionTime: number = outputTime - inputTime;
const interactionType: string = InteractionType[interaction.type];
logger.info(
{
executionTime: executionTime,
interactionType: interactionType
},
`${interactionType} interaction '${interactionIdentifier}' successfully handled in ${executionTime} ms.`
);
if (executionTime > 10000) {
logger.warn(
`${interactionType} interaction '${interactionIdentifier}' took ${executionTime} ms to execute.`
);
function getInteractionIdentifier(interaction: Interaction): string {
if (
interaction.type === InteractionType.ApplicationCommand ||
interaction.type === InteractionType.ApplicationCommandAutocomplete
) {
return (interaction as ChatInputCommandInteraction).commandName;
} else if (interaction.type === InteractionType.MessageComponent) {
return (interaction as MessageComponentInteraction).customId;
}
return;
return 'Unknown';
}
};