【问题标题】:Node fetching failed, but one also works节点获取失败,但也可以
【发布时间】:2020-08-29 05:10:31
【问题描述】:

我正在使用 discord.jsnode.js 制作一个 Discord 机器人。我正在尝试制作两个命令,一个purr 命令和一个woof 命令。我正在使用node-fetch 从网络上获取图像并将它们发布到不和谐。这是我的代码:

else if (command === 'purr') {
    const fetch = require('node-fetch');
    fetch('https://aws.random.cat/meow').then(response => response.json());
    const { file } = await fetch('https://aws.random.cat/meow').then(response => response.json());
    const catEmbed = new Discord.MessageEmbed()
        .setColor('#fbfb76')
        .setTitle('Purr...')
        .setImage(file)
        .setFooter('Over 1000 free cat pics thanks to aws.random.cat :)');
    message.channel.send(catEmbed);
}
else if (command === 'woof') {
    const fetch = require('node-fetch');
    fetch('https://dog.ceo/api/breeds/image/random').then(response => response.json());
    const { file } = await fetch('https://dog.ceo/api/breeds/image/random').then(response => response.json());
    message.channel.send(file);
}

purr 命令有效。 woof 命令……嗯,不是真的。它输出此错误:

DiscordAPIError: Cannot send an empty message

表示无法从dog.ceo/api/breeds/image/random 获取。由于它适用于purr 命令,但不适用于woof 命令,有人可以告诉我问题并建议如何解决吗?谢谢。

【问题讨论】:

  • 我无法重现这个 rn,但我想说你需要像 send({ file }) 一样发送它,而不仅仅是在它需要一个字符串时发送它
  • 唯一的问题是它输出的错误与我得到的相同

标签: node.js fetch bots discord.js


【解决方案1】:

这是因为 https://aws.random.cat/meow 返回一个 file 变量。但是,https://dog.ceo/api/breeds/image/random 返回一个 message 变量。改用这个:

const { message } = await fetch('https://dog.ceo/api/breeds/image/random').then(response => response.json());

【讨论】:

  • 哈哈,这完全是我的错。抱歉,我今天还没喝咖啡哈哈。检查编辑的答案。
  • 有问题;我已经在上层范围内声明了消息,所以我是否应该执行const { message1 } ...... 之类的操作?
  • 您可以先执行const res = await fetch('https://dog.ceo/api/breeds/image/random').then(response => response.json());,然后执行res.message 来获取消息。
  • 很高兴我能帮上忙 :) 如果它解决了您的问题,请确保mark my answer as accepted
  • 很抱歉,它不起作用。它说“无法读取 null 的‘消息’”。
猜你喜欢
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
相关资源
最近更新 更多