discord.js bot cannot find a channel by ID or name
TypeError: 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.
Fix it fast
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); });2Match what you see, then jump to its fix
What does the error look like?
TypeError: Cannot read properties of undefined (reading 'send')client.channels.cache.get(id) is undefinedclient.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.js2Fix
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.jsConfirmed by discordjs.guide
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.js2Fix
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.jsConfirmed by discord.js.org
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix