Trying to redirect messages from one channel to another one

Viewed 68

I am getting this error on the code below:

(node:13760) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined


bot.on('message', async (message) => {
  message
    .delete({ timeout: 10 })
    .catch((err) =>
      logger.error(`Could not delete message (Error: ${err.message})`),
    );

  // Checking if command was executed in report channel
  if (message.channel.name != 'from-user') {
    // Do nothing
    logger.info(
      `message has not been sent in the forwarding channel! Aborting request.`,
    );
  } else {
    if (message.author.bot) return;
    await client.channels.cache.get('910109122572218378').send(message.content);
    console.log(message.content); // log messages
    return;
  }
});
1 Answers

If the channel ID is correct, the channel is probably not cached. If you know the channel ID, you could fetch the channel by this ID. If it's cached, it returns the cached channel:

const channel = await client.channels.fetch(CHANNEL_ID)

await channel.send(message.content)
console.log(message.content) // log messages

fetch() returns a Promise, so you'll need to use await.

Related