我在想如何从这个抓取中得到一个字符串。到目前为止,我有这样的代码,例如,我希望级别类似于
message.reply(`Your level is ${level}`)现在我有个代码:
if(args[0]) {
let krunkerName = args[0]
const api = `https://apiktracer.herokuapp.com/user/${krunkerName}`;
fetch(api)
.then(res => res.text())
.then(body => console.log(body));
message.channel.send() //String here
} else {
message.reply("Please select a user to check stats on")
}在console.log中,当我输入一个名称时(在本例中是mackex5x5 ),就会得到这个结果。
{"stats":{"name":"mackex5x5","id":2858585,"score":2921985,"level":51,"levelProgress":28,"kills":25913,"deaths":5309,"kdr":"4.88","kpg":"19.92","spk":"112.76","totalGamesPlayed":1301,"wins":922,"loses":379,"wl":"0.71","playTime":"3d 26m","funds":22280,"clan":false,"featured":"No","hacker":false,"following":0,"followers":3,"shots":186973,"hits":72936,"nukes":71,"meleeKills":59,"createdDate":"2019-04-13","createdTime":"13:06:00","lastPlayedClass":"Marksman"},"uuid":"3e78ef6b-6121-42db-a112-e6757e5d0bf4","error":"False"}发布于 2019-12-21 23:35:12
您希望.send或.reply在您的.then链中。
if(args[0]) {
let krunkerName = args[0]
const api = `https://apiktracer.herokuapp.com/user/${krunkerName}`;
fetch(api)
.then(res => res.json())
.then(body => {
console.log(body)
message.reply(`Your level is ${body.stats.level}`) //String here
});
}else{
message.reply("Please select a user to check stats on")
}因为fetch api将来会解析,因为它是异步调用,所以message.channel.send或message.reply在解析之前被称为。
https://stackoverflow.com/questions/59440595
复制相似问题