Skip to content

Commit

Permalink
fix(webhooks): add oauth access token to request
Browse files Browse the repository at this point in the history
Fixes #3722
  • Loading branch information
sogehige committed May 11, 2020
1 parent 5e45fa9 commit e41fd7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
30 changes: 15 additions & 15 deletions src/bot/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class API extends Core {
}
*/

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;
if ((needToWait || notEnoughAPICalls)) {
Expand Down Expand Up @@ -705,7 +705,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/kraken/channels/${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
if (needToWait) {
return { state: false, opts };
Expand Down Expand Up @@ -807,7 +807,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/users/?id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;
if (needToWait || notEnoughAPICalls) {
Expand Down Expand Up @@ -849,7 +849,7 @@ class API extends Core {
async getLatest100Followers () {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/users/follows?to_id=${cid}&first=100`;
const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;

Expand Down Expand Up @@ -913,7 +913,7 @@ class API extends Core {

const cid = oauth.channelId;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;

Expand Down Expand Up @@ -1001,7 +1001,7 @@ class API extends Core {
}

try {
const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
if (token === '') {
throw new Error('token not available');
}
Expand Down Expand Up @@ -1051,7 +1051,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/streams/tags?broadcaster_id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;
if (needToWait || notEnoughAPICalls) {
Expand Down Expand Up @@ -1102,7 +1102,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/streams?user_id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;
if (needToWait || notEnoughAPICalls) {
Expand Down Expand Up @@ -1320,7 +1320,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/streams/tags?broadcaster_id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
if (needToWait) {
setTimeout(() => this.setTags(sender, tagsArg), 1000);
Expand Down Expand Up @@ -1376,7 +1376,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/kraken/channels/${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
if (needToWait) {
return { response: '', status: false };
Expand Down Expand Up @@ -1459,7 +1459,7 @@ class API extends Core {
}
const url = `https://api.twitch.tv/kraken/search/games?query=${encodeURIComponent(game)}&type=suggest`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
if (token === '') {
return;
}
Expand Down Expand Up @@ -1583,7 +1583,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/clips?broadcaster_id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 30 && this.calls.bot.refresh > Date.now() / 1000;
if (needToWait || notEnoughAPICalls) {
Expand Down Expand Up @@ -1633,7 +1633,7 @@ class API extends Core {
}
const url = `https://api.twitch.tv/kraken/users/${id}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
if (token === '') {
return;
}
Expand Down Expand Up @@ -1681,7 +1681,7 @@ class API extends Core {
const cid = oauth.channelId;
const url = `https://api.twitch.tv/helix/users/follows?from_id=${id}&to_id=${cid}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
const needToWait = isNil(cid) || cid === '' || token === '';
const notEnoughAPICalls = this.calls.bot.remaining <= 40 && this.calls.bot.refresh > Date.now() / 1000;
if (needToWait || notEnoughAPICalls) {
Expand Down Expand Up @@ -1812,7 +1812,7 @@ class API extends Core {
async getClipById (id) {
const url = `https://api.twitch.tv/helix/clips/?id=${id}`;

const token = await oauth.botAccessToken;
const token = oauth.botAccessToken;
if (token === '') {
return null;
}
Expand Down
14 changes: 10 additions & 4 deletions src/bot/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class Webhooks {
clearTimeout(this.timeouts[`unsubscribe-${type}`]);

const cid = oauth.channelId;
const clientId = await oauth.botClientId;
if (cid === '' || clientId === '') {
const clientId = oauth.botClientId;
const token = oauth.botAccessToken;
if (cid === '' || clientId === '' || token === '') {
this.timeouts[`unsubscribe-${type}`] = setTimeout(() => this.subscribe(type), 1000);
return;
}
Expand All @@ -72,6 +73,7 @@ class Webhooks {
method: 'post',
url: 'https://api.twitch.tv/helix/webhooks/hub',
headers: {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
'Content-Type': 'application/json',
},
Expand All @@ -87,6 +89,7 @@ class Webhooks {
method: 'post',
url: 'https://api.twitch.tv/helix/webhooks/hub',
headers: {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
'Content-Type': 'application/json',
},
Expand All @@ -104,8 +107,9 @@ class Webhooks {
clearTimeout(this.timeouts[`subscribe-${type}`]);

const cid = oauth.channelId;
const clientId = await oauth.botClientId;
if (cid === '' || clientId === '') {
const clientId = oauth.botClientId;
const token = oauth.botAccessToken;
if (cid === '' || clientId === '' || token === '') {
this.timeouts[`subscribe-${type}`] = setTimeout(() => this.subscribe(type), 1000);
return;
}
Expand All @@ -126,6 +130,7 @@ class Webhooks {
method: 'post',
url: 'https://api.twitch.tv/helix/webhooks/hub',
headers: {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
'Content-Type': 'application/json',
},
Expand All @@ -147,6 +152,7 @@ class Webhooks {
method: 'post',
url: 'https://api.twitch.tv/helix/webhooks/hub',
headers: {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
'Content-Type': 'application/json',
},
Expand Down

0 comments on commit e41fd7e

Please sign in to comment.