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=
|
CHANNEL=
|
||||||
CLIENT_ID=
|
CLIENT_ID=
|
||||||
CLIENT_SECRET=
|
CLIENT_SECRET=
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ services:
|
|||||||
- action: rebuild
|
- action: rebuild
|
||||||
path: ./src
|
path: ./src
|
||||||
environment:
|
environment:
|
||||||
|
- BOT_NAME=$BOT_NAME
|
||||||
- CHANNEL=$CHANNEL
|
- CHANNEL=$CHANNEL
|
||||||
# The following env variables can be removed once the bot has sucessfully run once
|
# The following env variables can be removed once the bot has sucessfully run once
|
||||||
- CLIENT_ID=$CLIENT_ID
|
- CLIENT_ID=$CLIENT_ID
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Bot } from '@twurple/easy-bot'
|
|||||||
|
|
||||||
import authProvider from './lib/auth';
|
import authProvider from './lib/auth';
|
||||||
import commands from './commands'
|
import commands from './commands'
|
||||||
|
import api, { broadcasterAuthProvider } from './lib/api';
|
||||||
|
|
||||||
const channel = process.env.CHANNEL ?? ''
|
const channel = process.env.CHANNEL ?? ''
|
||||||
|
|
||||||
@@ -13,6 +14,13 @@ const bot = new Bot({
|
|||||||
|
|
||||||
bot.onConnect(async () => {
|
bot.onConnect(async () => {
|
||||||
// await authProvider.refreshAccessTokenForUser(238377856)
|
// 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(() => {
|
setTimeout(() => {
|
||||||
console.log('Bot is ready to accept commands!')
|
console.log('Bot is ready to accept commands!')
|
||||||
}, 1000 * 5)
|
}, 1000 * 5)
|
||||||
|
|||||||
@@ -2,5 +2,6 @@ import authProvider, { broadcasterAuthProvider as BCAuthFunction } from "../lib/
|
|||||||
import { ApiClient } from "@twurple/api";
|
import { ApiClient } from "@twurple/api";
|
||||||
const api = new ApiClient({ authProvider })
|
const api = new ApiClient({ authProvider })
|
||||||
export default api
|
export default api
|
||||||
const broadcasterApi = BCAuthFunction !== undefined ? new ApiClient({ authProvider: await BCAuthFunction() }) : undefined
|
const broadcasterAuthProvider = BCAuthFunction !== undefined ? await BCAuthFunction() : undefined
|
||||||
export { broadcasterApi }
|
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')
|
const ttvauthid = await pb.collection('ttvauth').getFirstListItem('main=true')
|
||||||
await pb.collection('ttvauth').update(ttvauthid.id, { auth })
|
await pb.collection('ttvauth').update(ttvauthid.id, { auth })
|
||||||
|
|
||||||
console.log("Refreshed OAuth tokens.")
|
console.log("Refreshed Bot OAuth tokens.")
|
||||||
})
|
})
|
||||||
|
|
||||||
export default authProvider
|
export default authProvider
|
||||||
|
|||||||
Reference in New Issue
Block a user