Hey everyone, I’m having trouble with my Discord bot. I made a new bot and added it to a server with me. The bot can respond to slash commands in the server, but it can’t send me direct messages.
Here’s a simplified version of what I’m trying to do:
func handleCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Respond to slash command
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Check your DMs!",
Flags: 1 << 6,
},
})
// Try to send a DM
dmChannel, _ := s.UserChannelCreate(i.Member.User.ID)
s.ChannelMessageSend(dmChannel.ID, "Here's a private message!")
}
But when the bot tries to send the DM, I get this error:
Error sending DM: HTTP 403 Forbidden, {"message": "Cannot send messages to this user", "code": 50007}
I’ve checked my privacy settings and they’re all open. The bot can respond in the server channel, but not in DMs. Any ideas what might be causing this? Thanks!
I’ve dealt with this problem in my own Discord bot projects. One thing that’s often overlooked is the ‘Intents’ system Discord introduced a while back. Make sure you’ve enabled the necessary intents for your bot, especially the ‘MESSAGE_CONTENT’ intent if you’re trying to read or send messages.
Also, it’s worth noting that some users might have global settings that prevent bots from DMing them, regardless of server-specific settings. As a workaround, you could implement a system where users have to opt-in to receive DMs from your bot, perhaps by using a specific command.
Lastly, don’t forget to handle errors gracefully. Instead of just printing the error, you could log it and inform the user that the DM couldn’t be sent, suggesting they check their privacy settings or contact you for support.
I’ve encountered this issue before, and it’s often related to Discord’s privacy features. Even if your personal settings allow DMs, the bot itself might be restricted. Check the bot’s permissions in your server settings to ensure it has the ‘Send Messages’ permission globally.
Another potential cause is rate limiting. If your bot attempts to send too many DMs in a short period, Discord may temporarily block it from sending messages. Try implementing a delay between DM attempts or use a queue system to manage message sending.
Lastly, some users might have server-specific DM settings that override their global settings. You could add a fallback mechanism to send the message in a server channel if the DM fails.
hey mate, i had the same issue. turns out some users have their DM settings set to only receive messages from friends. try checking ur discord privacy settings and make sure ‘allow direct messages from server members’ is on. Also, the bot needs the ‘send messages’ permission in the server. hope this helps!