首页 > 解决方案 > Discord Bot无法获取随机图片

问题描述

我尝试制作一个不和谐的机器人,当在聊天中写“$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"]);

标签: discorddiscord.jsbots

解决方案


您使用的端点发送{"message":"Not Found"},所以当然data.url是未定义的。

查看文档,您需要使用端点https://waifu.pics/api/sfw/waifu


推荐阅读