javascript - 如何在 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
任何帮助将不胜感激,谢谢!
解决方案
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
推荐阅读
- performance - go - 如何在映射带有子切片的结构时降低时间复杂度?
- javascript - 如何使用 mocha chai 检查 prisma 服务器是否正在运行?
- c++ - 如何在你的库中包含外部库?
- c# - 如何在 ASP.Net Core 的剃须刀页面上连接 ViewData 中的两个数据?
- javascript - 控制台可用数据,但我发现说未定义
- c# - 播种身份角色
- java - 现在 Handler() 已被弃用,我该使用什么?
- javascript - JQuery JSON [对象对象]
- svelte-3 - 在 Svelte 组件中选择一个类?
- javascript - 如何让多个 Java 脚本在 Frida 中同时运行?