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 });
});
5 Replies
d.js toolkit
d.js toolkit17mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
sexy dark chocolate
Yeah the awaitMessages rejects with the collection of collected messages if you provided any reasons in the errors array and the collector ended for that reason. Only reasons I know of that djs stops the collector with are time and messageDelete, but messageDelete pertains to component collectors. Not sure why there's really an errors array to provide your own reasons since its promisified but there must be a reason why Oh wait, I think they provided that for IF you'd like the promise to reject, meaning if you'd like it to reject because of time itll reject with the collection of collected messages if the max hasn't been reached within the specific amount of time, if you provide no errors itll jus resolve the collection after the alotted time There we go, thats why the errors option exists. Incase you don't want the promise to reject for specific reasons or you wanrt it to reject for specific reasons. Like if you want it to error out if the channel was deleted but not if the time is up Well, that's my thoughts on why it exists, someone can correct me if Im wrong Why does it exist? O
JuanDelPueblo
JuanDelPuebloOP17mo ago
hmm, if I wrapped the entire switch case in a try-catch block and use await instead of then chaining, that seems like it would look cleaner and reduce repetition I understand now, I'll see how I can change it to better fit with the behavior I want
JuanDelPueblo
JuanDelPuebloOP17mo ago
GitHub
discord-application-bot/commands/forms/action/add.js at master · Ju...
Contribute to JuanDelPueblo/discord-application-bot development by creating an account on GitHub.
JuanDelPueblo
JuanDelPuebloOP17mo ago
Still a ton of work left to do for this bot, but hopefully I can finish before summer ends
Want results from more Discord servers?
Add your server