Bot failing to deliver private messages to users

I’m working on a Discord bot that should send private messages when users type certain keywords. Here’s my current implementation:

botClient.on("messageCreate", async (msg) => {
  if (msg.content.includes("trigger phrase")) {
    msg.author.send({embeds: [customEmbed]});
  }
});

When I test this code, I keep getting an error response that looks like this:

{
  "rawError": {
    "message": "Cannot send messages to this user",
    "code": 50007
  },
  "code": 50007,
  "status": 403,
  "method": "POST"
}

The bot should send an embed with information directly to the user’s private messages when they use the trigger phrase. What could be causing this issue and how can I fix it?

yeah this happend to me too. the user probably has their dm settings locked down or blocked your bot. try adding a fallback like replying in the same channel if the dm fails. also check if the user actualy shares a server with your bot cause that matters for permissions

The 50007 error indicates that the user’s privacy settings prevent direct messages from non-friends or members of shared servers. I faced this situation while creating a bot myself. It’s important to wrap your DM attempts in a try-catch block to handle such errors effectively. Instead of sending a direct message, consider sending notifications to a dedicated channel when DMs fail. Additionally, verify if your bot shares a server with the user, as this can affect permission settings. Keep in mind that some users may have DMs entirely disabled, making it impossible to reach them directly. Logging these errors can help you understand their frequency among your users.

This error occurs when Discord’s privacy settings block your bot from sending direct messages. From my experience building similar bots, the most reliable approach is implementing proper error handling with graceful degradation. You should modify your code to catch the error and provide an alternative response method. When the DM fails, send a message in the original channel notifying the user that you attempted to send them information privately but couldn’t due to their settings. You can also suggest they temporarily enable DMs from server members if they want to receive the private content. This way your bot remains functional regardless of individual user privacy configurations.