JuanDelPueblo
JuanDelPueblo
DIAdiscord.js - Imagine an app
Created by JuanDelPueblo on 6/26/2023 in #djs-questions
Proper error handling with awaitMessages
I'm trying to optimize my code as best as possible, and this includes error handling. I understand how to catch an error with promises, but I am confused as how to pass it into .catch, because the catch arguments in the discord.js collectors guide only includes collected, so am I supposed to add a second argument for the error, or check if collected is an error? Also, in the errors parameter of awaitMessages, is 'time' the only one I can put, or are there others? Here is my current code for the bit I'm trying to optimize just in case (collecting role ID to create an action for a forms bot)
await interaction.reply({ content: `Please mention the role you would like to ${action === 'addrole' ? 'add' : 'remove'} with this action`, ephemeral: true })
.then(() => {
return interaction.channel.awaitMessages({ filter, max: 1, time: 60000, errors: ['time'] });
})
.then(collected => {
const role = collected.first().mentions.roles.first();
if (!role) {
interaction.followUp({ content: 'You did not mention a role!', ephemeral: true });
return;
}
Actions.create({
form_channel_id: currentForm.form_channel_id,
name: name,
when: when,
do: action,
role_id: role.id,
})
.then(() => {
interaction.followUp({ content: `The action ${name} has been added to this form!`, ephemeral: true });
})
.catch(() => {
interaction.followUp({ content: 'There was an error creating the action!', ephemeral: true });
});
})
.catch(() => {
interaction.followUp({ content: 'There was an error creating the action!', ephemeral: true });
});
await interaction.reply({ content: `Please mention the role you would like to ${action === 'addrole' ? 'add' : 'remove'} with this action`, ephemeral: true })
.then(() => {
return interaction.channel.awaitMessages({ filter, max: 1, time: 60000, errors: ['time'] });
})
.then(collected => {
const role = collected.first().mentions.roles.first();
if (!role) {
interaction.followUp({ content: 'You did not mention a role!', ephemeral: true });
return;
}
Actions.create({
form_channel_id: currentForm.form_channel_id,
name: name,
when: when,
do: action,
role_id: role.id,
})
.then(() => {
interaction.followUp({ content: `The action ${name} has been added to this form!`, ephemeral: true });
})
.catch(() => {
interaction.followUp({ content: 'There was an error creating the action!', ephemeral: true });
});
})
.catch(() => {
interaction.followUp({ content: 'There was an error creating the action!', ephemeral: true });
});
12 replies