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)
5 Replies
• 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.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
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
The finished code now looks super clean https://github.com/JuanDelPueblo/discord-application-bot/blob/master/commands/forms/action/add.js
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.
Still a ton of work left to do for this bot, but hopefully I can finish before summer ends