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

Commit

Permalink
fix: adjustments to error and permission handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 18, 2023
1 parent 3e213d4 commit 6bcc489
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
35 changes: 22 additions & 13 deletions src/events/interactions/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,42 @@ module.exports = {
} else if (interaction.isChatInputCommand()) {
logger.debug(`[Shard ${interaction.guild.shardId}] Interaction created.`);
try {
await interaction.deferReply();

// causing issues with unknown interaction error?
if (await cannotSendMessageInChannel(interaction)) {
return;
}
const inputTime = new Date();

await interaction.deferReply();
const inputTime = new Date();
await command.execute({ interaction, client });

const outputTime = new Date();
const executionTime = outputTime - inputTime;

if (executionTime > 20000) {
logger.warn(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' took ${executionTime} ms to execute.`
);
} else {
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.`
);
}
logger.info(
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.`
);
} catch (error) {
logger.warn(
error,
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' throwed and error and might have failed to execute properly.`
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' throwed an unhandled error and might have failed to execute properly.`
);

if (!interaction.deferred || !interaction.replied) {
logger.warn(error, 'Interaction was not deferred or replied to, and an error was thrown.');
logger.warn(
`[Shard ${interaction.guild.shardId}] 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(
`[Shard ${interaction.guild.shardId}] Interaction was deferred, editing reply and sending Uh-oh message.`
);
await interaction.editReply({
embeds: [
new EmbedBuilder()
Expand All @@ -81,6 +82,9 @@ module.exports = {
]
});
} else {
logger.info(
`[Shard ${interaction.guild.shardId}] Interaction was not deferred or replied, sending new reply with Uh-oh message.`
);
await interaction.reply({
embeds: [
new EmbedBuilder()
Expand All @@ -92,6 +96,11 @@ module.exports = {
});
}
}
} else {
logger.warn(
interaction,
`[Shard ${interaction.guild.shardId}] Interaction created but was not a chat input or autocomplete interaction.`
);
}
}
};
36 changes: 22 additions & 14 deletions src/utils/validation/permissionValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,30 @@ exports.cannotSendMessageInChannel = async (interaction) => {

// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
if (!channel.viewable) {
// we can still send ephemeral replies in channels we can't view, so sending message to user instead
await interaction.deferReply({ ephemeral: true });
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel you are in.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.`
)
.setColor(embedOptions.colors.warning)
],
ephemeral: true
});

logger.debug(
logger.info(
`User tried to use command ${interaction.commandName} but the bot had no permission to send reply in text channel.`
);

try {
// we can still send ephemeral replies in channels we can't view, so sending message to user instead
await interaction.deferReply({ ephemeral: true });
await interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription(
`**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel you are in.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.`
)
.setColor(embedOptions.colors.warning)
],
ephemeral: true
});
} catch (error) {
logger.error(
error,
'Failed to send ephemeral reply to user in channel that bot cannot view/send message in.'
);
}

return true;
}

Expand Down

0 comments on commit 6bcc489

Please sign in to comment.