Skip to content

Commit

Permalink
Fixed checker
Browse files Browse the repository at this point in the history
  • Loading branch information
colehammond65 committed May 27, 2024
1 parent 95a13fd commit c825ca4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,28 @@ async function getTwitchToken() {
const res = await fetch(`https://id.twitch.tv/oauth2/token?client_id=${config.client_id}&client_secret=${config.client_secret}&grant_type=client_credentials`, {
method: 'POST',
});

if (!res.ok) {
console.error(`Error fetching Twitch token: ${res.statusText}`);
logChannel.send(`Error fetching Twitch token: ${res.statusText}`);
return;
}

const data = await res.json();
if (!data.access_token) {
console.error('Error: Twitch token response does not contain access_token');
logChannel.send('Error: Twitch token response does not contain access_token');
return;
}

twitchToken = data.access_token;
} catch (error) {
console.error('Error fetching Twitch token:', error);
logChannel.send(`Error fetching Twitch token: ${error.message}`);
}
}


// Check if the specified Twitch streamer is live
async function TwitchCheck() {
try {
Expand All @@ -90,19 +105,35 @@ async function TwitchCheck() {
'Authorization': `Bearer ${twitchToken}`
}
});

if (!res.ok) {
console.error(`Error fetching Twitch stream data: ${res.statusText}`);
logChannel.send(`Error fetching Twitch stream data: ${res.statusText}`);
return;
}

const data = await res.json();
if (!data.data) {
console.error('Error: Twitch API response does not contain data field');
logChannel.send('Error: Twitch API response does not contain data field');
return;
}

const isLive = data.data.length > 0;
if (isLive) {
handleStreamStarted();
} else {
handleStreamEnded();
}

if (state.firstCheck) state.firstCheck = false; // Reset firstCheck after the first run
} catch (error) {
console.error('Error checking Twitch stream:', error);
logChannel.send(`Error checking Twitch stream: ${error.message}`);
}
}


// Handle when the stream starts
function handleStreamStarted() {
if (!state.ready || (!state.firstCheck && state.hasStarted)) return;
Expand Down

0 comments on commit c825ca4

Please sign in to comment.