mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-05 06:28:52 +00:00
fix: update interactionCreate event handling
This commit is contained in:
1 parent
36f852c91c
commit
7902a8427c
1 file changed
+48
-46
@@ -32,26 +32,46 @@ module.exports = {
|
|||||||
|
|
||||||
logger.debug('Interaction received.');
|
logger.debug('Interaction received.');
|
||||||
|
|
||||||
let interactionIdentifier: string = 'Unknown';
|
const interactionIdentifier: string = getInteractionIdentifier(interaction);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.debug('Started handling interaction.');
|
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) {
|
switch (interaction.type) {
|
||||||
case InteractionType.ApplicationCommand:
|
case InteractionType.ApplicationCommand:
|
||||||
await handleCommand(
|
await handleCommand(interaction as ChatInputCommandInteraction, client, executionId, interactionIdentifier);
|
||||||
interaction as ChatInputCommandInteraction,
|
|
||||||
client,
|
|
||||||
executionId,
|
|
||||||
interactionIdentifier
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case InteractionType.ApplicationCommandAutocomplete:
|
case InteractionType.ApplicationCommandAutocomplete:
|
||||||
@@ -73,36 +93,18 @@ module.exports = {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logger.error(
|
throw new Error(`Interaction of type '${interaction.type}' was not handled`);
|
||||||
interaction,
|
|
||||||
`Interaction of type '${interaction.type}' was not handled, interaction attached as object.`
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
}
|
||||||
logger.debug('Error while handling received interaction.');
|
|
||||||
await handleError(interaction, error as CustomError, executionId, interactionIdentifier);
|
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 'Unknown';
|
||||||
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.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in new issue
Block a user