Reputation: 55
I'm using discord.js 12, and I've tried the following:
client.channels.cache.find(x => x.id == "id").send('message')
client.channels.cache.find(x => x.name == "channel-name").send('message')
...and...
client.channels.cache.get('id').send('message')
and all of them return an error saying 'cannot read property 'send' of undefined.
Now, I know that usually means that the ID of the channel is wrong. However, I've copied it and repasted it like five times. It's the right ID, I assure you.
I've been using this system for months, and now it won't work.
Note: I have the following defined in the file:
const Discord = require('discord.js')
const client = new Discord.Client()
Please help me!
Upvotes: 0
Views: 337
Reputation: 9041
You are either not caching the channel, or it doesn’t exist. I can’t help you if it doesn’t exist. It is returning undefined because it doesn’t see it in the cache. You can easily cache the channels by using a simple fetch()
method.
await client.channels.fetch();
client.channels.cache.get(…).send(…)
You can also directly fetch the channel using the API
(await client.channels.fetch('id')).send(…)
If you are getting an error that says await is only valid in async functions, change the listener to look like this:
client.on(event, async (variable) => {
//code here
//notice it says 'async' before 'variable'
})
//'event' can be any event, variable is the object provided (message events provide message objects)
Upvotes: 1
Reputation: 69
Try
const channel = await client.channels.fetch(<id>);
await channel.send('hi')
This should work.
You can also try this
client.channels.fetch(channelID).then(channel => {
channel.send("hi");
});
Upvotes: 0