Discord.js Bot to monitor streamers

um, did i need to do anything else to my OAuth token? i keep getting an 404 error?

im fetching this https://api.twitch.tv/helix/streams?first=20

Shouldn’t get a 404 for that endpoint assuming you get GET-ing and not POSTing or other HTTP Verb

i specified GET. i set var to my Oath and Client_id. maybe it doesn’t like that

I am running this:

// The parameters of fetch

    const options = {

        method: 'GET',

        Headers: {

            'Authorization': "Bearer <OAuth>",

            'Client-ID': "<CLIENT_ID>",

            'Content-Type': 'application/json',

        },

    };

    // Sends fetch from Twitch's API

    fetch('https://api.twitch.tv/helix/streams?first=20', options)

        .then(res => console.log(res)) // expecting a json response

        .then(json =>

            fs.writeFile('data.log', JSON.stringify(json), function (err) {

                if (err) throw err;

                console.log(json);

                console.log('Updated data.log!');

            })

        )

Looks correct to me.

Assuming node-fetch on the server since fs won’t work client side.

URL and options look correct.

How interesting, I don’t know what i did but i have a 401 error now
Yes node-fetch is installed

Would this be the issue? Notice what the mouse is hovering over. The const isn’t saving the authentication part in quotes.

ah, figured it out. I’m so slow. Headers was capitalized.

If I long pull every minute (of lets say 10 people’s streams), when i get the data, how would I:

  1. identify each user (so I can compare each user to themselves from the min older data)?
  2. create a compare statement (compare each user from online to offline)?

I was looking into JSON.stringify the old data to a .log file and caching the new data. JSON.parsing both and using const and if then to compare. I imagine I am overcomplicating the process or it wont work.

https://api.twitch.tv/helix/streams?user_id=anID&user_id=anotherID

Calling https://api.twitch.tv/helix/streams?user_id=71092938&user_id=39276140

I get two payloads for the two users and the user_id is in the payload

I add in https://api.twitch.tv/helix/streams?user_id=71092938&user_id=39276140&user_id=15185913(myself) and I get no third payload

but how do I do a check to trigger an event when the data of a user changes?
thanks

iterate each key of the Object and compare to the last stored value…

Sorry, but can you elaborate a tad bit. I sorta understand but not fully

var changed = false
var last_object = {stuff:'',thing:''}

var new_object = {stuff:''.thing:''}

for (var key in new_object) {
    if (last_object.hasOwnProperty(key)) {
        if (last_object[key] != new_object[key]) {
            // a key has changed
            changed = true;
        }
    } else {
        // a key was added
        changed = true;
    }
}

You’d probably only be testing the game/gameID or title for a discord bots live status, so rather than iterate all keys you’d test specific keys

Will account for new_object getting new keys but not new_object losing keys.

Or the last method

if (JSON.stringify(last_object) != JSON.stringify(new_object)) 

Assuming keys in the same order always.

key meaning like “user_id”?

but this won’t work for multi users will it?

It will

You just load the old object for each user and compare as needed

Load as in set the values of the last and new object to a specific user in the array and set the key to something I want to check for?

“load in” refers to recoverin the last object for the user from wherever you stored it, so you can compare later

as of now, i have the returned data being JSON.stringified and sent to a .log file. Whenever I try to set a const data = require("./data.log") so i can pull the data out, i get a weird error and im not sure why