fix: adjustments to error and permission handling

This commit is contained in:
Marius Begby committed 2023-08-18 08:08:48 +02:00
1 parent 3e213d4f8a
commit 6bcc489e84
2 files changed
+44 -27

No files matched your search

+22 -13
View File
@@ -36,34 +36,32 @@ 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;
}
@@ -71,6 +69,9 @@ module.exports = {
// 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()
@@ -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()
@@ -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.`
);
}
}
};
+22 -14
View File
@@ -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;
}