Ajout panel + récup infos depuis SQL + fix noms de fichiers

This commit is contained in:
Zachary Guénot
2023-05-10 01:53:55 +02:00
parent 172f811e91
commit de01c981f8
14 changed files with 162 additions and 58 deletions

23
utils/getRewardData.js Normal file
View File

@@ -0,0 +1,23 @@
const mysql = require('mysql2/promise')
module.exports = async function () {
// 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
})
// Retrieve the count of rewards claimed by each user, 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) })
// Terminate the connection to the database
await connection.end()
if (!results) return { error: 'No scoreboard data found' }
return results
}

View File

@@ -1,13 +1,13 @@
const axios = require('axios')
module.exports = async function (client_id, access_token, broadcaster_id) {
module.exports = async function (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}`,
'Client-Id': client_id
}
}).then(response => {
console.log(response.data)
return response.data.data[0].id
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 (code, client_id, client_secret, redirect_uri) {
module.exports = async function (client_id, client_secret, code, redirect_uri) {
return await axios.post('https://id.twitch.tv/oauth2/token', {
code,
client_id,

View File

@@ -1,7 +1,7 @@
const axios = require('axios')
module.exports = async function (client_id, access_token, login) {
return await axios.get(`https://api.twitch.tv/helix/users?login=${login}`, {
module.exports = async function (client_id, access_token) {
return await axios.get(`https://api.twitch.tv/helix/users`, {
headers: {
'Authorization': `Bearer ${access_token}`,
'Client-Id': client_id

13
utils/getUserInfo.js Normal file
View File

@@ -0,0 +1,13 @@
const axios = require('axios')
module.exports = async function (client_id, access_token) {
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]
}).catch(error => { console.log(error.response.data) })
}

View File

@@ -7,7 +7,7 @@ module.exports = async function (client_id, access_token) {
'Client-Id': client_id
}
}).then(response => {
console.log(response.data)
//console.log(response.data)
return response.data.data[0].login
}).catch(error => { console.log(error.response.data) })
}

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 parseMessage(message) {
module.exports = function (message) {
let parsedMessage = { // Contains the component parts.
tags: null,

View File

@@ -1,38 +1,30 @@
const mysql = require('mysql')
const mysql = require('mysql2/promise')
module.exports = function rewardRedemption(user_id, user_name) {
module.exports = async function (user_id, user_name) {
// Create a connection to the MySQL database
const connection = mysql.createConnection({
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
})
// Connect to the database
connection.connect(error => {
if (error) return console.error(error)
console.log(`Connected to MySql database as id ${connection.threadId} !`)
})
// Check if the user already exists in the rewards table
connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_id], (error, results) => {
if (error) return console.error(error)
if (results.length === 0) {
// User doesn't exist, insert a new row
connection.query('INSERT INTO rewards SET ?', { user_id, user_name, count: 1 }, error => {
if (error) return console.error(error)
})
} else {
// User exists, update the count
const newRow = { count: results[0].count + 1 }
connection.query('UPDATE rewards SET ? WHERE user_id = ?', [newRow, user_id], error => {
if (error) return console.error(error)
})
}
})
await connection.query('SELECT * FROM rewards WHERE user_id = ?', [user_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) })
} 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) })
}
}).catch(error => { console.error(error) })
// Terminate the connection to the database
connection.end()
await connection.end()
}

View File

@@ -1,6 +1,6 @@
const axios = require('axios')
module.exports = async function (access_token, session_id, client_id, type, version, condition) {
module.exports = async function (client_id, access_token, session_id, type, version, condition) {
return await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions', {
type,
version,