TypeError: Cannot read properties of undefined (reading 'send')
i did find a few posts on this error but no amount of research or looking through documentation has fixed my issue.
basically, im receiving this error when i try to send a message from my discord bot to a specific channel using client.channels.cashe.get
problem code:
const channel = client.channels.cache.find(channel => channel.id === "915481280538370059");
await interaction.reply({content: 'User kicked :peachcord_checkmark:', ephemeral: true});
await channel.send({embeds: [embed]})
interaction.guild.members.kick(user);
the problem is said to be with this line:
await channel.send({embeds: [embed]})
the previous code is not necessary as its just my slash command builder and the issue is with the method im trying to use to send this message, but if its needed i can provide.
if possible i would greatly appreciate some help ;-;18 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OPFirst of all, you can use the
get
method instead of find
Keep in mind that the channel can be undefined
if it wasn't found, so you can wrap the send
method inside of a conditional
do u know for sure that the channel is a text based channel in a guild the bot is on?
if (channel) { // If channel exists channel.send({ embeds: [embed] }); // Sends the message }that's just
correct, yes
im trying to collect the channel id to send the message into that channel when i kick someone
i tried
get
and it still doesnt work :/
i tried the 2nd method, the command works, user is kicked, but the message is not sent into that channel and i get no error
it is, its my bot and its in my server. it is a text based chat
(sorry for late replies)that means that the channel doesn't exist, try logging the channel object and see what you get
do you have the Guilds intent
im very sorry can you explain ;-;
yes i do
the id might be the wrong one
use console.log(channel) after getting the channel to see if it exists
it comes up as
undefined
when i use the id in discord it links the correct channel so the id definitely is correctare you absolutely sure?
when you right click and copy the channel id it is the correct one?
yes, i use <#id> and it works perfectly
maybe try fetch instead of cache.get?
i doubt it changes anything but i really don't know why the current one wouldn't work if everything is set correctly
hm, i get this error
TypeError: client.channels.cache.fetch is not a function
don't do cache.fetch, just fetch
so
actually, do instead
i get
when i use this, i get
Error: Expected token to be set for this request, but none was present
which is weird because my token is correct and ive logged my bot in correctlyusually only the interaction gets passed onto the command so you should always use interaction instead of client
did you await it?
also can you show me the code
that worked!! thank you!