简体   繁体   English

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

[英]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.

相关问题 如何在 javascript 中使用类似、电报、discord.js 设置 bot.on()? - How can i set bot.on() using like, telegraf, discord.js in javascript? bot.on('messageCreate') discord.js 如何工作? 机器人不会检测到发送的消息,试图制作一个 discord 聊天机器人 - how dose the bot.on('messageCreate') discord.js work? the bot wont detect a sent message, trying to make a discord chat bot 如何找到机器人在特定频道中发送的消息? [discord.js] - How can I find a message send in a specific Channel from the bot? [discord.js] Discord.js 如何让机器人对它刚刚通过 id 在 discord 中发送的消息做出反应? - Discord.js How do I get the bot to react to the message it just sent in discord by id? 如何列出我的机器人在控制台中的所有 Discord 服务器 ID? | discord.js - How can I list the all Discord servers ID where my bot are in the console? | discord.js 获取频道 ID discord.js - get a channel id discord.js DISCORD.JS 如何通过名称获取特定的频道 ID? - DISCORD.JS How to get a specific channel ID by name? Discord.js:如何使用其名称获取频道 ID - Discord.js: how to get channel id using its name discord.js - 如何获取机器人语音频道的名称/ID - discord.js - How to get the name/id of the bot's voiceChannel Discord 机器人无法断开与 discord.js 中的语音通道的连接 - The Discord Bot Can't Disconnect To The Voice Channel in discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM