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

103
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");
    }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are 2 types of problems I have for you:

  1. The bot doesn’t have the permissions to see the channel, or the channel doesn’t exist. (You said otherwise in comments however)
  2. You do not have the channel in the cache - The code you use relies on cache which means you have to make sure you have the channels cached. Use a simple GuildChannelManager.fetch() to cache all the 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");
about 4 years ago · Juan Pablo Isaza 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!