"Send" does not exist on type "channel"

Hi. Trying to make a discord bot that will send a message to a specific channel on startup. I try the method of caching the channel and then sending the message. An error is thrown saying "Send" does not exist on type "channel".
const channel = client.channels.cache.get('1076251641625456700');
channel.send("Hi");
const channel = client.channels.cache.get('1076251641625456700');
channel.send("Hi");
Any thoughts why?
Solution:
Solution(For Future People trying to solve this issue) ```ts const channel = await client.channels.fetch("1076251641625456700"); if(channel?.isTextBased() ) {...
Jump to solution
22 Replies
Ben
Ben2y ago
Channel might net be a text based channel. Use channel.isTextBased() to ensure that it is before trying to send a message
chpsterz
chpsterzOP2y ago
it is
Ben
Ben2y ago
You may know that it is but the code doesnt. You still need to preform the check in the code.
chpsterz
chpsterzOP2y ago
chpsterz
chpsterzOP2y ago
Ben
Ben2y ago
Two things. First, remove the casting to a TextChannel because you cant be sure its a text channel. Second, channel is not a TextChannel, its undefined.
chpsterz
chpsterzOP2y ago
yes so how do I make it not undefined and make it send a message?
Ben
Ben2y ago
Well, are you sure that the id your using is correct? If so, it probably is not cached so you will need to fetch it with client.channels.fetch(...)
chpsterz
chpsterzOP2y ago
yes that worked alrighty thank you very much.
Ben
Ben2y ago
np
Solution
chpsterz
chpsterz2y ago
Solution(For Future People trying to solve this issue)
const channel = await client.channels.fetch("1076251641625456700");
if(channel?.isTextBased() ) {
channel.send("Hello World!")
}
const channel = await client.channels.fetch("1076251641625456700");
if(channel?.isTextBased() ) {
channel.send("Hello World!")
}
Fetch the channel, make sure it is text based.
Favna
Favna2y ago
Wrong solution, see <#769862166131245066> in the DiscordJS server. Use isStageChannel function from @sapphire/discord.js-utilities and update to the latest sapphire framework as announced in #Announcements Specifically !isStageChannel(channel)
Ben
Ben2y ago
oop. I didn't see the djs anouncment. But why would you use isStageChannel instead of isTextBased? Wouldnt !isStageChannel(channel) return true for categries & voice channels?
Krish
Krish2y ago
You can send messages in VC
Ben
Ben2y ago
Right I forgot they had text channels now. But you still cant send messages in a category channel Also I dont think .send exists on forum channels but I could be wrong about that
Krish
Krish2y ago
Favna could answer
Favna
Favna2y ago
because stage channels are also text channels so isTextChannel doesn't exclude stage channel stage channels can have messages now as of like last week or so which is why the change was made to discord-api-types to begin with
Ben
Ben2y ago
So stage channels can now have text channels once djs updates to support it but checking !isStageChannel still assumes that you can send a message to a category which I'm pretty sure you cant.
Favna
Favna2y ago
stage channels can now have messages and therefore they are text channels because the definition of a text channel is "can have messages"
Ben
Ben2y ago
Right so then why would you check if a channel is not a stage channel before sending a message to it then?
Favna
Favna2y ago
because at least for now DJS doesnt support sending a message to a stage channel until an update rolls out anyway yes you should do
if (!isTextChannel(channel) || isStageChannel(channel) {
return;
}

// do other stuff
if (!isTextChannel(channel) || isStageChannel(channel) {
return;
}

// do other stuff
for now
Ben
Ben2y ago
Right so you would need to do that check in addition to the isTextBased. If you just did the stage check then you could end up sending to a category Yep I thought you were saying to just do the stage check, not both.
Want results from more Discord servers?
Add your server