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
d.js toolkit
d.js toolkit2mo ago
- 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 OP
Mark
Mark2mo ago
The error codes should be different
LukeZ
LukeZOP2mo ago
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?
d.js docs
d.js docs2mo ago
:dtypes: v10: RESTJSONErrorCodes read more
NyR
NyR2mo ago
You can use this enum Better for readability
Mark
Mark2mo ago
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
LukeZ
LukeZOP2mo ago
hm, okay yeah, makes sense
Mark
Mark2mo ago
But you can use the enum above once you do have a list, yeah
LukeZ
LukeZOP2mo ago
I'll try to implement something and as well ask there for clarification.
TÆMBØ
TÆMBØ2mo ago
That error covers many of the possibilites
d.js docs
d.js docs2mo ago
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 case
LukeZ
LukeZOP2mo ago
indeed
TÆMBØ
TÆMBØ2mo ago
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
LukeZ
LukeZOP2mo ago
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
Want results from more Discord servers?
Add your server