【问题标题】:How can I get a channel id in discord.js inside of the bot.on('ready', () => { }) function如何在 bot.on('ready', () => { }) 函数内的 discord.js 中获取频道 ID
【发布时间】:2021-10-19 20:17:37
【问题描述】:

我想在执行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

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

【问题讨论】:

  • 这可能属于 API 滥用,因为您会在短时间内发出如此多的请求。
  • 另外,您遇到了什么错误?我怀疑是Cannot read property 'cache' of undefined,如果你要买的话。
  • 错误确实应该是Cannot read property 'cache' of undefined,因为channel 属性在Client 类上不存在。属性是channels,你忘了s
  • 对这种混乱感到抱歉,(我对问题进行了更改),但是是的,我确实有这个错误@MrMythical 和 CypElf。

标签: javascript discord.js channel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2020-07-26
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多