2

I was trying to send a message to a specific channel so I use the script below

let msg = "I am a message"
clients.channels.cache.get("902208249099612170").send(msg)

but all I got were this error

clients.channels.cache.get("902208249099612170").send(msg)
                                                 ^

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

I have no idea why can't it send a message, I am using discord js v13, anyone has any idea what is the problem? Thanks

4 Answers4

1

The channel might not exist in the client's cache. Try fetching the channel instead:

clients.channels.fetch("902208249099612170").then((channel) => channel.send(msg));

// Or with async/await
const channel = await clients.channels.fetch("902208249099612170");
channel.send(msg)
iamkneel
  • 1,303
  • 8
  • 12
1

It means that the channel either doesn't exist or the bot can't see the channel/server the channel is in. Try catching it :

let msg = "This is a message."
clients.channels.fetch("902208249099612170")
.then((channel) => channel.send(msg));
.catch(err => console.log("Could not find the channel."))
Staxlinou
  • 1,351
  • 1
  • 5
  • 20
  • I tested it out and surprisingly it say could not find channel, but I tried other channels it still said it couldn't find the channel – some kid playing js Dec 05 '21 at 13:05
  • @somekidplayingjs yea that means that the bot can't find the channel. Make sure the bot can see it and is in the server the channel is in (and ofc that it's a valid id) – Staxlinou Dec 05 '21 at 21:22
1

Well, I found the problem. Actually, there are multiple clients on the file and the one logged in to the bot isn't the one sending the message to the specific channel so that's why I am unable to send it. Sorry if I wasted you guys' time .

Lesson learned: When you want to send a message to a specific channel make sure the client is logged into the bot's token!

-1

Maybe forgot to listen to the client.on('ready', client => {}) event?

If not, try checking the channel id and the bots permissions on this channel. The bot maybe doesn't have enough permission to view/send messages to the channel.

Try calling the send function without the cache. Although this should only work on legacy versions.

clients.channels.get("902208249099612170").send(msg)

References:

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Simon
  • 1
  • 1
  • 5