mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 08:41:39 +01:00
refresh (both) auth tokens on startup
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
BOT_NAME=
|
||||
CHANNEL=
|
||||
CLIENT_ID=
|
||||
CLIENT_SECRET=
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user