Skip to content

Commit

Permalink
fix(twitch): refresh token if invalid on call
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Dec 21, 2021
1 parent eca573d commit 5d01059
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/services/twitch/calls/checkClips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getRepository } from 'typeorm';
import { TwitchClips } from '../../../database/entity/twitch';
import { error } from '../../../helpers/log';
import client from '../api/client';
import { refresh } from '../token/refresh.js';

export async function checkClips () {
try {
Expand All @@ -25,7 +26,11 @@ export async function checkClips () {
}
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
}
return { state: true };
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/createClip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getRepository } from 'typeorm';
import { TwitchClips } from '../../../database/entity/twitch';
import { error } from '../../../helpers/log';
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { isStreamOnline } from '~/helpers/api';
import { variables } from '~/watchers';
Expand Down Expand Up @@ -42,7 +43,11 @@ export async function createClip (opts: { createAfterDelay: boolean }) {
return (await isClipChecked(clipId)) ? clipId : null;
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
}
return null;
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/createMarker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { error } from '../../../helpers/log';
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { variables } from '~/watchers';

Expand All @@ -11,7 +12,11 @@ export async function createMarker () {
clientBot.streams.createStreamMarker(channelId, 'Marked from sogeBot');
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack || e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
}
}
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getAllStreamTags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getRepository, IsNull } from 'typeorm';

import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { rawDataSymbol } from '~/../node_modules/@twurple/common/lib';
import { TwitchTag, TwitchTagLocalizationDescription, TwitchTagLocalizationName } from '~/database/entity/twitch';
Expand Down Expand Up @@ -39,7 +40,11 @@ export async function getAllStreamTags(opts: any) {
await getRepository(TwitchTagLocalizationName).delete({ tagId: IsNull() });
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
}
return { state: true, opts };
Expand Down
8 changes: 7 additions & 1 deletion src/services/twitch/calls/getBannedEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { chunk } from 'lodash';
import { getRepository } from 'typeorm';

import { refresh } from '../token/refresh.js';

import { rawDataSymbol } from '~/../node_modules/@twurple/common/lib';
import { BannedEventsTable } from '~/database/entity/bannedEvents';
import { debug, error, warning } from '~/helpers/log';
Expand Down Expand Up @@ -53,7 +55,11 @@ export async function getBannedEvents (opts: any) {
debug('api.stream', 'API: ' + JSON.stringify(getBanEvents));
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('broadcaster');
} else {
error(e.stack ?? e.message);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getChannelChattersUnofficialAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'typeorm';

import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { eventEmitter } from '~/helpers/events';
import { getAllOnlineUsernames } from '~/helpers/getAllOnlineUsernames';
Expand Down Expand Up @@ -113,7 +114,11 @@ export const getChannelChattersUnofficialAPI = async (opts: any) => {
}
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { state: false, opts };
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getChannelInformation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { gameCache, gameOrTitleChangedManually, rawStatus } from '~/helpers/api';
import {
Expand Down Expand Up @@ -69,7 +70,11 @@ export async function getChannelInformation (opts: any) {
}
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { state: false, opts };
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getChannelSubscribers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getRepository } from 'typeorm';

import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { HelixSubscription } from '~/../node_modules/@twurple/api/lib';
import { User } from '~/database/entity/user';
Expand Down Expand Up @@ -41,7 +42,11 @@ export async function getChannelSubscribers<T extends { noAffiliateOrPartnerWarn
opts.notCorrectOauthWarningSent = false;
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('broadcaster');
} else {
error(e.stack ?? e.message);
}
}
}
return { state: true, opts };
Expand Down
8 changes: 7 additions & 1 deletion src/services/twitch/calls/getCurrentStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { dayjs } from '@sogebot/ui-helpers/dayjsHelper';

import { refresh } from '../token/refresh.js';

import { isStreamOnline, setCurrentRetries, streamId, streamStatusChangeSince, streamType } from '~/helpers/api';
import {
stats as apiStats, chatMessagesAtStart,
Expand Down Expand Up @@ -70,7 +72,11 @@ export async function getCurrentStream (opts: any) {
}
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { state: false, opts };
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getCurrentStreamTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { rawDataSymbol } from '../../../../node_modules/@twurple/common/lib';
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { currentStreamTags } from '~/helpers/api';
import { error } from '~/helpers/log';
Expand All @@ -18,7 +19,11 @@ export async function getCurrentStreamTags (opts: any) {
}
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { state: false, opts };
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getCustomRewards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { error } from '~/helpers/log';
import { variables } from '~/watchers';
Expand All @@ -10,7 +11,11 @@ export const getCustomRewards = async () => {
return await clientBroadcaster.channelPoints.getCustomRewards(channelId);
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('broadcaster');
} else {
error(e.stack ?? e.message);
}
}
}
};
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getIdFromTwitch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { error } from '~/helpers/log';

Expand All @@ -13,7 +14,11 @@ async function getIdFromTwitch (userName: string): Promise<string> {
}
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
throw(e);
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getLatest100Followers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import {
stats as apiStats,
Expand Down Expand Up @@ -34,7 +35,11 @@ export async function getLatest100Followers () {
apiStats.value.currentFollowers = getFollows.total;
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { state: false };
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getModerators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getRepository, In, Not } from 'typeorm';

import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { User } from '~/database/entity/user';
import { error, warning } from '~/helpers/log';
Expand Down Expand Up @@ -34,7 +35,11 @@ export async function getModerators(opts: { isWarned: boolean }) {
setStatus('MOD', getModeratorsPaginated.map(o => o.userId).includes(botId));
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('broadcaster');
} else {
error(e.stack ?? e.message);
}
}
}
return { state: true };
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/getTopClips.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DAY } from '@sogebot/ui-helpers/constants';

import { HelixClip } from '../../../../node_modules/@twurple/api/lib';
import client from '../api/client';
import { refresh } from '../token/refresh.js';
import { getGameNameFromId } from './getGameNameFromId';

import { streamStatusChangeSince } from '~/helpers/api';
Expand Down Expand Up @@ -34,7 +35,11 @@ export async function getTopClips (opts: any) {
return shuffle(clips).slice(0, opts.first);
} catch (e) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
}
return [];
Expand Down
10 changes: 9 additions & 1 deletion src/services/twitch/calls/isFollowerUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as constants from '@sogebot/ui-helpers/constants';
import { getRepository } from 'typeorm';

import { eventEmitter } from '../../../helpers/events';
import { unfollow } from '../../../helpers/log';
import { error, unfollow } from '../../../helpers/log';
import { refresh } from '../token/refresh.js';

import { follow } from '~/helpers/events/follow';
import * as changelog from '~/helpers/user/changelog.js';
Expand Down Expand Up @@ -53,6 +54,13 @@ export async function isFollowerUpdate (user: UserInterface | null) {
return { isFollower: true, followedAt: user.followedAt };
}
} catch (e: any) {
if (e instanceof Error) {
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return null;
}
}
7 changes: 6 additions & 1 deletion src/services/twitch/calls/sendGameFromTwitch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { error } from '~/helpers/log';

Expand All @@ -9,7 +10,11 @@ async function sendGameFromTwitch (game: string) {
return searchCategories.map(o => o.name);
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/setTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from 'typeorm';

import client from '../api/client';
import { refresh } from '../token/refresh.js';

import { error } from '~/helpers/log';
import { variables } from '~/watchers';
Expand Down Expand Up @@ -33,7 +34,11 @@ async function setTags (tagsArg: string[]) {
}
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('broadcaster');
} else {
error(e.stack ?? e.message);
}
}
return false;
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/twitch/calls/setTitleAndGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { error } from 'console';
import { defaults, isNil } from 'lodash';

import client from '../api/client';
import { refresh } from '../token/refresh.js';
import { getGameIdFromName } from './getGameIdFromName';

import {
Expand Down Expand Up @@ -52,7 +53,11 @@ async function setTitleAndGame (args: { title?: string | null; game?: string | n
});
} catch (e: unknown) {
if (e instanceof Error) {
error(e.stack ?? e.message);
if (e.message === 'Invalid OAuth token') {
await refresh('bot');
} else {
error(e.stack ?? e.message);
}
}
return { response: '', status: false };
}
Expand Down
Loading

0 comments on commit 5d01059

Please sign in to comment.