Modif utils pour "require-all" + Fix refresh error

This commit is contained in:
Zachary Guénot
2023-05-11 01:23:54 +02:00
parent 6bc5e67cbc
commit e5526cc908
16 changed files with 39 additions and 50 deletions

20
app.js
View File

@@ -2,20 +2,7 @@
const WebSocketClient = require('websocket').client
const express = require('express')
require('dotenv').config()
// UTILS
const checkUser = require('./utils/checkUser')
const checkChannel = require('./utils/checkChannel')
const getRewardData = require('./utils/getRewardData')
const getRewardID = require('./utils/getRewardID')
const getUserAccessToken = require('./utils/getUserAccessToken')
const getUserInfo = require('./utils/getUserInfo')
const oauthGen = require('./utils/oauthGen')
const parseMessage = require('./utils/parseMessage')
const rewardRedemption = require('./utils/rewardRedemption')
const subscribeToEvents = require('./utils/subscribeToEvents')
const writeEnv = require('./utils/writeEnv')
require('require-all')(__dirname + '/utils/')
// VARIABLES
@@ -23,7 +10,6 @@ let client_id = process.env.TWITCH_APP_ID
let client_secret = process.env.TWITCH_APP_SECRET
let user_name = process.env.TWITCH_USER_USERNAME
let channel_name = process.env.TWITCH_CHANNEL_USERNAME
let channel_reward_name = process.env.TWITCH_CHANNEL_REWARD_NAME
@@ -94,7 +80,7 @@ app.get('/twitch/panel/:file', async (req, res) => {
else return res.sendFile(__dirname + '/public/panel/' + file)
})
app.listen(port, () => { console.log(`Express listening at port ${port} !`) })
//app.listen(port, () => { console.log(`Express listening at port ${port} !`) })
// CHATBOT
@@ -108,6 +94,7 @@ clientChatBot.on('connect', async connection => {
// Check if the user access token is still valid
let [user_access_token, user_name] = await checkUser(process.env.TWITCH_USER_ACCESS_TOKEN)
if (user_access_token === 'no_refresh') return console.log("Can't refresh user access token: ", user_name)
// Authenticate to Twitch IRC and join channel
connection.sendUTF('CAP REQ :twitch.tv/commands twitch.tv/membership twitch.tv/tags')
@@ -157,6 +144,7 @@ const clientEventSub = new WebSocketClient().on('connect', async connection => {
// Check if the channel access token is still valid
let [channel_access_token, channel_name] = await checkChannel(process.env.TWITCH_CHANNEL_ACCESS_TOKEN)
if (channel_access_token === 'no_refresh') return console.log("Can't refresh channel access token: ", channel_name)
connection.on('message', async message => {
if (message.type === 'utf8') {

View File

@@ -22,6 +22,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mysql2": "^3.3.0",
"require-all": "^3.0.0",
"websocket": "^1.0.34"
}
}

View File

@@ -7,14 +7,14 @@ const writeEnv = require('./writeEnv')
let client_id = process.env.TWITCH_APP_ID
let client_secret = process.env.TWITCH_APP_SECRET
module.exports = async function (access_token) {
module.exports = checkChannel = (async (access_token) => {
// Check if channel_access_token is valid
if (!await validateToken(access_token)) {
// If not, refresh it
let result = await refreshToken(client_id, client_secret, access_token)
if (result.status) return `Can't refresh channel_access_token: ${result.message}`
let result = await refreshToken(client_id, client_secret, process.env.TWITCH_CHANNEL_REFRESH_TOKEN)
if (result.status) return result = ['no_refresh', result.message]
let [channel_access_token, channel_refresh_token] = result
writeEnv('TWITCH_CHANNEL_ACCESS_TOKEN', channel_access_token)
@@ -23,5 +23,5 @@ module.exports = async function (access_token) {
let channel_name = await getUserInfo(client_id, channel_access_token).login
writeEnv('TWITCH_CHANNEL_USERNAME', channel_name)
}
return [channel_access_token, channel_name]
}
return result
})

View File

@@ -7,14 +7,14 @@ const writeEnv = require('./writeEnv')
let client_id = process.env.TWITCH_APP_ID
let client_secret = process.env.TWITCH_APP_SECRET
module.exports = async function (access_token) {
module.exports = checkUser = (async (access_token) => {
// Check if user_access_token is valid
if (!await validateToken(access_token)) {
// If not, refresh it
let result = await refreshToken(client_id, client_secret, access_token)
if (result.status) return `Can't refresh user_access_token: ${result.message}`
let result = await refreshToken(client_id, client_secret, process.env.TWITCH_USER_REFRESH_TOKEN)
if (result.status) return result = ['no_refresh', result.message]
let [user_access_token, user_refresh_token] = result
writeEnv('TWITCH_USER_ACCESS_TOKEN', user_access_token)
@@ -23,5 +23,5 @@ module.exports = async function (access_token) {
let user_name = await getUserInfo(client_id, user_access_token).login
writeEnv('TWITCH_USER_USERNAME', user_name)
}
return [user_access_token, user_name]
}
return result
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, client_secret) {
module.exports = getAppAccessToken = (async (client_id, client_secret) => {
return await axios.post('https://id.twitch.tv/oauth2/token', {
client_id,
client_secret,
@@ -9,4 +9,4 @@ module.exports = async function (client_id, client_secret) {
//console.log(response.data)
if (response.data.token_type === 'bearer') return response.data.access_token
}).catch(error => { console.log(error.response.data) })
}
})

View File

@@ -1,7 +1,7 @@
const mysql = require('mysql2/promise')
require('dotenv').config()
module.exports = async function () {
module.exports = getRewardData = (async () => {
// Create a connection to the MySQL database
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
@@ -21,4 +21,4 @@ module.exports = async function () {
if (!results) return { error: 'No scoreboard data found' }
return results
}
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, access_token, broadcaster_id, reward_name) {
module.exports = getRewardID = (async (client_id, access_token, broadcaster_id, reward_name) => {
return await axios.get(`https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=${broadcaster_id}`, {
headers: {
'Authorization': `Bearer ${access_token}`,
@@ -11,4 +11,4 @@ module.exports = async function (client_id, access_token, broadcaster_id, reward
let reward = response.data.data.find(reward => reward.title === reward_name)
return reward.id
}).catch(error => { console.log(error.response.data) })
}
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, client_secret, code, redirect_uri) {
module.exports = getUserAccessToken = (async (client_id, client_secret, code, redirect_uri) => {
return await axios.post('https://id.twitch.tv/oauth2/token', {
code,
client_id,
@@ -11,4 +11,4 @@ module.exports = async function (client_id, client_secret, code, redirect_uri) {
//console.log(response.data)
if (response.data.token_type === 'bearer') return [response.data.access_token, response.data.refresh_token]
}).catch(error => { console.log(error.response.data) })
}
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, access_token) {
module.exports = getUserInfo = (async (client_id, access_token) => {
return await axios.get(`https://api.twitch.tv/helix/users`, {
headers: {
'Authorization': `Bearer ${access_token}`,
@@ -10,4 +10,4 @@ module.exports = async function (client_id, access_token) {
//console.log(response.data)
return response.data.data[0]
}).catch(error => { console.log(error.response.data) })
}
})

View File

@@ -1,4 +1,4 @@
module.exports = async function (client_id, redirect_uri, scope) {
module.exports = oauthGen = (async (client_id, redirect_uri, scope) => {
console.log(scope)
let queries = {
response_type: 'code',
@@ -7,4 +7,4 @@ module.exports = async function (client_id, redirect_uri, scope) {
scope: scope.join('+')
}
return `https://id.twitch.tv/oauth2/authorize?${Object.keys(queries).map(key=>`${key}=${queries[key]}`).join('&')}`
}
})

View File

@@ -3,7 +3,7 @@
// Expects the caller to pass a single message. (Remember, the Twitch
// IRC server may send one or more IRC messages in a single message.)
module.exports = function (message) {
module.exports = parseMessage = (message => {
let parsedMessage = { // Contains the component parts.
tags: null,
@@ -82,7 +82,7 @@ module.exports = function (message) {
}
return parsedMessage;
}
})
// Parses the tags component of the IRC message.

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, client_secret, refresh_token) {
module.exports = refreshToken = (async (client_id, client_secret, refresh_token) => {
return await axios.post('https://id.twitch.tv/oauth2/token', {
client_id,
client_secret,
@@ -10,4 +10,4 @@ module.exports = async function (client_id, client_secret, refresh_token) {
//console.log(response.data)
if (response.data.token_type === 'bearer') return [response.data.access_token, response.data.refresh_token]
}).catch(error => { return error.response.data })
}
})

View File

@@ -1,6 +1,6 @@
const mysql = require('mysql2/promise')
module.exports = async function (user_id, user_name) {
module.exports = rewardRedemption = (async (user_id, user_name) => {
// Create a connection to the MySQL database
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
@@ -27,4 +27,4 @@ module.exports = async function (user_id, user_name) {
// Terminate the connection to the database
await connection.end()
}
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (client_id, access_token, session_id, type, version, condition) {
module.exports = subscribeToEvents = (async (client_id, access_token, session_id, type, version, condition) => {
return await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions', {
type,
version,
@@ -19,4 +19,4 @@ module.exports = async function (client_id, access_token, session_id, type, vers
//console.log(response.data)
return response.data.data[0].status
}).catch(error => { return error.response.data })
}
})

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (access_token) {
module.exports = validateToken = (async (access_token) => {
return await axios.get('https://id.twitch.tv/oauth2/validate', {
headers: {
'Authorization': `OAuth ${access_token}`,
@@ -10,4 +10,4 @@ module.exports = async function (access_token) {
if (response.data.status === '401') return false
else return true
}).catch(error => { console.log(error.response.data) })
}
})

View File

@@ -1,7 +1,7 @@
const fs = require('fs')
module.exports = function (variable, value) {
module.exports = writeEnv = ((variable, value) => {
let parsedFile = fs.readFileSync('./.env', 'utf8')
parsedFile = parsedFile.replace(new RegExp(`${variable}=.*`, 'g'), `${variable}=${value}`)
fs.writeFileSync('./.env', parsedFile)
}
})