fix: catch missing role errors (closes #518)

This commit is contained in:
Isaac
2024-03-03 20:10:45 +00:00
parent f4ff01398e
commit c09972f3cf
8 changed files with 104 additions and 157 deletions

80
src/lib/error.js Normal file
View File

@@ -0,0 +1,80 @@
const { getSUID } = require('./logging');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
/**
*
* @param {Object} event
* @param {import("discord.js").Interaction<"cached">} event.interaction
* @param {Error} event.error
* @returns
*/
module.exports.handleInteractionError = async event => {
const {
interaction,
error,
} = event;
const { client } = interaction;
const ref = getSUID();
client.log.error.buttons(ref);
if (interaction.isAnySelectMenu()) {
client.log.error.menus(`"${event.menu.id}" menu execution error:`, error);
} else if (interaction.isButton()) {
client.log.error.buttons(`"${event.button.id}" button execution error:`, error);
} else if (interaction.isModalSubmit()) {
client.log.error.modals(`"${event.modal.id}" modal execution error:`, error);
} else if (interaction.isCommand()) {
client.log.error.commands(`"${event.command.name}" command execution error:`, error);
}
let locale = null;
if (interaction.guild) {
locale = (await client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [],
};
if (/Supplied parameter is not a User nor a Role/.test(error.message)) {
data.embeds.push(
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.role_error.title'))
.setDescription(getMessage('misc.role_error.description'))
.addFields([
{
name: getMessage('misc.role_error.fields.for_admins.name'),
value: getMessage('misc.role_error.fields.for_admins.value', { url: 'https://discordtickets.app/self-hosting/troubleshooting/#invalid-user-or-role' }),
},
]),
);
} else {
data.embeds.push(
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(ref),
},
]),
);
}
return interaction.reply(data).catch(() => interaction.editReply(data));
};

View File

@@ -77,12 +77,12 @@ module.exports.sendToHouston = async client => {
client.log.success('Posted client stats');
client.log.debug(res);
} catch (res) {
client.log.warn('The following error is not important and can be safely ignored');
try {
const json = await res.json();
client.log.error('An error occurred whilst posting stats:', json);
} catch (error) {
client.log.error('An error occurred whilst posting stats and the response couldn\'t be parsed');
client.log.error(error.message);
client.log.error('An error occurred whilst posting stats and the response couldn\'t be parsed:', error.message);
}
client.log.debug(res);
}