refresh (both) auth tokens on startup

This commit is contained in:
2025-04-03 11:19:31 +02:00
parent 66b410a0cd
commit 9f3d207032
5 changed files with 14 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
BOT_NAME=
CHANNEL=
CLIENT_ID=
CLIENT_SECRET=

View File

@@ -28,6 +28,7 @@ services:
- action: rebuild
path: ./src
environment:
- BOT_NAME=$BOT_NAME
- CHANNEL=$CHANNEL
# The following env variables can be removed once the bot has sucessfully run once
- CLIENT_ID=$CLIENT_ID

View File

@@ -2,6 +2,7 @@ import { Bot } from '@twurple/easy-bot'
import authProvider from './lib/auth';
import commands from './commands'
import api, { broadcasterAuthProvider } from './lib/api';
const channel = process.env.CHANNEL ?? ''
@@ -13,6 +14,13 @@ const bot = new Bot({
bot.onConnect(async () => {
// await authProvider.refreshAccessTokenForUser(238377856)
const name = await api.users.getUserByName(process.env.BOT_NAME!)
await authProvider.refreshAccessTokenForUser(name?.id!)
if (broadcasterAuthProvider) {
const broadcastername = await api.users.getUserByName(channel)
await broadcasterAuthProvider.refreshAccessTokenForUser(broadcastername?.id!)
}
setTimeout(() => {
console.log('Bot is ready to accept commands!')
}, 1000 * 5)

View File

@@ -2,5 +2,6 @@ import authProvider, { broadcasterAuthProvider as BCAuthFunction } from "../lib/
import { ApiClient } from "@twurple/api";
const api = new ApiClient({ authProvider })
export default api
const broadcasterApi = BCAuthFunction !== undefined ? new ApiClient({ authProvider: await BCAuthFunction() }) : undefined
export { broadcasterApi }
const broadcasterAuthProvider = BCAuthFunction !== undefined ? await BCAuthFunction() : undefined
const broadcasterApi = broadcasterAuthProvider !== undefined ? new ApiClient({ authProvider: broadcasterAuthProvider }) : undefined
export { broadcasterApi, broadcasterAuthProvider }

View File

@@ -80,7 +80,7 @@ authProvider.onRefresh(async (_id, newTokenData) => {
const ttvauthid = await pb.collection('ttvauth').getFirstListItem('main=true')
await pb.collection('ttvauth').update(ttvauthid.id, { auth })
console.log("Refreshed OAuth tokens.")
console.log("Refreshed Bot OAuth tokens.")
})
export default authProvider