Sending private messages via Discord Bot: What intents are needed?

Hey folks, I’m new to Discord bots and I’m struggling to send DMs to users. I’ve read the docs but they’re a bit confusing. What intents do I need to send a private message to a Discord user using a bot? I want to trigger this with internal function calls, not server events.

Here’s a basic example of what I’m trying:

const { Client, GatewayIntentBits } = require('discord.js');

const bot = new Client({ intents: [GatewayIntentBits.DIRECT_MESSAGES] });

async function dmUser(content) {
  const targetId = '123456789012345678';
  
  await bot.login('your-bot-token-here');
  
  const recipient = await bot.users.fetch(targetId);
  
  await recipient.send(content);
}

I’ve tried different combos of intents and methods, but nothing works. Any help would be awesome! Thanks for your patience with a newbie.

yo, for sending DMs u dont need any special intents. just make sure ur bot is logged in before trying to send messages. heres a quick fix:

bot.login('your-token');

async function dmUser(content, userId) {
  try {
    const user = await bot.users.fetch(userId);
    await user.send(content);
  } catch (err) {
    console.log('dm failed:', err);
  }
}

give it a shot and lmk if u need more help!

For sending DMs, you actually don’t need any special intents. The DIRECT_MESSAGES intent is only for receiving DMs, not sending them. Your code looks mostly correct, but there are a few tweaks I’d suggest:

  1. Move the bot login outside of the dmUser function. You should only log in once when your bot starts up.

  2. Make sure you’re using a valid user ID for targetId.

  3. Error handling is crucial. Users might have DMs closed or the bot might not share a server with them.

Here’s a slightly modified version that should work:

const bot = new Client({ intents: [] });
bot.login('your-bot-token-here');

async function dmUser(content) {
  try {
    const recipient = await bot.users.fetch('123456789012345678');
    await recipient.send(content);
  } catch (error) {
    console.error('Failed to send DM:', error);
  }
}

Hope this helps! Let me know if you run into any other issues.

Hey there, I’ve been working with Discord bots for a while now, and I can share some insights on sending DMs.

The previous answers are correct about not needing special intents for sending DMs, but I’d like to add a few practical tips from personal experience. Ensure your bot and the target user are in a mutual server, since this connection is essential for successful DMs. Another crucial point is to implement rate limiting; Discord can be strict with how many DMs are sent over a short period. Also, always consider the possibility that the user has DMs disabled, so it may be worth designing a fallback mechanism.

Here’s a slightly more robust function I use in production:

async function safeDM(userId, content) {
  try {
    const user = await bot.users.fetch(userId);
    await user.send(content);
  } catch (error) {
    if (error.code === 50007) {
      console.log(`User ${userId} has DMs closed`);
      // Implement fallback here
    } else {
      console.error('Failed to send DM:', error);
    }
  }
}

This approach has worked well for me over time. Hope it helps!