I try all the possibilities of stack overflow on this subject and that doesn't work for me, I think that outdated. I'm on Discord.JS V13.6
So, for the moment, I have this :
const channel = client.channels.cache.get('my_id-channel');
channel.send('content');
I take my_id-channel with a right click on the channel → copy id
My error :
Uncaught TypeError: Cannot read property 'send' of undefined
It's likely the channel doesn't currently exist in the ChannelManager's cache, try using the ChannelManager.fetch() method.
Your code should look something like this:
client.channels.fetch('my_id-channel').then(channel => channel.send('content'));
If you're unfamiliar, a cache is a local temporary storage used to save data temporarily so you don't have to do operations which may take a long time (i.e. requesting information from a server). Essentially this means that a cache is volatile and you can't always guarantee what you're looking for is there.
I suspect in this case the channel has not previously been retrieved so it does not exist in the cache yet.
After a switch ON my brain, I understand my stupid script -_-
I didn't know I'm supposed to write my script here :
client.on("ready", () => {
// HERE MY CODE
});
So now that's work ^^'