【问题标题】:TypeError: channel.send is not a function (Deprecated?)TypeError:channel.send 不是函数(已弃用?)
【发布时间】:2020-12-06 13:38:27
【问题描述】:

我正在尝试构建一个不和谐机器人,但我需要向特定频道发送消息。我已经尝试了 100 次,但不断得到:

TypeError: bot.channels.fetch(...).send is not a function.

在我阅读的所有地方,我都看到“使用 channel.send()”,但我找不到解决问题的实际方法。不推荐使用 channel.send() 吗?以下是错误引发错误的行:

bot.channels.fetch(id).send(message);

我知道 channels.get() 被贬低了,当使用 .fetch(id) 时,我得到了通道对象,但它不会让我发送消息。任何帮助都将不胜感激,因为我已经花了几天时间来解决这个问题。 谢谢。

完整的代码块:

var reportEmbed = new Discord.MessageEmbed()
   .setColor('#0099ff')
   .setTitle('New Report')
   .setAuthor(msg.author.tag)
   .setDescription(reportMessage)
   .setTimestamp();
console.log(bot.channels.fetch('my channel id'));
bot.channels.fetch('my channel id').then(channel => {
   channel.send(reportEmbed);
});

当控制台登录时,我得到了正确的通道对象。

【问题讨论】:

  • 请展示更多您的代码。该代码块在哪个事件函数中?

标签: discord discord.js


【解决方案1】:

尝试使用:

bot.channels.cache.get(id).send(message)

相反

【讨论】:

  • 刚试过。我收到“TypeError:channel.send 不是函数”。
  • 这是我运行的行:bot.channels.cache.get('my channel id').send(reportEmbed); where "report embed is a Discord.MessageEmbed Object.
  • 那行不包含channel.send() 函数,所以我认为它指向另一行。
  • 我很愚蠢。它使用相同的代码行引发了类似的错误,但我没有意识到。我修复了第二个错误,它现在可以工作了。太感谢了!我总是被告知 .get() 通常被贬低了,但是嗯。多谢,伙计! :)
  • bot.channels.get() 已弃用,我认为这就是您感到困惑的原因。 discord.js v12+ 使用 Managers,因此现在需要缓存属性 (bot.channels.cache.get)
【解决方案2】:

正如我在文档中看到的, fetch() 方法返回一个承诺。 (Link)

所以,为了获取频道,你需要使用当前的代码:

bot.channels.fetch("id").then(channel => {
  channel.send(message);
});

【讨论】:

  • 它非常适合我。尝试将您的 discord.js 版本更新到最新版本。
  • 通常是的。我不明白为什么它不起作用,因为它对我有用。
  • 它可能没有找到该 id 的频道。如果您将更多代码放在某个粘贴箱中可能会有所帮助,以便我们可以查看它
  • 机器人是否有权访问该特定频道?好像在服务器里?
  • 绝对机器人可以访问它。但如果机器人无权访问,则会显示不同的错误
猜你喜欢
  • 2020-09-25
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
相关资源
最近更新 更多