Home > Software design >  Discord TypeError: message.channel.isText is not a function
Discord TypeError: message.channel.isText is not a function

Time:07-19

I've a Discord bot where I'm checking if a channel is a text channel. The following code worked until today, but now it throws an error.

if (message.channel.isText()) { ... }

Error:

  if (message.channel.isText())
                      ^
TypeError: message.channel.isText is not a function

I also tried it with message.channel.type === 'GUILD_TEXT' but it always returns false, even if the channel is a text channel.

CodePudding user response:

In discordjs v14, channel#isText(), channel#isVoice(), channel#isDM(), etc. are not available any more. You should use the ChannelType enums instead.

If you want to check if a channel is a TextChannel, you can use the following:

const { ChannelType, Client } = require('discord.js');
// ..
if (message.channel.type === ChannelType.GuildText)

Also, channel.type is no longer a string but a number. For example, channel.type for a TextChannel returns 0, that's why channel.type === 'GUILD_TEXT' no longer works for you.

Here is a list of channel types in v14:

channel type v12 v13 v14 v14 type()
guild text channel text GUILD_TEXT GuildText 0
DM channel dm DM DM 1
guild voice channel voice GUILD_VOICE GuildVoice 2
group DM channel N/A GROUP_DM GroupDM 3
guild category channel category GUILD_CATEGORY GuildCategory 4
guild news channel news GUILD_NEWS GuildNews 5
guild news channel's public thread channel N/A GUILD_NEWS_THREAD GuildNewsThread 10
guild text channel's public thread channel N/A GUILD_PUBLIC_THREAD GuildPublicThread 11
guild text channel's private thread channel N/A GUILD_PRIVATE_THREAD GuildPrivateThread 12
guild stage voice channel N/A GUILD_STAGE_VOICE GuildStageVoice 13
guild store channel store GUILD_STORE N/A N/A
generic channel of unknown type unknown UNKNOWN N/A N/A

CodePudding user response:

Discord.js’ BaseChannel class doesn’t have an isText() method, at least not according to its latest API docs.

Is it possible you meant to use isTextBased() method instead?

if (message.channel.isTextBased()) { /* … */ }
  • Related