无法读取未定义不和谐的属性'get'.js



我一直试图使用channelId将消息发送到服务器中的特定文本通道,但最终收到错误:

TypeError: Cannot read property 'get' of undefined
at Object.<anonymous>

使用时

const channel = bot.channels.cache.get('711580200315650078');

这是我的全部代码:

require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
const channel = bot.channels.cache.get('711580200315650078');
channel.send('hello');

});

您可以尝试:

require('dotenv').config();
const Discord = require('discord.js');
const bot = new Discord.Client();
const TOKEN = process.env.TOKEN;
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
const channel = bot.channels.get('711580200315650078');
channel.send('hello');

});

这可能是因为您使用的是Discord.js v11。您应该使用npm install discord.js迁移到v12。然后您将能够使用:

const channel = bot.channels.cache.get('711580200315650078');

相关内容

  • 没有找到相关文章