Skip to content

Commit

Permalink
Make Twitch tokens refresh every 45 minutes, should mean at least a 1…
Browse files Browse the repository at this point in the history
…5 minute margin

Some commands stopped working, like raid, I suspect that is due to the access token timing out, so now we refresh on an interval.
  • Loading branch information
BOLL7708 committed Jan 31, 2024
1 parent fc0ed1d commit 5a78333
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Color from './ColorConstants.js'
import DataBaseHelper from './DataBaseHelper.js'
import {SettingTwitchClient, SettingTwitchTokens} from '../Objects/Setting/SettingTwitch.js'

export default class TwitchTokens {
export default class TwitchTokensHelper {
/**
* Load existing tokens for channel and chatbot (if different) and refresh them.
*/
async refreshToken() {
static async refreshToken() {
let channelTokenData = await DataBaseHelper.load(new SettingTwitchTokens(), 'Channel')
if(channelTokenData) await this.refresh(channelTokenData, 'Channel')
else Utils.log(`TwitchTokens: No tokens for Channel user, load editor to login.`, Color.Purple)
Expand All @@ -16,7 +16,7 @@ export default class TwitchTokens {
else Utils.log(`TwitchTokens: No tokens for Chatbot user, load editor to login.`, Color.Purple)
}

private async refresh(data: SettingTwitchTokens, key: string) {
private static async refresh(data: SettingTwitchTokens, key: string) {
const clientData = await DataBaseHelper.load(new SettingTwitchClient(), 'Main')
const response = await fetch('https://id.twitch.tv/oauth2/token', {
method: 'post',
Expand Down
24 changes: 16 additions & 8 deletions src/Pages/Widget/MainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import DataBaseHelper from '../../Classes/DataBaseHelper.js'
import AuthUtils from '../../Classes/AuthUtils.js'
import PasswordForm from './PasswordForm.js'
import {SettingUser} from '../../Objects/Setting/SettingUser.js'
import {
SettingTwitchClip,
SettingTwitchRedemption,
SettingTwitchReward,
SettingTwitchTokens
} from '../../Objects/Setting/SettingTwitch.js'
import {SettingTwitchClip, SettingTwitchRedemption, SettingTwitchReward, SettingTwitchTokens} from '../../Objects/Setting/SettingTwitch.js'
import {SettingDictionaryEntry} from '../../Objects/Setting/SettingDictionary.js'
import {SettingAccumulatingCounter, SettingIncrementingCounter} from '../../Objects/Setting/SettingCounters.js'
import {SettingStreamQuote} from '../../Objects/Setting/SettingStream.js'
Expand All @@ -24,6 +19,7 @@ import TwitchHelixHelper from '../../Classes/TwitchHelixHelper.js'
import {ConfigController} from '../../Objects/Config/ConfigController.js'
import EnlistData from '../../Objects/EnlistData.js'
import {DataUtils} from '../../Objects/DataUtils.js'
import TwitchTokensHelper from '../../Classes/TwitchTokensHelper.js'

export default class MainController {
public static async init() {
Expand Down Expand Up @@ -52,7 +48,7 @@ export default class MainController {

// region Init
await StatesSingleton.initInstance() // Init states
await modules.twitchTokens.refreshToken()
await this.startTwitchTokenRefreshInterval() // Init Twitch tokens
const controllerConfig = await DataBaseHelper.loadMain(new ConfigController())
if(controllerConfig.useWebsockets.twitchEventSub) modules.twitchEventSub.init().then()

Expand All @@ -62,7 +58,8 @@ export default class MainController {

// Steam Web API intervals
MainController.startSteamAchievementsInterval().then()


// TODO: Should not the player summary be active at all time in case the user has websockets on but not playing VR?
if(!controllerConfig.useWebsockets.openvr2ws) {
MainController.startSteamPlayerSummaryInterval().then()
const steamConfig = await DataBaseHelper.loadMain(new ConfigSteam())
Expand All @@ -89,6 +86,17 @@ export default class MainController {

// region Intervals

public static async startTwitchTokenRefreshInterval() {
const states = StatesSingleton.getInstance()
await TwitchTokensHelper.refreshToken()
if(states.twitchTokenRefreshIntervalHandle == -1) {
Utils.log('Starting Twitch token refresh interval', Color.Green)
states.twitchTokenRefreshIntervalHandle = setInterval(async() => {
await TwitchTokensHelper.refreshToken()
}, 1000 * 60 * 45) // 45 minutes for a chunky margin
}
}

public static async startSteamPlayerSummaryInterval() {
const states = StatesSingleton.getInstance()
const steamConfig = await DataBaseHelper.loadMain(new ConfigSteam())
Expand Down
2 changes: 0 additions & 2 deletions src/Singletons/ModulesSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import TwitchTokens from '../Classes/TwitchTokens.js'
import OBS from '../Classes/OBS.js'
import Twitch from '../Classes/Twitch.js'
import OpenVR2WS from '../Classes/OpenVR2WS.js'
Expand All @@ -23,7 +22,6 @@ export default class ModulesSingleton {
}

public twitch = new Twitch()
public twitchTokens = new TwitchTokens()
public twitchEventSub = new TwitchEventSub()
public tts = new GoogleTTS()
public pipe = new Pipe()
Expand Down
1 change: 1 addition & 0 deletions src/Singletons/StatesSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class StatesSingleton {
public scaleIntervalHandle: number = -1
public steamPlayerSummaryIntervalHandle: number = -1
public steamAchievementsIntervalHandle: number = -1
public twitchTokenRefreshIntervalHandle: number = -1
public lastSteamAppId: string|undefined
public lastSteamAppIsVR: boolean = false
public runRemoteCommands: boolean = false
Expand Down

0 comments on commit 5a78333

Please sign in to comment.