From 2f767bd3223d110e53fde24e257603f383ac9482 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 06:31:02 -0500 Subject: [PATCH 01/16] fix: chunk whispers when its too long --- packages/dota/src/twitch/chatClient.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/dota/src/twitch/chatClient.ts b/packages/dota/src/twitch/chatClient.ts index 6b2ef451..e18407fd 100644 --- a/packages/dota/src/twitch/chatClient.ts +++ b/packages/dota/src/twitch/chatClient.ts @@ -39,6 +39,10 @@ export const chatClient = { twitchChat.emit('say', channel, `${prefix}${text}`) }, whisper: (channel: string, text: string) => { - twitchChat.emit('whisper', channel, `${prefix}${text}`) + const MAX_WHISPER_LENGTH = 10000 + const chunks = text.match(new RegExp(`.{1,${MAX_WHISPER_LENGTH}}`, 'g')) || [] + chunks.forEach((chunk) => { + twitchChat.emit('whisper', channel, `${prefix}${chunk}`) + }) }, } From df5e641029c81aa3bf72b9dd3264814a9c28f187 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 06:49:15 -0500 Subject: [PATCH 02/16] [CI] New Crowdin Translations (#454) Co-authored-by: Geczy <1036968+Geczy@users.noreply.github.com> --- packages/dota/locales/fa-IR/translation.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/dota/locales/fa-IR/translation.json b/packages/dota/locales/fa-IR/translation.json index c9efa144..684b22ac 100644 --- a/packages/dota/locales/fa-IR/translation.json +++ b/packages/dota/locales/fa-IR/translation.json @@ -13,7 +13,7 @@ "betaTester_off": "{{channel}} دیگه قابلیت بتا نیست برای فعال سازی دوباره !beta رو بزن", "betaTester_on": "{{channel}} is now a beta tester. Visit discord.dotabod.com to see the beta features. Type !beta to undo", "bets": { - "error": "Unable to open bets. $t(refreshToken)", + "error": "نشد قمار را به راه کنیم. $t(refreshToken)", "lost": "این رو که باختیم. عب نداره بعدی هم لوزه", "notScored": "همه بازی رو دبه کردن {{emote}} بازی {{matchId}} هیچ تاثیری نداشت", "open": "قمار کنید {{emote}}", @@ -21,7 +21,7 @@ "won": "بلاخره بردیم. یک تنه بازی رو حمل کردم" }, "bounties": { - "pickup": "+$t(bounties.totalGold, {\"count\": {{bountyValue}} }) from bounty ({{totalBounties}}/4) {{emote}} Thanks {{heroNames}} {{emote2}}", + "pickup": "+$t(bounties.totalGold, {\"count\": {{bountyValue}} })گلد از بانتی ({{totalBounties}}/4){{emote}} مرسی از {{heroNames}}{{emote2}}", "totalGold": "{{count}} گلد" }, "chatters": { @@ -50,16 +50,16 @@ "commands": "", "commands_everyone": "کامندهای همگانی: {{commandList}}", "commands_mods": "کامندهای مادها {{commandList}}", - "commandsPage": "To view {{- channel}}'s commands, visit {{- url}}", + "commandsPage": "برای دیدن کامندهای {{- channel}} برین تو لینک {{- url}}", "connections": { - "gsi_one": "{{count}} streamer connected to Dotabod GSI", + "gsi_one": "{{count}} استریمر از Dotabod GSI استفاده میکنن", "gsi_other": "{{count}} streamers connected to Dotabod GSI", "overlay_one": "{{count}} streamer using the overlay", "overlay_other": "{{count}} streamers using the overlay" }, "dota2pt": "میخوای چیز جدید واسه قهرمان {{- heroName}} یاد بگیری؟ این اینک رو چک کن {{- url}}", "dotabod": "من یک ربات سورس باز هستم. سازنده من {{author}}. اطلاعات بیشتر: {{- url}}", - "facet": "{{- heroName}} facet {{number}}: {{- facetTitle}} · {{- facetDescription}}", + "facet": "{{- heroName}} با فست {{number}}{{- facetTitle}}{{- facetDescription}}", "facetInvalid": "Invalid facet specified · $t(facetNotFound, {{- heroName }})", "facetNotFound": "Use !facet {{- heroName}} 1 or !facet {{- heroName}} 2 · Dotabod only knows the actually chosen facet for the streamer's current hero", "facetSelection": "{{- heroName}} selected facet: {{- facetTitle}} · {{- facetDescription}}", From 67897aad6ff28651cb1d12a1a85aa682969439f8 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 07:42:49 -0500 Subject: [PATCH 03/16] fix: add a space so my name can be clicked --- packages/dota/src/twitch/commands/dotabod.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dota/src/twitch/commands/dotabod.ts b/packages/dota/src/twitch/commands/dotabod.ts index 9a2b6399..6fb61179 100644 --- a/packages/dota/src/twitch/commands/dotabod.ts +++ b/packages/dota/src/twitch/commands/dotabod.ts @@ -11,7 +11,7 @@ commandHandler.registerCommand('dotabod', { chatClient.say( channel, - t('dotabod', { url: 'dotabod.com', author: '@techleed', lng: client.locale }), + t('dotabod', { url: 'dotabod.com', author: '@techleed ', lng: client.locale }), ) }, }) From c7c50f248af3a9b2e6df76ee6b8f96b5d88178cc Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 19:23:59 -0500 Subject: [PATCH 04/16] new: try to fix wins (dev command only for now) --- packages/dota/src/twitch/commands/test.ts | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index 92439bb2..bd6d269d 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -3,11 +3,13 @@ import { t } from 'i18next' import axios from 'axios' import type { DelayedGames } from '../../../../steam/src/types/index.js' import supabase from '../../db/supabase.js' +import { server } from '../../dota/index.js' import { gsiHandlers } from '../../dota/lib/consts.js' import { getAccountsFromMatch } from '../../dota/lib/getAccountsFromMatch.js' import MongoDBSingleton from '../../steam/MongoDBSingleton.js' import { steamSocket } from '../../steam/ws.js' import { getWinProbability2MinAgo } from '../../stratz/livematch' +import { logger } from '../../utils/logger.js' import { chatClient } from '../chatClient.js' import commandHandler, { type MessageType } from '../lib/CommandHandler.js' @@ -181,6 +183,52 @@ const handle2mDataCommand = async (message: MessageType) => { } } +async function fixWins(token: string) { + const ONE_DAY_IN_MS = 86_400_000 // 1 day in ms + const dayAgo = new Date(Date.now() - ONE_DAY_IN_MS).toISOString() + + const { data: bets } = await supabase + .from('bets') + .select('id, matchId, myTeam, userId, hero_slot') + .is('won', null) + .eq('userId', token) + .gte('created_at', dayAgo) + .order('created_at', { ascending: false }) + .range(0, 10) + + if (!bets) return + + for (const bet of bets) { + server.io + .in(bet.userId) + .fetchSockets() + .then((sockets: any) => { + if (!Array.isArray(sockets) || !sockets.length) return + + sockets[0] + .timeout(25000) + .emit( + 'requestMatchData', + { matchId: bet.matchId, heroSlot: bet.hero_slot }, + async (err: any, response: any) => { + if (typeof response?.radiantWin !== 'boolean') return + + await supabase + .from('bets') + .update({ + won: response.radiantWin && bet.myTeam === 'radiant', + lobby_type: response.lobbyType, + }) + .eq('id', bet.id) + }, + ) + }) + .catch((e) => { + logger.error('Error fetching sockets', { e }) + }) + } +} + commandHandler.registerCommand('test', { permission: 4, @@ -213,6 +261,9 @@ commandHandler.registerCommand('test', { case 'wp': handleWpCommand(message, args) break + case 'fixwins': + await fixWins(message.channel.client.token) + break default: chatClient.whisper(message.user.userId, 'Invalid command') } From cf92b1a44e2a20358f4f7587b1333fa8dafd3e1e Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 19:30:49 -0500 Subject: [PATCH 05/16] debug logs --- packages/dota/src/twitch/commands/test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index bd6d269d..dc32052c 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -183,7 +183,7 @@ const handle2mDataCommand = async (message: MessageType) => { } } -async function fixWins(token: string) { +async function fixWins(token: string, twitchChatId: string) { const ONE_DAY_IN_MS = 86_400_000 // 1 day in ms const dayAgo = new Date(Date.now() - ONE_DAY_IN_MS).toISOString() @@ -196,6 +196,8 @@ async function fixWins(token: string) { .order('created_at', { ascending: false }) .range(0, 10) + chatClient.whisper(twitchChatId, JSON.stringify(bets)) + if (!bets) return for (const bet of bets) { @@ -203,14 +205,18 @@ async function fixWins(token: string) { .in(bet.userId) .fetchSockets() .then((sockets: any) => { + chatClient.whisper(twitchChatId, 'Found some sockets') if (!Array.isArray(sockets) || !sockets.length) return + chatClient.whisper(twitchChatId, 'Emitting requestMatchData') + sockets[0] .timeout(25000) .emit( 'requestMatchData', { matchId: bet.matchId, heroSlot: bet.hero_slot }, async (err: any, response: any) => { + chatClient.whisper(twitchChatId, JSON.stringify(response)) if (typeof response?.radiantWin !== 'boolean') return await supabase @@ -262,7 +268,7 @@ commandHandler.registerCommand('test', { handleWpCommand(message, args) break case 'fixwins': - await fixWins(message.channel.client.token) + await fixWins(message.channel.client.token, message.user.userId) break default: chatClient.whisper(message.user.userId, 'Invalid command') From b62050b2f79f586432455bb3507cb656e9436c51 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 20:49:23 -0500 Subject: [PATCH 06/16] dev: use last socket --- packages/dota/src/twitch/commands/test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index dc32052c..492a776c 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -210,7 +210,8 @@ async function fixWins(token: string, twitchChatId: string) { chatClient.whisper(twitchChatId, 'Emitting requestMatchData') - sockets[0] + const lastSocket = sockets[sockets.length - 1] + lastSocket .timeout(25000) .emit( 'requestMatchData', From 509d634a423078a2b91c988febd66ac52a8c3a43 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 20:57:14 -0500 Subject: [PATCH 07/16] slot no care --- packages/dota/src/twitch/commands/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index 492a776c..ecfee5a2 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -215,7 +215,7 @@ async function fixWins(token: string, twitchChatId: string) { .timeout(25000) .emit( 'requestMatchData', - { matchId: bet.matchId, heroSlot: bet.hero_slot }, + { matchId: bet.matchId, heroSlot: bet.hero_slot || 0 }, async (err: any, response: any) => { chatClient.whisper(twitchChatId, JSON.stringify(response)) if (typeof response?.radiantWin !== 'boolean') return From c66883c73e26f887ab2c36ac6a7833ad70beb23d Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 9 Sep 2024 21:23:32 -0500 Subject: [PATCH 08/16] async fetch the bets --- packages/dota/src/twitch/commands/test.ts | 66 +++++++++++------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index ecfee5a2..3d4e0a9e 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -200,40 +200,40 @@ async function fixWins(token: string, twitchChatId: string) { if (!bets) return - for (const bet of bets) { - server.io - .in(bet.userId) - .fetchSockets() - .then((sockets: any) => { - chatClient.whisper(twitchChatId, 'Found some sockets') - if (!Array.isArray(sockets) || !sockets.length) return - - chatClient.whisper(twitchChatId, 'Emitting requestMatchData') - - const lastSocket = sockets[sockets.length - 1] - lastSocket - .timeout(25000) - .emit( - 'requestMatchData', - { matchId: bet.matchId, heroSlot: bet.hero_slot || 0 }, - async (err: any, response: any) => { - chatClient.whisper(twitchChatId, JSON.stringify(response)) - if (typeof response?.radiantWin !== 'boolean') return - - await supabase - .from('bets') - .update({ - won: response.radiantWin && bet.myTeam === 'radiant', - lobby_type: response.lobbyType, - }) - .eq('id', bet.id) - }, - ) - }) - .catch((e) => { + await Promise.all( + bets.map(async (bet) => { + const sockets = await server.io.in(bet.userId).fetchSockets() + if (!Array.isArray(sockets) || !sockets.length) return + + const lastSocket = sockets[sockets.length - 1] + try { + const response = await new Promise((resolve, reject) => { + lastSocket + .timeout(25000) + .emit( + 'requestMatchData', + { matchId: bet.matchId, heroSlot: bet.hero_slot || 0 }, + (err: any, response: any) => { + if (err) reject(err) + else resolve(response) + }, + ) + }) + + if (typeof response?.radiantWin === 'boolean') { + await supabase + .from('bets') + .update({ + won: response.radiantWin && bet.myTeam === 'radiant', + lobby_type: response.lobbyType, + }) + .eq('id', bet.id) + } + } catch (e) { logger.error('Error fetching sockets', { e }) - }) - } + } + }), + ) } commandHandler.registerCommand('test', { From 5abde07c89a13ac4f897f4076546082929f5c883 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Tue, 10 Sep 2024 09:56:36 -0500 Subject: [PATCH 09/16] dev: check why we disconnect --- packages/dota/src/twitch/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/dota/src/twitch/index.ts b/packages/dota/src/twitch/index.ts index 19607b03..1c5db04a 100644 --- a/packages/dota/src/twitch/index.ts +++ b/packages/dota/src/twitch/index.ts @@ -19,6 +19,10 @@ twitchChat.on('connect', () => { logger.info('We alive on dotabod chat server!') }) +twitchChat.on('disconnect', (reason, details) => { + logger.warn('Disconnected from dotabod chat server', { reason, details }) +}) + const lastMissingUserMessageTimestamps: Record = {} twitchChat.on( From 02c78831e2f367d2f6822c290fd4a48a5776ab6d Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Tue, 10 Sep 2024 10:41:38 -0500 Subject: [PATCH 10/16] fix: add reconnection logic --- packages/twitch-chat/src/index.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/twitch-chat/src/index.ts b/packages/twitch-chat/src/index.ts index f3a12e9c..731c532f 100644 --- a/packages/twitch-chat/src/index.ts +++ b/packages/twitch-chat/src/index.ts @@ -45,8 +45,27 @@ io.on('connection', (socket) => { hasDotabodSocket = true - socket.on('disconnect', () => { - console.log('We lost the server! Respond to all messages with "server offline"') + socket.on('reconnect', () => { + console.log('Reconnecting to the server') + hasDotabodSocket = true + }) + + socket.on('reconnect_failed', () => { + console.log('Reconnect failed') + hasDotabodSocket = false + }) + + socket.on('reconnect_error', (error) => { + console.log('Reconnect error', error) + hasDotabodSocket = false + }) + + socket.on('disconnect', (reason, details) => { + console.log( + 'We lost the server! Respond to all messages with "server offline"', + reason, + details, + ) hasDotabodSocket = false }) From 6b02c8e133c97bea263a2ee370d1fac921fe1f22 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Wed, 11 Sep 2024 10:59:31 -0500 Subject: [PATCH 11/16] dev: add more whispers --- packages/dota/src/twitch/commands/test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index 3d4e0a9e..5b4ad3d3 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -204,7 +204,7 @@ async function fixWins(token: string, twitchChatId: string) { bets.map(async (bet) => { const sockets = await server.io.in(bet.userId).fetchSockets() if (!Array.isArray(sockets) || !sockets.length) return - + chatClient.whisper(twitchChatId, 'Emitting requestMatchData') const lastSocket = sockets[sockets.length - 1] try { const response = await new Promise((resolve, reject) => { @@ -214,6 +214,7 @@ async function fixWins(token: string, twitchChatId: string) { 'requestMatchData', { matchId: bet.matchId, heroSlot: bet.hero_slot || 0 }, (err: any, response: any) => { + chatClient.whisper(twitchChatId, JSON.stringify(response)) if (err) reject(err) else resolve(response) }, From 539c401205c4fc392d77bf855dfb481c18719bf0 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Wed, 11 Sep 2024 11:50:12 -0500 Subject: [PATCH 12/16] fix: update wl overlay, add kda, scores to !test fixwins --- packages/dota/src/twitch/commands/test.ts | 31 ++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/dota/src/twitch/commands/test.ts b/packages/dota/src/twitch/commands/test.ts index 5b4ad3d3..7a93c5e0 100644 --- a/packages/dota/src/twitch/commands/test.ts +++ b/packages/dota/src/twitch/commands/test.ts @@ -6,6 +6,7 @@ import supabase from '../../db/supabase.js' import { server } from '../../dota/index.js' import { gsiHandlers } from '../../dota/lib/consts.js' import { getAccountsFromMatch } from '../../dota/lib/getAccountsFromMatch.js' +import { heroes } from '../../dota/lib/heroes' import MongoDBSingleton from '../../steam/MongoDBSingleton.js' import { steamSocket } from '../../steam/ws.js' import { getWinProbability2MinAgo } from '../../stratz/livematch' @@ -183,28 +184,36 @@ const handle2mDataCommand = async (message: MessageType) => { } } -async function fixWins(token: string, twitchChatId: string) { +async function fixWins(token: string, twitchChatId: string, currentMatchId?: string) { const ONE_DAY_IN_MS = 86_400_000 // 1 day in ms const dayAgo = new Date(Date.now() - ONE_DAY_IN_MS).toISOString() const { data: bets } = await supabase .from('bets') - .select('id, matchId, myTeam, userId, hero_slot') + .select('id, matchId, myTeam, userId, hero_name') .is('won', null) .eq('userId', token) + .neq('matchId', currentMatchId) .gte('created_at', dayAgo) .order('created_at', { ascending: false }) .range(0, 10) - chatClient.whisper(twitchChatId, JSON.stringify(bets)) + chatClient.whisper( + twitchChatId, + bets?.map((b) => b.matchId).join(', ') || 'No broken games found', + ) if (!bets) return await Promise.all( bets.map(async (bet) => { + const heroId = bet?.hero_name ? heroes[bet.hero_name as keyof typeof heroes]?.id || 0 : 0 const sockets = await server.io.in(bet.userId).fetchSockets() if (!Array.isArray(sockets) || !sockets.length) return - chatClient.whisper(twitchChatId, 'Emitting requestMatchData') + chatClient.whisper( + twitchChatId, + `Requesting opendota match data from overlay for match "${bet.matchId}" with hero id "${heroId}"...`, + ) const lastSocket = sockets[sockets.length - 1] try { const response = await new Promise((resolve, reject) => { @@ -212,7 +221,7 @@ async function fixWins(token: string, twitchChatId: string) { .timeout(25000) .emit( 'requestMatchData', - { matchId: bet.matchId, heroSlot: bet.hero_slot || 0 }, + { matchId: bet.matchId, heroId }, (err: any, response: any) => { chatClient.whisper(twitchChatId, JSON.stringify(response)) if (err) reject(err) @@ -225,6 +234,9 @@ async function fixWins(token: string, twitchChatId: string) { await supabase .from('bets') .update({ + radiant_score: response?.radiantScore, + dire_score: response?.direScore, + kda: { kills: response?.kills, deaths: response?.deaths, assists: response?.assists }, won: response.radiantWin && bet.myTeam === 'radiant', lobby_type: response.lobbyType, }) @@ -235,6 +247,9 @@ async function fixWins(token: string, twitchChatId: string) { } }), ) + + const handler = gsiHandlers.get(token) + handler?.emitWLUpdate() } commandHandler.registerCommand('test', { @@ -270,7 +285,11 @@ commandHandler.registerCommand('test', { handleWpCommand(message, args) break case 'fixwins': - await fixWins(message.channel.client.token, message.user.userId) + await fixWins( + message.channel.client.token, + message.user.userId, + message.channel.client.gsi?.map?.matchid, + ) break default: chatClient.whisper(message.user.userId, 'Invalid command') From ef648a1340f43f96edc087fe2985f671859fc962 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Wed, 11 Sep 2024 11:50:24 -0500 Subject: [PATCH 13/16] fix: update wl overlay, add kda, scores to !test fixwins --- packages/dota/src/dota/lib/heroes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dota/src/dota/lib/heroes.ts b/packages/dota/src/dota/lib/heroes.ts index 0ed2f655..cf05bdc5 100644 --- a/packages/dota/src/dota/lib/heroes.ts +++ b/packages/dota/src/dota/lib/heroes.ts @@ -1,6 +1,6 @@ import { t } from 'i18next' -const heroes = { +export const heroes = { npc_dota_hero_antimage: { id: 1, localized_name: 'Anti-Mage', From 15b0b9dcf00934dba3ff58c7bf05a0a32cd69bc1 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Thu, 12 Sep 2024 10:01:00 -0400 Subject: [PATCH 14/16] removed: this is bugged --- packages/dota/src/db/watcher.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/dota/src/db/watcher.ts b/packages/dota/src/db/watcher.ts index 0bc4f79d..c41856ae 100644 --- a/packages/dota/src/db/watcher.ts +++ b/packages/dota/src/db/watcher.ts @@ -5,9 +5,8 @@ import { server } from '../dota/index.js' import findUser from '../dota/lib/connectedStreamers.js' import { didTellUser, gsiHandlers } from '../dota/lib/consts.js' import { getRankDetail } from '../dota/lib/ranks.js' -import { DBSettings, getValueOrDefault } from '../settings.js' +import { DBSettings } from '../settings.js' import { chatClient } from '../twitch/chatClient.js' -import { twitchChat } from '../twitch/index.js' import { toggleDotabod } from '../twitch/toggleDotabod.js' import { logger } from '../utils/logger.js' import getDBUser from './getDBUser.js' @@ -128,9 +127,9 @@ class SetupSupabase { const ONE_DAY_IN_MS = 86_400_000 // 1 day in ms const dayAgo = new Date(Date.now() - ONE_DAY_IN_MS).toISOString() - const hasNewestScopes = client.Account?.scope?.includes('channel:bot') + // const hasNewestScopes = client.Account?.scope?.includes('channel:bot') const requiresRefresh = client.Account?.requires_refresh - if ((!hasNewestScopes || requiresRefresh) && !didTellUser.has(client.name)) { + if (requiresRefresh && !didTellUser.has(client.name)) { didTellUser.add(client.name) const { data, error } = await supabase From e8915349cdc6b700cd5bb2461f26db3a1c9be3fd Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 16 Sep 2024 07:26:21 -0400 Subject: [PATCH 15/16] fix: remove & re-add the twurple user if scopes change --- packages/dota/src/db/watcher.ts | 2 ++ packages/dota/src/twitch/lib/getTwitchAPI.ts | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/packages/dota/src/db/watcher.ts b/packages/dota/src/db/watcher.ts index c41856ae..8217a905 100644 --- a/packages/dota/src/db/watcher.ts +++ b/packages/dota/src/db/watcher.ts @@ -7,6 +7,7 @@ import { didTellUser, gsiHandlers } from '../dota/lib/consts.js' import { getRankDetail } from '../dota/lib/ranks.js' import { DBSettings } from '../settings.js' import { chatClient } from '../twitch/chatClient.js' +import { updateTwurpleTokenForTwitchId } from '../twitch/lib/getTwitchAPI' import { toggleDotabod } from '../twitch/toggleDotabod.js' import { logger } from '../utils/logger.js' import getDBUser from './getDBUser.js' @@ -85,6 +86,7 @@ class SetupSupabase { const client = findUser(newObj.userId) if (client?.Account) { client.Account.scope = newObj.scope + updateTwurpleTokenForTwitchId(newObj.providerAccountId) } } diff --git a/packages/dota/src/twitch/lib/getTwitchAPI.ts b/packages/dota/src/twitch/lib/getTwitchAPI.ts index 77037cdd..3d5d520e 100644 --- a/packages/dota/src/twitch/lib/getTwitchAPI.ts +++ b/packages/dota/src/twitch/lib/getTwitchAPI.ts @@ -39,3 +39,11 @@ export const getTwitchAPI = (twitchId: string): ApiClient => { return api } + +// Remove and re-add the user to get the new tokens in Twurple cache +export function updateTwurpleTokenForTwitchId(twitchId: string) { + const authProvider = getAuthProvider() + + authProvider.removeUser(twitchId) + getTwitchAPI(twitchId) +} From d19459a2f741c84b5fec6cf9b237a4a377d9b6f3 Mon Sep 17 00:00:00 2001 From: Matt Gates Date: Mon, 16 Sep 2024 07:29:42 -0400 Subject: [PATCH 16/16] fix: remove & re-add the twurple user if scopes change --- packages/dota/src/db/watcher.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/dota/src/db/watcher.ts b/packages/dota/src/db/watcher.ts index 8217a905..f56323c4 100644 --- a/packages/dota/src/db/watcher.ts +++ b/packages/dota/src/db/watcher.ts @@ -86,6 +86,11 @@ class SetupSupabase { const client = findUser(newObj.userId) if (client?.Account) { client.Account.scope = newObj.scope + client.Account.access_token = newObj.access_token + client.Account.refresh_token = newObj.refresh_token + client.Account.expires_at = newObj.expires_at + client.Account.expires_in = newObj.expires_in + client.Account.obtainment_timestamp = new Date(newObj.obtainment_timestamp ?? '') updateTwurpleTokenForTwitchId(newObj.providerAccountId) } }