我目前正在根据之前的代码设置一个高级队列机器人,但是我需要从snekfetch切换到node fetch
我尝试使用get函数,但没有结果,因为它不存在
run(msg, { user }) {
fetch.get('https://api.2b2t.dev/prioq').then(r => {
fetch.get('https://2b2t.io/api/queue').then(qwerty => {
let entry = r.body[1]
let response = qwerty.body[0][1]
client.user.setActivity("Queue: " + response + " PrioQueue: " + entry);
if(message.channel.id === 598643633398349854) {
message.delete().catch(O_o=>{});
message.author.send("The owner has disabled this command in this channel!")
return
}
const queueembed = new RichEmbed()
.setColor("#32CD32")
.addField('Regular Queue:', response, true)
.addField('Priority Queue:', entry, true)
.addField('Regular Queue Time:', Math.round(response * 0.7) + " minutes.", true)
.addField('Priority Queue Time:', Math.round(entry * 0.7) + " minutes.", true)
.setThumbnail('https://i.redd.it/jyvrickfyuly.jpg')
.setFooter("https://discordapp.com/invite/uGfHNVQ")
message.channel.send(queueembed).then(msg => {
var timerID = setInterval(function() {
const queueembed = new RichEmbed()
.setColor("#32CD32")
.addField('Regular Queue:', response, true)
.addField('Priority Queue:', entry, true)
.addField('Regular Queue Time:', Math.round(response * 0.7) + " minutes.", true)
.addField('Priority Queue Time:', Math.round(entry * 0.7) + " minutes.", true)
.setThumbnail('https://i.redd.it/jyvrickfyuly.jpg')
.setFooter("https://discordapp.com/invite/uGfHNVQ")
message.channel.edit(msg)
}, 5 * 1000);
})})
})
}
}
通常该机器人会启动,但会弹出此错误
我尝试切换获取和删除获取,但是我很困惑 关于下一步做什么
"TypeError: Cannot read property 'get' of undefined"
答案 0 :(得分:0)
好像有两个问题:
1)fetch
未定义(是否需要安装/要求?)
2)get
不是获取API的一部分
对于GET请求,您只需执行fetch('<url>')
。
将两者放在一起:
const fetch = require('node-fetch')
fetch('https://api.2b2t.dev/prioq').then(r => {
fetch('https://2b2t.io/api/queue').then(qwerty => {
// ...rest
})
})
https://github.com/bitinn/node-fetch#api
编辑
您还需要使其余代码兼容。根据获取规范,响应公开的body
是ReadableStream
,这很可能导致您的错误。正如您从其界面中看到的那样,它还公开了text()
和json()
方法:
interface mixin Body {
readonly attribute ReadableStream? body;
readonly attribute boolean bodyUsed;
[NewObject] Promise<ArrayBuffer> arrayBuffer();
[NewObject] Promise<Blob> blob();
[NewObject] Promise<FormData> formData();
[NewObject] Promise<any> json();
[NewObject] Promise<USVString> text();
};
https://fetch.spec.whatwg.org/#body-mixin
我假设响应是JSON,因此您将要使用response.json()
:
fetch('https://api.2b2t.dev/prioq').then(r => r.json()).then(r => {
fetch('https://2b2t.io/api/queue').then(qwerty => qwerty.json()).then(qwerty => {
// ...rest
})