-1

I'm currently setting up a premium queue bot from previous code but I need to switch from snekfetch to node fetch

I've tried using the get function yet to no avail it comes out as nonexistent

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); 
    })})
    })
  }
}

Usually the bot would start but this error pops up

I have tried switching around fetch and removing get but I am confused on what do to next

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

anthonygood
  • 402
  • 4
  • 8

1 Answers1

0

Looks like there are two problems:

1) fetch is undefined (Do you need to install/require it?)

2) get is not part of the fetch API

For a GET request you can just do fetch('<url>').

Putting the two together:

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

EDIT You also need to make the rest of your code fetch-compliant. As per the fetch spec, the body exposed by the response is a ReadableStream, which is probably causing your error. As you can see from its interface it also exposes text() and json() methods:

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

I presume the response is JSON so you'll want to use 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
  })
anthonygood
  • 402
  • 4
  • 8