Had this exact issue when I started with discord.js. The error’s misleading - it’s not that the function doesn’t exist, it’s because .cache.get() returns undefined when the user isn’t cached, then you’re calling .send() on undefined. Even with fetch, Discord restricts who bots can DM. Your bot can only DM users who share a server with it or have DMed the bot before. If neither’s true, fetch works but send fails with a 403 error. Always wrap DM attempts in error handling since users constantly disable bot DMs or leave servers.
Your bot can’t find the user because they’re not cached. .cache.get() only grabs users your bot has already seen or talked to. If they’re not there, you get undefined - that’s why .send() fails. Use discordBot.users.fetch("UserID123456789") instead. It returns a promise, so handle it with .then() or async/await. This actually hits Discord’s API instead of just checking your local cache. Just remember - your bot can only DM users who share a server with it or have DMed the bot before.
That error indicates a version mismatch in discord.js. Older versions used discordBot.users.get() without .cache. You should run npm list discord.js to verify your version. If you’re on v12 or later, remember that the user must be cached first for the cache method to work. I faced a similar issue when migrating from v11 to v13 and had to rewrite all my user fetching logic. The fetch method that’s been suggested is the most reliable approach; just ensure you are using the correct syntax for your specific version.
You’re hitting the classic caching issue. Discord.js only caches users after they interact with your bot, so .cache.get() comes up empty for most people.
Fetching works, but you’ll spend forever handling edge cases. DMs break constantly - users block bots, turn off DMs, or leave servers. Then you’re stuck building retry logic and rate limiting from scratch.
I used to write all this boilerplate code until I got tired of reinventing the wheel. Now I just automate the whole Discord workflow. Set triggers for user events, auto-send messages with built-in retries, connect to other systems without touching code.
Once you’re running multiple bots or complex messaging, automation destroys manual coding. You get reliability without the constant debugging.
Latenode handles Discord’s API weirdness and gives you visual workflow building. No more cache debugging or custom error handlers.
The bot probably doesn’t have that user cached yet. You’ve got to fetch them first before sending the DM.
Try this:
try {
const user = await discordBot.users.fetch("UserID123456789");
await user.send("Hello there!");
} catch (error) {
console.log("Failed to send DM:", error);
}
fetch() grabs the user even when they’re not cached. Always use try-catch since DMs fail when users disable them.
Honestly, if you’re building something with multiple Discord operations, automate the whole thing. I’ve watched too many bots break trying to handle complex messaging manually.
Latenode makes this way easier. Set up automated flows for user fetching, messaging, error handling, and retry logic without all that boilerplate. It connects with tons of other services too if you expand later.
yeah, same thing happened to me lol. .cache.get() returns undefined when the user isn’t cached. just add a null check before sending: const user = discordBot.users.cache.get("UserID123456789"); if(user) { user.send("hello"); } or use fetch like everyone else mentioned.
The user isn’t in your bot’s cache. This typically occurs if they have not interacted with your bot recently or if you aren’t in any shared servers. To avoid issues with the cache, you should fetch the user directly from the Discord API instead:
try {
const user = await discordBot.users.fetch("UserID123456789");
await user.send("Hello there!");
} catch (error) {
console.error("Failed to send DM:", error);
}
Using this method ensures you have the most up-to-date user object, and incorporating error handling is crucial in case users have disabled DMs or blocked your bot. This approach has proven effective in several of my production bots.