How do I check if a message is a DM in Discord.js?

Currently I am using the latest version of Discord.js (v13.1.0).

I want to be able to detect if a certain message is a DM. I tried making a messageCreate event and did an if statement to check if the channel is a DM

client.on('messageCreate', message => {
   if (message.channel.type == 'DM') {
      console.log('Dm recieved!')
   }
})

This for some reason didn’t work. So I have no idea what will.

Here are my intents:

[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGES]

Btw, the event does work:

client.on('messageCreate', message => {
   console.log('Event')
}

Answer

I had this issue earlier as well; I dug into the changes between discord.js v12 and v13 and figured out why.

As seen here on the discordjs.guide webpage dedicated to the breaking changes between the updates:

On Discord API v8 and later, DM Channels do not emit the CHANNEL_CREATE event, which means discord.js is unable to cache them automatically. In order for your bot to receive DMs, the CHANNEL partial must be enabled.

For the very important feature of handling DM messages, that notice was pretty difficult to find. It only directly mentions the CHANNEL_CREATE event, but basically what it means is that your bot is unable to receive DMs at all unless the CHANNEL partial is enabled in your client options.

Here is an example of how you would do that:

const client = new Discord.Client({ partials: ["CHANNEL"], intents: [...] });

You need to add that partials property to your Client constructor in order to receive DMs. After doing so, your code will work properly.