【问题标题】:Cannot read property 'get' of undefined discord.js无法读取未定义 discord.js 的属性“获取”
【发布时间】:2021-04-14 22:10:36
【问题描述】:

我一直在尝试使用 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');
  
});

【问题讨论】:

  • 你使用的是哪个版本的 Discord JS?
  • @Jakye 谢谢。这确实是问题所在。

标签: node.js discord discord.js bots typeerror


【解决方案1】:

你可以试试:

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');

【讨论】:

  • 谢谢。迁移到 v12 后问题得到解决。
猜你喜欢
  • 2020-12-05
  • 2022-06-10
  • 2021-11-17
  • 1970-01-01
  • 2023-01-14
  • 2023-02-21
  • 2021-06-27
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多