fix: more changes to logging

This commit is contained in:
Marius Begby committed 2023-08-26 13:59:48 +02:00
1 parent 7ef666f03d
commit 9d9087d4c3
39 files changed
+403 -260

No files matched your search

+67 -51
View File
@@ -1,19 +1,29 @@
const logger = require('../../services/logger');
const config = require('config');
const embedOptions = config.get('embedOptions');
const botOptions = config.get('botOptions');
const { cannotSendMessageInChannel } = require('../../utils/validation/permissionValidator');
const { Events, EmbedBuilder } = require('discord.js');
const { v4: uuidv4 } = require('uuid');
module.exports = {
name: Events.InteractionCreate,
isDebug: false,
execute: async (interaction, { client }) => {
const executionId = uuidv4();
const logger = require('../../services/logger').child({
source: 'interactionCreate.js',
module: 'event',
name: 'interactionCreate',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
});
if (interaction.isMessageComponent()) {
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${
interaction.guild.id
}> Message component interaction created for id ${interaction.customId}${
logger.debug(
`Message component interaction created for id ${interaction.customId}${
interaction.values ? ' and values ' + interaction.values : ''
}.`
);
@@ -22,32 +32,30 @@ module.exports = {
const command = client.commands.get(interaction.commandName);
if (!command) {
logger.warn(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction created but command '${interaction.commandName}' was not found.`
);
logger.warn(`Interaction created but command '${interaction.commandName}' was not found.`);
return;
}
if (interaction.isAutocomplete()) {
logger.debug(`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete created.`);
logger.debug('Autocomplete interaction created.');
try {
await command.autocomplete({ interaction, client });
await command.autocomplete({ interaction, executionId });
} catch (error) {
if (
error.message === 'Unknown interaction' ||
error.message === 'Interaction has already been acknowledged.'
) {
logger.debug(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Autocomplete failed to be responded to, unknown interaction or already acknowledged.`
'Autocomplete failed to be responded to, unknown interaction or already acknowledged.'
);
return;
} else {
logger.warn(error, `[Shard ${interaction.guild.shardId}] Autocomplete failed to execute.`);
logger.warn(error, 'Autocomplete failed to execute.');
}
return;
}
} else if (interaction.isChatInputCommand()) {
logger.debug(`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction created.`);
logger.debug(`Chat input command interaction created for ${interaction.commandName}.`);
try {
await interaction.deferReply();
@@ -56,62 +64,70 @@ module.exports = {
}
const inputTime = new Date();
await command.execute({ interaction, client });
await command.execute({ interaction, client, executionId });
const outputTime = new Date();
const executionTime = outputTime - inputTime;
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.`
`Command '${interaction}' executed in ${executionTime} ms.`
);
} catch (error) {
logger.warn(
error,
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' throwed an unhandled error and might have failed to execute properly.`
`Command '${interaction}' throwed an unhandled error and might have failed to execute properly.`
);
if (!interaction.deferred || !interaction.replied) {
logger.warn(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction was not deferred or replied to, and an error was thrown.`
);
if (
error.message === 'Unknown interaction' ||
error.message === 'Interaction has already been acknowledged.'
) {
logger.debug('Command failed to be responded to, unknown interaction or already acknowledged.');
return;
}
if (interaction.replied) {
// If the interaction has already been replied to, most likely command executed successfully or error is already handled.
return;
} else if (interaction.deferred) {
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction was deferred, editing reply and sending Uh-oh message.`
);
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._`
)
.setColor(embedOptions.colors.error)
]
});
} else {
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction was not deferred or replied, sending new reply with Uh-oh message.`
);
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._`
)
.setColor(embedOptions.colors.error)
]
});
if (!interaction.deferred || !interaction.replied) {
logger.warn(
'Interaction was not deferred or replied to, and an error was thrown.'
);
return;
}
if (interaction.replied) {
// If the interaction has already been replied to, most likely command executed successfully or error is already handled.
return;
} else if (interaction.deferred) {
logger.info(
'Interaction was deferred, editing reply and sending Uh-oh message.'
);
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._`
)
.setColor(embedOptions.colors.error)
]
});
} else {
logger.info(
'Interaction was not deferred or replied, sending new reply with Uh-oh message.'
);
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.error} Uh-oh... _Something_ went wrong!**\nThere was an unexpected error while trying to execute this command.\n\nYou can try to perform the command again.\n\n_If this problem persists, please submit a bug report in the **[support server](${botOptions.serverInviteUrl})**._`
)
.setColor(embedOptions.colors.error)
]
});
}
}
}
} else {
logger.warn(
interaction,
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Interaction created but was not a chat input or autocomplete interaction.`
'Interaction created but was not a chat input or autocomplete interaction.'
);
}
}