How do I send a message to a specific channel without a message object Discord.js

I’m trying make my bot to send a random question from a .json file every two hours into a specific channel. It’s not inside any event listener so I don’t have a message object to use to send messages.

I’ve tried defining channel with client.channels.cache.get('id') but that just says that .send is not defined. Here’s my current code:

setTimeout(() => {
  const quiz = require('./quiz.json');
  const item = quiz[Math.floor(Math.random() * quiz.length)];
  let channel = client.channels.cache.get('812178275463856128')
  channel.send(item.question)
}, 7200000);

Answer

Try to fetch the channel and check if it’s a text channel by accessing the type property:

client.once('ready', async () => {
  console.log('Bot is connected...');

  const quiz = require('./quiz.json');
  const channelID = '812178275463856128';

  try {
    const channel = await client.channels.fetch(channelID);

    if (!channel || channel.type !== 'text')
      return console.log(`Can't send message to this channel`);

    setTimeout(async () => {
      const item = quiz[Math.floor(Math.random() * quiz.length)];

      channel.send(item.question);
    }, 7200000);
  } catch (error) {
    console.log(error);
  }
});