discord - 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"]);
解决方案
您使用的端点发送{"message":"Not Found"}
,所以当然data.url
是未定义的。
查看文档,您需要使用端点https://waifu.pics/api/sfw/waifu
。
推荐阅读
- vba - 如何将两个查询中的数据附加到一个表中?运行时错误:发生 3061 预期 2
- c# - ASP.NET Core 3.1 找不到类型或命名空间名称 DataTable。我怎样才能解决这个问题?
- r - 在R中按行将大数据帧拆分为多个数据帧
- encryption - 解码部分加密的文件 C#
- solidity - 如何使用 Solidity 转移余额?
- python - 如何在 Windows 10 和 Python 3.8 上为 C# (VS2019) 安装 Python.NET?
- eclipse - 阴影插件未执行
- java - 为什么流在并行模式下不起作用?
- haskell - 如何使用 pandoc API 转换多文件 LaTeX 文档?
- javascript - 数组 javascript 分组