Skip to content
Advertisement

Checking if a message was sent from a DM channel type not working

I am using Discord.js v12 and trying to check if a message was sent from a DM but it doesn’t work for me, I have tried this method:

  if (msg.channel.type === 'dm')

This was working fine couple months ago but it doesn’t work anymore. When i try console.log(msg.channel.type) it only returns text and doesn’t return dm when the bot gets an DM and I can’t find any solutions for this.

Advertisement

Answer

Are you sure you haven’t updated your discord.js version and you’re still using v12?

Channel types in v13 are now uppercase and align with Discord’s naming conventions. See below the changes:

channel type v12 v13
DM channel dm DM
group DM channel N/A GROUP_DM
guild text channel text GUILD_TEXT
guild text channel’s public thread channel N/A GUILD_PUBLIC_THREAD
guild text channel’s private thread channel N/A GUILD_PRIVATE_THREAD
guild voice channel voice GUILD_VOICE
guild stage voice channel N/A GUILD_STAGE_VOICE
guild category channel category GUILD_CATEGORY
guild news channel news GUILD_NEWS
guild news channel’s public thread channel N/A GUILD_NEWS_THREAD
guild store channel store GUILD_STORE
generic channel of unknown type unknown UNKNOWN

It means, that if you want to check if the channel‘s type is DM, you need to use:

if (msg.channel.type === 'DM')

There are some changes in v14 too. You can use the ChannelType enums:

const { ChannelType, Client } = require('discord.js');
// ..
if (message.channel.type === ChannelType.DM)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement