Modifs pour rewards + ajout J/leaderboard
This commit is contained in:
@@ -17,8 +17,6 @@ module.exports = modifyReward = (async (viewer_id, viewer_name, action, quantity
|
||||
// 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 }
|
||||
@@ -44,6 +42,8 @@ module.exports = modifyReward = (async (viewer_id, viewer_name, action, 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 }
|
||||
|
||||
result.status = 'update_add_entry'
|
||||
}
|
||||
else if (action === 'remove') { // Remove the quantity
|
||||
if (type === 'permanent') sql_data.count = rows[0].count - quantity
|
||||
@@ -52,15 +52,18 @@ module.exports = modifyReward = (async (viewer_id, viewer_name, action, 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
|
||||
|
||||
result.status = 'update_remove_entry'
|
||||
}
|
||||
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 }
|
||||
|
||||
result.status = 'update_set_entry'
|
||||
}
|
||||
|
||||
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) })
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const mysql = require('mysql2/promise')
|
||||
const axios = require('axios')
|
||||
require('dotenv').config()
|
||||
|
||||
module.exports = rewardRedemption = (async (viewer_id, viewer_name) => {
|
||||
module.exports = rewardRedemption = (async (viewer_id, viewer_name, client_id, access_token, id, broadcaster_id, reward_id) => {
|
||||
// Create a connection to the MySQL database
|
||||
const connection = await mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST,
|
||||
@@ -18,16 +19,15 @@ module.exports = rewardRedemption = (async (viewer_id, viewer_name) => {
|
||||
.then(async ([rows, fields]) => {
|
||||
if (rows.length === 0) {
|
||||
// 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 {
|
||||
}
|
||||
else {
|
||||
// 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) })
|
||||
@@ -38,6 +38,21 @@ module.exports = rewardRedemption = (async (viewer_id, viewer_name) => {
|
||||
// Terminate the connection to the database
|
||||
await connection.end()
|
||||
|
||||
//return [newRow.current_count, newRow.count]
|
||||
// Fullfil the reward redemption
|
||||
if (result.status === 'insert_entry' || result.status === 'update_entry') {
|
||||
await axios.patch(`https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions?id=${id}&broadcaster_id=${broadcaster_id}&reward_id=${reward_id}`, {
|
||||
status: 'FULFILLED'
|
||||
}, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).then(response => {
|
||||
console.log(response.data)
|
||||
//return response.data.data[0].status
|
||||
}).catch(error => { console.log(error.response.data) })
|
||||
}
|
||||
|
||||
return result
|
||||
})
|
||||
Reference in New Issue
Block a user