Sapphire - Imagine a framework
Sapphire - Imagine a framework

sapphire-support

Root Question Message

chpsterz
chpsterz2/18/2023
"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");


Any thoughts why?
Ben855
Ben8552/18/2023
Channel might net be a text based channel. Use channel.isTextBased() to ensure that it is before trying to send a message
chpsterz
chpsterz2/18/2023
it is
Ben855
Ben8552/18/2023
You may know that it is but the code doesnt. You still need to preform the check in the code.
chpsterz
chpsterz2/18/2023
chpsterz
chpsterz2/18/2023
Ben855
Ben8552/18/2023
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
chpsterz2/18/2023
yes
chpsterz
chpsterz2/18/2023
so how do I make it not undefined
chpsterz
chpsterz2/18/2023
and make it send a message?
Ben855
Ben8552/18/2023
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
chpsterz2/18/2023
yes that worked
chpsterz
chpsterz2/18/2023
alrighty
chpsterz
chpsterz2/18/2023
thank you very much.
Ben855
Ben8552/18/2023
np
chpsterz
chpsterz2/18/2023
Solution(For Future People trying to solve this issue)

const channel =  await client.channels.fetch("1076251641625456700");
        if(channel?.isTextBased() ) {
            channel.send("Hello World!")
        }

Fetch the channel, make sure it is text based.
Favna
Favna2/18/2023
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 #737142071319855105
Favna
Favna2/18/2023
Specifically !isStageChannel(channel)
Ben855
Ben8552/18/2023
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
Krish2/18/2023
You can send messages in VC
Ben855
Ben8552/18/2023
Right I forgot they had text channels now. But you still cant send messages in a category channel
Ben855
Ben8552/18/2023
Also I dont think .send exists on forum channels but I could be wrong about that
Krish
Krish2/18/2023
Favna could answer
Favna
Favna2/18/2023
because stage channels are also text channels so isTextChannel doesn't exclude stage channel
Favna
Favna2/18/2023
stage channels can have messages now
Favna
Favna2/18/2023
as of like last week or so
Favna
Favna2/18/2023
which is why the change was made to discord-api-types to begin with
Ben855
Ben8552/18/2023
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
Favna2/18/2023
stage channels can now have messages and therefore they are text channels because the definition of a text channel is "can have messages"
Ben855
Ben8552/18/2023
Right so then why would you check if a channel is not a stage channel before sending a message to it then?
Favna
Favna2/18/2023
because at least for now DJS doesnt support sending a message to a stage channel
Favna
Favna2/18/2023
until an update rolls out
Favna
Favna2/18/2023
anyway yes you should do
if (!isTextChannel(channel) || isStageChannel(channel) {
  return;
}

// do other stuff

for now
Ben855
Ben8552/18/2023
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
Ben855
Ben8552/18/2023
Yep
Ben855
Ben8552/18/2023
I thought you were saying to just do the stage check, not both.
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy