Traitement commandes et ajout J/arrow + Fix login
This commit is contained in:
@@ -7,21 +7,26 @@ const writeEnv = require('./writeEnv')
|
||||
let client_id = process.env.TWITCH_APP_ID
|
||||
let client_secret = process.env.TWITCH_APP_SECRET
|
||||
|
||||
module.exports = checkChannel = (async (access_token) => {
|
||||
module.exports = checkChannel = (async (channel_access_token) => {
|
||||
let result = [channel_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, process.env.TWITCH_CHANNEL_REFRESH_TOKEN)
|
||||
if (!await validateToken(channel_access_token)) {
|
||||
result = await refreshToken(client_id, client_secret, process.env.TWITCH_CHANNEL_REFRESH_TOKEN)
|
||||
|
||||
if (result.status) return result = ['no_refresh', result.message]
|
||||
else {
|
||||
let [channel_access_token, channel_refresh_token] = result
|
||||
writeEnv('TWITCH_CHANNEL_ACCESS_TOKEN', channel_access_token)
|
||||
writeEnv('TWITCH_CHANNEL_REFRESH_TOKEN', channel_refresh_token)
|
||||
|
||||
let [channel_access_token, channel_refresh_token] = result
|
||||
writeEnv('TWITCH_CHANNEL_ACCESS_TOKEN', channel_access_token)
|
||||
writeEnv('TWITCH_CHANNEL_REFRESH_TOKEN', channel_refresh_token)
|
||||
|
||||
let channel_name = await getUserInfo(client_id, channel_access_token).login
|
||||
writeEnv('TWITCH_CHANNEL_USERNAME', channel_name)
|
||||
let channel_name = await getUserInfo(client_id, channel_access_token, 'login')
|
||||
writeEnv('TWITCH_CHANNEL_NAME', channel_name)
|
||||
}
|
||||
} else {
|
||||
let channel_name = await getUserInfo(client_id, channel_access_token, 'login')
|
||||
result = [channel_access_token, channel_name]
|
||||
writeEnv('TWITCH_CHANNEL_NAME', channel_name)
|
||||
}
|
||||
return result
|
||||
})
|
||||
@@ -7,21 +7,26 @@ const writeEnv = require('./writeEnv')
|
||||
let client_id = process.env.TWITCH_APP_ID
|
||||
let client_secret = process.env.TWITCH_APP_SECRET
|
||||
|
||||
module.exports = checkUser = (async (access_token) => {
|
||||
module.exports = checkUser = (async (user_access_token) => {
|
||||
let result = [user_access_token, '']
|
||||
|
||||
// Check if user_access_token is valid
|
||||
if (!await validateToken(access_token)) {
|
||||
if (!await validateToken(user_access_token)) {
|
||||
result = await refreshToken(client_id, client_secret, process.env.TWITCH_USER_REFRESH_TOKEN)
|
||||
|
||||
// If not, refresh it
|
||||
let result = await refreshToken(client_id, client_secret, process.env.TWITCH_USER_REFRESH_TOKEN)
|
||||
if (result.status) return result = ['no_refresh', result.message]
|
||||
else {
|
||||
let [user_access_token, user_refresh_token] = result
|
||||
writeEnv('TWITCH_USER_ACCESS_TOKEN', user_access_token)
|
||||
writeEnv('TWITCH_USER_REFRESH_TOKEN', user_refresh_token)
|
||||
|
||||
let [user_access_token, user_refresh_token] = result
|
||||
writeEnv('TWITCH_USER_ACCESS_TOKEN', user_access_token)
|
||||
writeEnv('TWITCH_USER_REFRESH_TOKEN', user_refresh_token)
|
||||
|
||||
let user_name = await getUserInfo(client_id, user_access_token).login
|
||||
writeEnv('TWITCH_USER_USERNAME', user_name)
|
||||
let user_name = await getUserInfo(client_id, user_access_token, 'login')
|
||||
writeEnv('TWITCH_USER_NAME', user_name)
|
||||
}
|
||||
} else {
|
||||
let user_name = await getUserInfo(client_id, user_access_token, 'login')
|
||||
result = [user_access_token, user_name]
|
||||
writeEnv('TWITCH_USER_NAME', user_name)
|
||||
}
|
||||
return result
|
||||
})
|
||||
@@ -11,7 +11,7 @@ module.exports = getRewardData = (async () => {
|
||||
database: process.env.MYSQL_DATABASE
|
||||
})
|
||||
|
||||
// Retrieve the count of rewards claimed by each user, sorted by count
|
||||
// Retrieve the count of rewards claimed by each viewer, sorted by count
|
||||
let results = await connection.execute('SELECT * FROM rewards ORDER BY count DESC')
|
||||
.then(async ([rows, fields]) => { return rows })
|
||||
.catch(error => { console.error(error) })
|
||||
|
||||
@@ -8,7 +8,7 @@ module.exports = getUserAccessToken = (async (client_id, client_secret, code, re
|
||||
redirect_uri,
|
||||
grant_type: 'authorization_code'
|
||||
}).then(response => {
|
||||
//console.log(response.data)
|
||||
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) })
|
||||
})
|
||||
13
utils/getUserID.js
Normal file
13
utils/getUserID.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const axios = require('axios')
|
||||
|
||||
module.exports = getUserID = (async (client_id, access_token, login) => {
|
||||
return await axios.get(`https://api.twitch.tv/helix/users?login=${login}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id
|
||||
}
|
||||
}).then(response => {
|
||||
//console.log(response.data)
|
||||
return response.data.data[0].id
|
||||
}).catch(error => { console.log(error.response.data) })
|
||||
})
|
||||
@@ -1,13 +1,14 @@
|
||||
const axios = require('axios')
|
||||
|
||||
module.exports = getUserInfo = (async (client_id, access_token) => {
|
||||
return await axios.get(`https://api.twitch.tv/helix/users`, {
|
||||
module.exports = getUserInfo = (async (client_id, access_token, type) => {
|
||||
return await axios.get('https://api.twitch.tv/helix/users', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id
|
||||
}
|
||||
}).then(response => {
|
||||
//console.log(response.data)
|
||||
return response.data.data[0]
|
||||
if (type === 'login') return response.data.data[0].login
|
||||
if (type === 'id') return response.data.data[0].id
|
||||
}).catch(error => { console.log(error.response.data) })
|
||||
})
|
||||
72
utils/modifyReward.js
Normal file
72
utils/modifyReward.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const mysql = require('mysql2/promise')
|
||||
require('dotenv').config()
|
||||
|
||||
module.exports = modifyReward = (async (viewer_id, viewer_name, action, quantity, type) => {
|
||||
// Create a connection to the MySQL database
|
||||
const connection = await mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST,
|
||||
port: process.env.MYSQL_PORT,
|
||||
user: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE
|
||||
})
|
||||
|
||||
const result = {}
|
||||
let sql_data = {}
|
||||
|
||||
// Check if the viewer already exists in the rewards table
|
||||
await connection.query('SELECT * FROM rewards WHERE viewer_id = ?', [viewer_id])
|
||||
.then(async ([rows, fields]) => {
|
||||
console.log(rows)
|
||||
|
||||
if (rows.length === 0) {
|
||||
// Viewer doesn't exist, insert a new row
|
||||
sql_data = { viewer_id, viewer_name }
|
||||
|
||||
if (action === 'add' || action === 'set') { // Add or set the viewer with the quantity
|
||||
if (type === 'permanent') sql_data.count = quantity
|
||||
else if (type === 'current') sql_data.current_count = quantity
|
||||
else { sql_data.count = quantity; sql_data.current_count = quantity }
|
||||
|
||||
await connection.query('INSERT INTO rewards SET ?', sql_data) .catch(error => { console.error(error) })
|
||||
return result.status = 'insert_entry'
|
||||
}
|
||||
else if (action === 'get') return result.status = 'no_get_entry' // Can't get a viewer that doesn't exist
|
||||
else if (action === 'remove') return result.status = 'no_delete_entry' // Can't remove a viewer that doesn't exist
|
||||
} else {
|
||||
// Viewer exists, update the count
|
||||
result.count = rows[0].count
|
||||
result.current_count = rows[0].current_count
|
||||
|
||||
if (action === 'get') return result.status = 'get_entry'
|
||||
|
||||
else if (action === 'add') { // Add the quantity
|
||||
if (type === 'permanent') sql_data.count = rows[0].count + quantity
|
||||
else if (type === 'current') sql_data.current_count = rows[0].current_count + quantity
|
||||
else { sql_data.count = rows[0].count + quantity; sql_data.current_count = rows[0].current_count + quantity }
|
||||
}
|
||||
else if (action === 'remove') { // Remove the quantity
|
||||
if (type === 'permanent') sql_data.count = rows[0].count - quantity
|
||||
else if (type === 'current') sql_data.current_count = rows[0].current_count - quantity
|
||||
else { sql_data.count = rows[0].count - quantity; sql_data.current_count = rows[0].current_count - quantity }
|
||||
|
||||
if (sql_data.count < 0) return result.status = 'not_enough_count' // Can't delete more than the count
|
||||
if (sql_data.current_count < 0) return result.status = 'not_enough_current_count' // Can't delete more than the current_count
|
||||
}
|
||||
else if (action === 'set') { // Set the quantity
|
||||
if (type === 'permanent') sql_data.count = quantity
|
||||
else if (type === 'current') sql_data.current_count = quantity
|
||||
else { sql_data.count = quantity; sql_data.current_count = quantity }
|
||||
}
|
||||
|
||||
await connection.query('UPDATE rewards SET ? WHERE viewer_id = ?', [sql_data, viewer_id]).catch(error => { console.error(error) })
|
||||
return result.status = 'update_entry'
|
||||
}
|
||||
}).catch(error => { console.error(error) })
|
||||
|
||||
// Terminate the connection to the database
|
||||
await connection.end()
|
||||
|
||||
result.sql_data = sql_data
|
||||
return result
|
||||
})
|
||||
@@ -1,6 +1,7 @@
|
||||
const mysql = require('mysql2/promise')
|
||||
require('dotenv').config()
|
||||
|
||||
module.exports = rewardRedemption = (async (user_id, user_name) => {
|
||||
module.exports = rewardRedemption = (async (viewer_id, viewer_name) => {
|
||||
// Create a connection to the MySQL database
|
||||
const connection = await mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST,
|
||||
@@ -10,21 +11,33 @@ module.exports = rewardRedemption = (async (user_id, user_name) => {
|
||||
database: process.env.MYSQL_DATABASE
|
||||
})
|
||||
|
||||
// Check if the user already exists in the rewards table
|
||||
await connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_id])
|
||||
const result = {}
|
||||
|
||||
// Check if the viewer already exists in the rewards table
|
||||
await connection.query('SELECT * FROM rewards WHERE viewer_id = ?', [viewer_id])
|
||||
.then(async ([rows, fields]) => {
|
||||
if (rows.length === 0) {
|
||||
// User doesn't exist, insert a new row
|
||||
await connection.query('INSERT INTO rewards SET ?', { user_id, user_name, count: 1, current_count: 1 })
|
||||
.catch(error => { console.error(error) })
|
||||
// Viewer doesn't exist, insert a new row
|
||||
|
||||
let sql_data = { viewer_id, viewer_name, count: 1, current_count: 1 }
|
||||
|
||||
await connection.query('INSERT INTO rewards SET ?', sql_data).catch(error => { console.error(error) })
|
||||
return result.status = 'insert_entry'
|
||||
} else {
|
||||
// User exists, update the count
|
||||
const newRow = { count: rows[0].count + 1, current_count: rows[0].current_count + 1 }
|
||||
await connection.query('UPDATE rewards SET ? WHERE user_id = ?', [newRow, user_id])
|
||||
.catch(error => { console.error(error) })
|
||||
// Viewer exists, update the count
|
||||
result.count = rows[0].count + 1
|
||||
result.current_count = rows[0].current_count + 1
|
||||
|
||||
let sql_data = { count: result.count, current_count: result.current_count }
|
||||
|
||||
await connection.query('UPDATE rewards SET ? WHERE viewer_id = ?', [sql_data, viewer_id]).catch(error => { console.error(error) })
|
||||
return result.status = 'update_entry'
|
||||
}
|
||||
}).catch(error => { console.error(error) })
|
||||
|
||||
// Terminate the connection to the database
|
||||
await connection.end()
|
||||
|
||||
//return [newRow.current_count, newRow.count]
|
||||
return result
|
||||
})
|
||||
@@ -7,7 +7,6 @@ module.exports = validateToken = (async (access_token) => {
|
||||
}
|
||||
}).then(response => {
|
||||
//console.log(response.data)
|
||||
if (response.data.status === '401') return false
|
||||
else return true
|
||||
return true
|
||||
}).catch(error => { console.log(error.response.data) })
|
||||
})
|
||||
Reference in New Issue
Block a user