如何在 bot.on('ready', () => { }) 函数中获取 discord.js 中的频道 ID

How can I get a channel id in discord.js inside of the bot.on('ready', () => { }) function

我想在执行 bot.on('ready', () => { }) 功能时获取特定频道的 ID。我想这样做是为了在特定频道中重复发送一条消息。这是我现在的代码:

bot.on('ready', () => {
   console.log(`${bot.user.tag} successfully logged in!`)
   bot.user.setActivity('%help', ({type: "LISTENING"}))
   function message() {
      const channel = bot.channel.cache.find(ch => ch.id === 'id-of-channel');
       if (!channel) return;
      channel.send('message I am sending');
   }

   setInterval(message, 1000);
})

我得到的错误是:

Cannot read property 'cache' of undefined

如有任何帮助,我们将不胜感激!

const channel = bot.channels.cache.find(ch => ch.id === 'channelid'); 或者你可以试试: const channel = bot.channels.guild.cache.find(ch => ch.id === 'channelid') 我希望这会帮助你。我不是 100% 确定,但它看起来就是这里的问题。当您使用 find() 函数

获取 id 时,您不能忘记 channels 而不是 channel 中的 s