Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

discord.js bot cannot find a channel by ID or name

ErrorTypeError: Cannot read properties of undefined (reading 'send')

Short answer

In discord.js, client.channels.cache only holds channels that Discord has sent to your bot. If the Guilds intent is missing, or your code runs before the client is ready, the cache is empty and get() returns undefined. Add the intent, run the code in the ready event, and use channels.fetch(id) for a channel that is not cached.

  • Applies to discord.js 14, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log how many channels the bot has cached right after it is ready. Zero means a cache or intent problem; a positive number means the channel you want is not in the list.

    client.once('clientReady', () => {
      console.log('cached channels:', client.channels.cache.size);
      console.log('cached guilds:', client.guilds.cache.size);
    });
  2. 2Match what you see, then jump to its fix

    1. 1cache.size is 0 after the bot is readyThe Guilds intent is not enabledFix
    2. 2get() works later in an event handler but not at the top of the fileThe lookup runs before the client is readyFix
    3. 3cache.size is above 0 but your channel is missingThe channel is not in the cacheFix

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'send')
  • client.channels.cache.get(id) is undefined
  • client.channels.cache.find(c => c.name === 'general') is undefined

Cause 1 of 3

The Guilds intent is not enabled

1Check

The discord.js guide says the Guilds intent populates and maintains the guilds, channels and roles caches. Without it, data comes as bare IDs. Look at the intents array in new Client().

grep -n "intents" -r src index.js

2Fix

Add GatewayIntentBits.Guilds to the client's intents.

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [GatewayIntentBits.Guilds],
});

3Verify

Restart the bot and read the cache size again.

node index.js

Confirmed by discordjs.guide

Did this fix it?

Cause 2 of 3

The lookup runs before the client is ready

1Check

The ready event is documented as the moment the client becomes ready to start working. A lookup placed straight after login() runs before that.

grep -n "login(\|channels.cache" index.js

2Fix

Move the lookup into the ready event handler, or into the command handler that runs after it.

client.once('clientReady', async (c) => {
  const channel = c.channels.cache.get('123456789012345678');
  if (channel) await channel.send('Online');
});
client.login(process.env.DISCORD_TOKEN);

3Verify

The message arrives once, after the bot shows as online.

node index.js

Confirmed by discord.js.org

Did this fix it?

Cause 3 of 3

The channel is not in the cache

1Check

The docs say DM channels are not cached initially, and fetch() checks the cache first and then asks Discord. Ask Discord directly for the ID.

const channel = await client.channels.fetch('123456789012345678').catch(console.error);
console.log(channel?.name);

2Fix

Use channels.fetch(id) instead of cache.get(id), and handle the null or the rejection.

const channel = await client.channels.fetch(channelId);
if (!channel) return;
await channel.send('Hello');

3Verify

fetch resolves to a channel object and send works.

console.log(channel?.id);

Confirmed by discord.js.org

Did this fix it?

How do I stop it coming back?

  • Keep channel IDs in config and fetch them with channels.fetch, not by name, because names can change.
  • Run channel lookups only inside the ready handler or in command handlers.
  • Log the cache size once at startup so an intent problem shows up on day one.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.