尝试使用 node-fetch 启动机器人时出错

Error when tryign to start the bot with node-fetch

我目前正在使用之前的代码设置高级队列机器人,但我需要从 snekfetch 切换到节点获取

我已经尝试使用 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); 
    })})
    })
  }
}

通常机器人会启动但弹出此错误

我试过切换 fetch 和删除 get 但我很困惑 关于下一步做什么

"TypeError: Cannot read property 'get' of undefined"

看起来有两个问题:

1) fetch未定义(需要install/require吗?)

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

编辑 您还需要使其余代码兼容。根据获取规范,响应公开的 bodyReadableStream,这可能会导致您的错误。从它的界面可以看出,它还公开了 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
  })