Search code examples
discorddiscord.js

Discord.js issue at finding channel


I am trying to make a verify command but I keep getting stuck on the bot saying "Please Enter A Valid Channel!" even with a valid channel name.

 let channel = message.mentions.channels.first() ||
 message.guild.channels.cache.get(args[0]) ||
 message.guild.channels.cache.find(c => c.name.toLowerCase() ===
 args[0].toLocaleLowerCase());
         if (!channel || channel.type !== 'text') return message.channel.send("**Please Enter A Valid Channel!**");

Example: Discord Example code

Don't mind the role mention. It's for a role to be given when reacting


Solution

  • In discord.js v13 channels types have changed:

    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

    This table was taken from this answer made by @Zsolt Meszaros

    Change your code to this:

    if (!channel || channel.type !== "GUILD_TEXT") return message.channel.send("**Please Enter A Valid Channel!**")