Search code examples
javascriptdiscorddiscord.js

How can I make bot receive DM on Discord js v14?


module.exports = {
    name: 'messageCreate',
    execute(message) {
        if (message.channel.type == 'DM') {
            console.log('Dm recieved')
            client.channels.get('1026279100626767924').send(message);
         }

    }
};

I created event handling by referring to the Discord JS guide. I want my bot to receive a DM and send that message to my admin channel.

But the bot is not recognizing the DM What should I do

(I'm using a translator)


Solution

  • There could be a couple problems with this. First off, it could be your Intents.

    Intents allow your bot to process certain events. The intents that you will want are: DirectMessages MessageContent (this one may not be required, I am unsure).

    So, go to where you initialize your client. It should look something like this:

    const Discord = require("discord.js");
    const client = new Discord.Client();
    

    and change it into this:

    const Discord = require("discord.js");
    const { GatewayIntentBits, Client } = Discord;
    
    const client = new Client({
      intents: [
        GatewayIntentBits.DirectMessages
        GatewayIntentBits.MessageContent
    
      ]
    });
    

    The second issue could be that your message handler isn't detecting it properly. Like the other answer says, you should just determine if there is no Guild attached to the message.

    client.on("messageCreate", async(message) => {
      if(!message.guild) {
       client.channels.cache.get("YOUR_ID").send(message);
      }
    });
    

    There could be other issues, but those two are the most probable. Considering you're not getting any errors, I'd presume it's the first one, the issue with Intents.

    Hope this helps!