Skip to content Skip to sidebar Skip to footer

Sending A Message In A Specific Channel - Not Working

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'

Solution 1:

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)

Solution 2:

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");
}); 

Post a Comment for "Sending A Message In A Specific Channel - Not Working"