Discord.js Bot Error: Cannot read property 'send' of undefined when messaging channel

I’m working on a Discord bot using Node.js and trying to make it send messages to a particular channel in a specific server. However, I keep getting an error that says the send property is undefined.

Here’s what I’m trying to do:

bot.on('message', message => {
  if (message.content.startsWith('!testcommand')) {
    var targetChannel = message.client.channels.get('123456789012345678');
    var serverChannel = message.client.guilds.get('987654321098765432').targetChannel;
    serverChannel.send({
      embed: new Discord.MessageEmbed()
                        .setColor("#FF0000")
                        .setAuthor("Bot Response")
                        .setDescription(`Requested by <@${message.author.id}>`)
    })
  }
})

The error I’m getting is: TypeError: Cannot read property 'send' of undefined

What am I doing wrong here? I thought I was getting the channel correctly but something seems to be missing.

You’re accessing targetChannel wrong. You’ve got the guild part right with message.client.guilds.get('987654321098765432'), but targetChannel doesn’t exist on that object. You need to access the guild’s channels collection instead. Change your line to var serverChannel = message.client.guilds.get('987654321098765432').channels.get('123456789012345678');. If you’re on a newer discord.js version, use cache.get() for channels. Or skip the guild entirely and go straight to message.client.channels.get('123456789012345678') since you already have the channel ID.

hey, looks like ur mixing up some stuff. u got targetChannel but u’re trying to access .targetChannel from the guild object, which ain’t right. just call targetChannel.send() directly since u’ve already gotten it. also, make sure the channel ID is correct, and ur bot can send messages there.

You’re mixing up your variables. You defined targetChannel but then tried to use serverChannel which doesn’t exist. Just use message.client.channels.get('123456789012345678') directly since you already have the channel ID. If you’re on Discord.js v12 or newer, it’s message.client.channels.cache.get('123456789012345678'). Also check that your bot has permission to send messages in that channel - you’ll get errors even with correct code if permissions are wrong.