[英]How can I get a channel id in discord.js inside of the bot.on('ready', () => { }) function
I want to get a the id of a specific channel when doing the bot.on('ready', () => { })
function.我想在执行
bot.on('ready', () => { })
函数时获取特定频道的 ID。 I want to do this to send a message repeated times in the specific channel.我想这样做是为了在特定频道中重复发送一条消息。 This is the code I have now:
这是我现在的代码:
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);
})
The error that I am getting is:我得到的错误是:
Cannot read property 'cache' of undefined
Any help would be appreciated, thx!任何帮助将不胜感激,谢谢!
const channel = bot.channels.cache.find(ch => ch.id === 'channelid');
or you can try: const channel = bot.channels.guild.cache.find(ch => ch.id === 'channelid')
I hope that will help you.或者您可以尝试:
const channel = bot.channels.guild.cache.find(ch => ch.id === 'channelid')
希望对您有所帮助。 I'm not 100% sure but its looking that was the problem here.我不是 100% 确定,但它看起来就是这里的问题。 You can't forget about the s in channels instead of channel when you get id of it using find() function
当你使用 find() 函数获取它的 id 时,你不能忘记 channel 中的 s 而不是 channel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.