-1

我目前正在从以前的代码中设置一个高级队列机器人,但我需要从 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); 
    })})
    })
  }
}

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

我曾尝试在 fetch 和删除 get 之间切换,但我对下一步做什么感到困惑

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

4

1 回答 1

0

看起来有两个问题:

1)fetch未定义(您需要安装/需要它吗?)

2)get不是 fetch 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

编辑 您还需要使其余代码兼容。根据 fetch 规范,body响应公开的是 a 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
  })
于 2019-09-07T20:23:55.777 回答