mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-04 06:28:53 +00:00
fix: adjustments to error and permission handling
This commit is contained in:
1 parent
3e213d4f8a
commit
6bcc489e84
2 files changed
+44
-27
No files matched your search
@@ -36,34 +36,32 @@ module.exports = {
|
|||||||
} else if (interaction.isChatInputCommand()) {
|
} else if (interaction.isChatInputCommand()) {
|
||||||
logger.debug(`[Shard ${interaction.guild.shardId}] Interaction created.`);
|
logger.debug(`[Shard ${interaction.guild.shardId}] Interaction created.`);
|
||||||
try {
|
try {
|
||||||
|
await interaction.deferReply();
|
||||||
|
|
||||||
|
// causing issues with unknown interaction error?
|
||||||
if (await cannotSendMessageInChannel(interaction)) {
|
if (await cannotSendMessageInChannel(interaction)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const inputTime = new Date();
|
|
||||||
|
|
||||||
await interaction.deferReply();
|
const inputTime = new Date();
|
||||||
await command.execute({ interaction, client });
|
await command.execute({ interaction, client });
|
||||||
|
|
||||||
const outputTime = new Date();
|
const outputTime = new Date();
|
||||||
const executionTime = outputTime - inputTime;
|
const executionTime = outputTime - inputTime;
|
||||||
|
|
||||||
if (executionTime > 20000) {
|
logger.info(
|
||||||
logger.warn(
|
`[Shard ${interaction.guild.shardId}] Guild ${interaction.guild.id}> Command '${interaction}' executed in ${executionTime} ms.`
|
||||||
`[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.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
error,
|
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) {
|
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;
|
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.
|
// If the interaction has already been replied to, most likely command executed successfully or error is already handled.
|
||||||
return;
|
return;
|
||||||
} else if (interaction.deferred) {
|
} else if (interaction.deferred) {
|
||||||
|
logger.info(
|
||||||
|
`[Shard ${interaction.guild.shardId}] Interaction was deferred, editing reply and sending Uh-oh message.`
|
||||||
|
);
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
new EmbedBuilder()
|
||||||
@@ -81,6 +82,9 @@ module.exports = {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
logger.info(
|
||||||
|
`[Shard ${interaction.guild.shardId}] Interaction was not deferred or replied, sending new reply with Uh-oh message.`
|
||||||
|
);
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
embeds: [
|
embeds: [
|
||||||
new EmbedBuilder()
|
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.`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -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
|
// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
|
||||||
if (!channel.viewable) {
|
if (!channel.viewable) {
|
||||||
// we can still send ephemeral replies in channels we can't view, so sending message to user instead
|
logger.info(
|
||||||
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(
|
|
||||||
`User tried to use command ${interaction.commandName} but the bot had no permission to send reply in text channel.`
|
`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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user