To start off I am a noob at this coding game. started a couple of days ago. i am making a chatbot for twitch to use in my channel for fun. got the bot up and going and started to make commands. I am hoping to create an uptime command and have managed to get as far as requesting the data from the helix API but I am now completely stumped on how to "use" it.
const querystring = require("querystring"),
fetch = require("node-fetch");
const CLIENT_ID = "###";
const STREAMS_URL = "https://api.twitch.tv/helix/streams/";
var started_at = ""
const qs = querystring.stringify({
user_login: "lil__gizmo"
});
const qUrl = `${STREAMS_URL}?${qs}`;
const fetchArgs = {
headers: {
"Client-ID": CLIENT_ID,
'Authorization': 'Bearer ' + "###"
}
};
fetch(qUrl, fetchArgs)
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
the response I get is
{
data: [
{
id: '38236753072',
user_id: '63931875',
user_name: 'Lil__Gizmo',
game_id: '497057',
type: 'live',
title: 'FEAR THE REAPERS!!!',
viewer_count: 6,
started_at: '2020-05-23T13:09:18Z',
language: 'en',
thumbnail_url: 'https://static-cdn.jtvnw.net/previews-ttv/live_user_lil__gizmo-{width}x{height}.jpg',
tag_ids: [Array]
}
],
pagination: {}
}
I am trying to take the started_at
property of the returned object and use it in my code.
You can use async-await
syntax
You making async operation so you have to do this operation in function.
let data;
async function getData() {
try {
data = await fetch(qUrl, fetchArgs).then(res => res.json());
} catch (error) {
console.log(error);
}
}
getData();
//Wait for 2 minutes to get data from server
setTimeout(() => {
console.log(data.data[0].started_at);
}, 2000);