Discord Bot 无法获取随机图片

时间:2021-05-02 22:56:00

标签: discord discord.js bots

我尝试制作一个不和谐机器人,当在聊天中写入“$sad”时,它会发送随机动漫图像。代码失效了,有人知道怎么解决吗?

const Discord = require("discord.js");
const fetchData = require("node-fetch");
const client = new Discord.Client();

function getImg() {
  return fetchData("https://waifu.pics/api/random")
    .then((res) => {
      return res.json();
    })
    .then((data) => {
      return data.url;
    });
}

client.on("ready", () => {
  console.log(`logged in as ${client.user.tag}`);
});
client.on("message", (msg) => {
  if (msg.author.bot) return;

  if (msg.content === "$sad") {
    getImg().then((img) => msg.channel.send(img));
  }
});

client.login(process.env["key"]);

1 个答案:

答案 0 :(得分:0)

您正在使用的端点发送 {"message":"Not Found"},因此 data.url 当然将是未定义的。

the documentation,需要使用端点https://waifu.pics/api/sfw/waifu