How to differentiate between the possible errors when DMing a user?
I want to do send a DM to a user and handle the errors.
I know that there is more than one possible error, but not how to handle it. There is the one if the user has DMs disabled and one if user doesn't share any servers with the bot and one for Unknown user and also one for "Cannot send messages to this user".
How can I differentiate between those errors and what is even possible?
14 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPThe error codes should be different
yeah I've read https://discord.com/developers/docs/topics/opcodes-and-status-codes and don't actually know which error can be thrown 😅
at, least, not every one.
50007
is Cannot send messages to this user
But what else it possible?You can use this enum
Better for readability
That'd be a question for ddevs in #useful-servers since it's more an of an API question if you want to know all of the possible reasons a DM would fail
hm, okay yeah, makes sense
But you can use the enum above once you do have a list, yeah
I'll try to implement something and as well ask there for clarification.
That error covers many of the possibilites
DiscordAPIError: Cannot send messages to this user
Your bot is trying to send a DM to a user, but failed to do so:
- The user has DMs disabled or the bot blocked
- The user no longer shares a guild with the bot (make sure to send informational DMs before banning/kicking)
- The bot is trying to DM itself or another bot
Note: You cannot check if you can send a DM beforehand but have to handle the rejection caseindeed
What I'm trying to say is: There isn't a specific error code for if the person has DMs disabled or if they don't share a server with your app, so there's not really a way to differentiate those
yeah, I assumed that after reading this...
I think I'll just handle this specific case and show the raw error code otherwise
Yes, I can do that
ty