Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Problems with sending

So, my server has channels for each user, and the name of the channel is their user id.

The way I want it to work is;

When a user DMs the bot with any message, the bot looks for that channel (the channel that's named as their user id) in the server and sends the content of the message it receieved in DMs, but I keep getting an error:

Cannot read property "send" of undefined

Here is my code

client.on("message", async (message) => {
    if (message.channel.type === "dm") {
        if (message.author.bot) return;

        var guild = client.guilds.cache.get("840120593311334410");

        const cha = message.author.id;
        var hsa = await guild.channels.cache.find((ch) => ch.name === cha);
        hsa.send("test");
    }
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There are 2 types of problems I have for you:

  1. The bot does not have the permissions to view the channel or the channel does not exist. (However, you said otherwise in the comments)
  2. You don't have the channel in the cache: The code you use is cache-based, which means you need to make sure you have the channels in the cache. Use a simple GuildChannelManager.fetch() to cache all channels.
 var guild = client.guilds.cache.get("840120593311334410"); const cha = message.author.id; await guild.channels.fetch(); //ONLY NEW LINE var hsa = await guild.channels.cache.find((ch) => ch.name === cha); hsa.send("test");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!