Unable to send private messages from Discord bot to user

Hey everyone, I’m having trouble with my Discord bot. It’s not sending DMs to users and I can’t figure out why.

I set up a simple bot using Go and the discordgo library. The bot responds to a slash command in the server, but when it tries to send a private message, it fails.

Here’s a simplified version of what I’m doing:

func handleCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
    s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
        Type: discordgo.InteractionResponseChannelMessageWithSource,
        Data: &discordgo.InteractionResponseData{
            Content: "Got your command!",
            Flags:   1 << 6,
        },
    })

    dmChannel, _ := s.UserChannelCreate(i.Member.User.ID)
    s.ChannelMessageSend(dmChannel.ID, "Here's a private message for you!")
}

But I keep getting this error:

Error sending DM: HTTP 403 Forbidden, {"message": "Cannot send messages to this user", "code": 50007}

The bot and I are in the same server, and my DM settings are open. The bot responds fine in the server channel, just not in DMs.

Any ideas what might be causing this? Thanks for any help!

hey grace, i’ve seen this before. sometimes discord’s weird about DMs. try catching the error and logging it. also, make sure ur bot has the right permissions in the server settings. if nothing works, u could always fallback to sending the message in the channel instead of DM. good luck!

Another aspect to consider is rate limiting. Discord has strict limits on how frequently bots can send messages, especially DMs. If you’re sending multiple DMs in quick succession, you might hit these limits. Try implementing a delay between DM attempts; a 1-2 second wait between each message can help. Also, ensure you’re not accidentally spamming users with repeated messages. It’s worth checking your bot’s OAuth2 scopes too. Make sure you’ve included the ‘bot’ and ‘applications.commands’ scopes when adding the bot to servers. Without proper scopes, the bot might lack necessary permissions for DMs. Lastly, double-check your bot’s token. A mistyped or outdated token can cause unexpected permission issues. Regenerating the token and updating your code might resolve the problem.

I’ve run into this issue before, and it can be frustrating. The 403 Forbidden error usually means the user has their DM settings closed for that server or for non-friends in general. Even if your personal settings are open, the bot might not have the necessary permissions.

A workaround I’ve found effective is to first send a message in the server channel asking the user to enable DMs temporarily. Then, have them initiate the DM by sending a message to the bot first. This establishes the connection and often resolves the issue.

Also, make sure your bot has the necessary intents enabled, particularly the DIRECT_MESSAGES intent. Without it, the bot can’t interact via DMs.

Lastly, consider implementing error handling for DM failures. You could fall back to sending the message in the server channel if the DM fails, ensuring the user still receives the information.