首页 > 解决方案 > 如何在 bot.on('ready', () => { }) 函数内的 discord.js 中获取频道 ID

问题描述

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

任何帮助将不胜感激,谢谢!

标签: javascriptdiscord.jschannel

解决方案


const channel = bot.channels.cache.find(ch => ch.id === 'channelid'); 或者您可以尝试: const channel = bot.channels.guild.cache.find(ch => ch.id === 'channelid') 希望对您有所帮助。我不是 100% 确定,但它看起来就是这里的问题。当你使用 find() 函数获取它的 id 时,你不能忘记 channel 中的 s 而不是 channel


推荐阅读