I want to send a message to a specific channel but it doesn't work, I've tried
client.guilds.cache.get("<id>").send("<msg>")
but I get the error ".send is not a function".
What am I missing here?
CodePudding user response:
You are trying to get from the cache a Guild
not a Channel
, or more specifically a TextChannel
, that's why what you try is not working, you should try to change the code to something like this and it should work.
const channel = client.channels.cache.get('Channel Id');
channel.send({ content: 'This is a message' });
Also, as something extra, I would recommend using the fetch
method for these things, since it checks the bot's cache and in the case of not finding the requested object it sends a request to the api, but it depends on the final use if you need it or not.
const channel = await client.channels.fetch('Channel Id');
channel.send({ content: 'This is a message' });
CodePudding user response:
There are three ways of sending message to a specific channel.
1. Using a fetch method
const channel = await <client>.channels.fetch('channelID')
channel.send({content: "Example Message"})
2. Using a get method
const channel = await <client>.channels.cache.get('channelID')
channel.send({content: "Example Message"})
3. Using a find method
a.
const channel = await <client>.channels.cache.find(channel => channel.id === "channelID")
channel.send({content: "Example Message"})
b.
const channel = await <client>.channels.cache.find(channel => channel.name === "channelName")
channel.send({content: "Example Message})
Your current code line is getting the GUILD_ID not the CHANNEL_ID.