【发布时间】:2021-10-12 05:44:15
【问题描述】:
我正在尝试开发我的 discord 机器人并从 PokeApi 获取信息,特别是 pokemon 的移动列表。我有以下代码,但我正在努力弄清楚如何获取每个口袋妖怪的移动列表。
client.on('message',(async message =>{
const args = message.content.toLowerCase().slice(poke.prefix.length).split(/ +/);
if(!message.content.startsWith(poke.prefix))return;
if(args[0] === "api"){
const fetch = require ('node-fetch');
fetch('https://pokeapi.co/api/v2/pokemon/25')
.then(res => res.json())
.then(data => message.channel.send(data.name.moves.move.name))
.catch(err => message.reply('that spell does not exist!'));}}))
现在我知道我在这里指定了一个特定的口袋妖怪(第 25 号),这很好,因为我可以稍后更改它,但这似乎是让我退缩的部分:
.then(data => message.channel.send(data.name.moves.move.name))
还有一种干净的方法来创建包含数据的嵌入。我必须创建一个包含信息的变量还是可以使用“res”?
非常感谢任何帮助或指导!谢谢!
【问题讨论】:
-
我自己查看 API 响应,看起来这些移动将在
data.moves(一个数组)下,然后其中的每个项目都有一个.nameattr。这就是你遇到的所有问题吗?
标签: node.js json discord pokeapi