Skip to content

Commit

Permalink
fix: catch missing role errors (closes #518)
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Mar 3, 2024
1 parent f4ff013 commit c09972f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 157 deletions.
10 changes: 9 additions & 1 deletion src/i18n/en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ misc:
Sorry, an unexpected error occurred.
Please give this information to an administrator.
fields:
code: Error code
identifier: Identifier
title: ⚠️ Something went wrong
expired:
Expand Down Expand Up @@ -375,6 +374,15 @@ misc:
ratelimited:
description: Try again in a few seconds.
title: 🐢 Please slow down
role_error:
description: Sorry, the operation failed because of an invalid configuration.
fields:
for_admins:
name: For server administrators
value: >
An invalid user or role was supplied, which usually means a staff role has been deleted.
[Click here]({url}) for resolution instructions.
title: ⚠️ Something went wrong
unknown_category:
description: Please try a different category.
title: ❌ That ticket category doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ process.on('uncaughtException', (error, origin) => {
log.error(error);
});

process.on('warning', warning => log.warn(warning.stack));
process.on('warning', warning => log.warn(warning.stack || warning));

const client = new Client(config, log);
client.login().then(() => {
Expand Down
80 changes: 80 additions & 0 deletions src/lib/error.js
Original file line number Diff line number Diff line change
@@ -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));
};
4 changes: 2 additions & 2 deletions src/lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 3 additions & 38 deletions src/listeners/buttons/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');
const { handleInteractionError } = require('../../lib/error');

module.exports = class extends Listener {
constructor(client, options) {
Expand All @@ -14,38 +10,7 @@ module.exports = class extends Listener {
});
}

async run({
button,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.buttons(ref);
this.client.log.error.buttons(`"${button.id}" button execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
async run(...params) {
return handleInteractionError(...params);
}
};
41 changes: 3 additions & 38 deletions src/listeners/commands/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');
const { handleInteractionError } = require('../../lib/error');

module.exports = class extends Listener {
constructor(client, options) {
Expand All @@ -14,38 +10,7 @@ module.exports = class extends Listener {
});
}

async run({
command,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.commands(ref);
this.client.log.error.commands(`"${command.name}" command execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
async run(...params) {
return handleInteractionError(...params);
}
};
41 changes: 3 additions & 38 deletions src/listeners/menus/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');
const { handleInteractionError } = require('../../lib/error');

module.exports = class extends Listener {
constructor(client, options) {
Expand All @@ -14,38 +10,7 @@ module.exports = class extends Listener {
});
}

async run({
menu,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.menus(ref);
this.client.log.error.menus(`"${menu.id}" menu execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
async run(...params) {
return handleInteractionError(...params);
}
};
42 changes: 3 additions & 39 deletions src/listeners/modals/error.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');

const { handleInteractionError } = require('../../lib/error');
module.exports = class extends Listener {
constructor(client, options) {
super(client, {
Expand All @@ -14,38 +9,7 @@ module.exports = class extends Listener {
});
}

async run({
modal,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.modals(ref);
this.client.log.error.modals(`"${modal.id}" modal execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
async run(...params) {
return handleInteractionError(...params);
}
};

0 comments on commit c09972f

Please sign in to comment.