Meko
Meko

Reputation: 35

How can I fix `DiscordAPIError: Cannot send messages to this user`?

I'm making a new command that sends the author a message, however, when the author's DMs is closed it's giving me the error DiscordAPIError: Cannot send messages to this user

    if (!message.author.send) {
        return message.channel.send("DMs closed.");
    } else {
        message.author.send("DMs opened")
    }

Upvotes: 0

Views: 1606

Answers (2)

Hao C.
Hao C.

Reputation: 320

Well, as you said the DM's for the author is closed, so it doesn't work. You can try something like this:


    //Put the messages you wanted to send the author's DM's
    //Add this below it
    .catch(error => {
          console.error(
            `Could not send help DM to ${message.author.tag}.\n`,
            error
          );
          message.reply("it seems like I can't DM you! Do you have DMs disabled?");
     });

     //If the user has the DM's turned off, then it'll send this in the channel and `console.log` the error

Upvotes: 1

Guaxinim
Guaxinim

Reputation: 304

the send function will always exists, even if the user dm is closed or open, to check if the dm is closed you can use .catch to catch the DiscordAPIError error.
See: Promise.catch

Try this:

message.author.send("DMs opened").catch(error => message.channel.send("DMs closed"))

Upvotes: 0

Related Questions