Ajout panel + récup infos depuis SQL + fix noms de fichiers
This commit is contained in:
67
app.js
67
app.js
@@ -5,9 +5,11 @@ require('dotenv').config()
|
||||
|
||||
|
||||
// UTILS
|
||||
const getReward = require('./utils/getReward')
|
||||
const getRewardData = require('./utils/getRewardData')
|
||||
const getRewardID = require('./utils/getRewardID')
|
||||
const getUserAccessToken = require('./utils/getUserAccessToken')
|
||||
const getUserID = require('./utils/getUserID')
|
||||
const getUserInfo = require('./utils/getUserInfo')
|
||||
const getUserName = require('./utils/getUserName')
|
||||
const oauthGen = require('./utils/oauthGen')
|
||||
const parseMessage = require('./utils/parseMessage')
|
||||
@@ -25,11 +27,13 @@ let user_name = process.env.TWITCH_USER_USERNAME
|
||||
|
||||
let channel_access_token = process.env.TWITCH_CHANNEL_ACCESS_TOKEN
|
||||
let channel_name = process.env.TWITCH_CHANNEL_USERNAME
|
||||
let channel_reward_name = process.env.TWITCH_CHANNEL_REWARD_NAME
|
||||
|
||||
const user_scope = ['chat:read', 'chat:edit', 'channel:moderate']
|
||||
const channel_scope = ['channel:manage:redemptions']
|
||||
|
||||
const redirect_uri = 'https://angels-dev.fr/twitch/oauth/login/'
|
||||
const uri = 'https://angels-dev.fr/twitch/'
|
||||
const redirect_uri = uri + 'oauth/login/'
|
||||
const chatBeginMsg = `PRIVMSG #${channel_name}`
|
||||
|
||||
|
||||
@@ -37,33 +41,32 @@ const chatBeginMsg = `PRIVMSG #${channel_name}`
|
||||
const port = 3000
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
app.use(express.static('panel'))
|
||||
app.use(express.static('public'))
|
||||
|
||||
// Twitch OAuth
|
||||
app.get('/twitch/oauth/:type', async (req, res) => {
|
||||
let type = req.params.type
|
||||
let url = await oauthGen(client_id, redirect_uri + type, type === 'user' ? user_scope : type === 'channel' ? channel_scope : [])
|
||||
|
||||
let url = await oauthGen(client_id, redirect_uri + type, type === 'user' ? user_scope : type === 'channel' ? channel_scope : [])
|
||||
return res.redirect(url)
|
||||
})
|
||||
|
||||
app.get('/twitch/oauth/login/:type', async (req, res) => {
|
||||
console.log(req.query)
|
||||
let type = req.params.type
|
||||
|
||||
if (type === 'user') {
|
||||
user_access_token = await getUserAccessToken(req.query.code, client_id, client_secret, redirect_uri + type)
|
||||
user_access_token = await getUserAccessToken(client_id, client_secret, req.query.code, redirect_uri + type)
|
||||
writeEnv('TWITCH_USER_ACCESS_TOKEN', user_access_token)
|
||||
|
||||
user_name = await getUserName(client_id, user_access_token)
|
||||
user_name = await getUserName(client_id, user_access_token).login
|
||||
writeEnv('TWITCH_USER_USERNAME', user_name)
|
||||
|
||||
clientChatBot.connect('wss://irc-ws.chat.twitch.tv:443')
|
||||
}
|
||||
else if (type === 'channel') {
|
||||
channel_access_token = await getUserAccessToken(req.query.code, client_id, client_secret, redirect_uri + type)
|
||||
channel_access_token = await getUserAccessToken(client_id, client_secret, req.query.code, redirect_uri + type)
|
||||
writeEnv('TWITCH_CHANNEL_ACCESS_TOKEN', channel_access_token)
|
||||
|
||||
channel_name = await getUserName(client_id, channel_access_token)
|
||||
channel_name = await getUserName(client_id, channel_access_token).login
|
||||
writeEnv('TWITCH_CHANNEL_USERNAME', channel_name)
|
||||
|
||||
clientEventSub.connect('wss://eventsub.wss.twitch.tv/ws')
|
||||
@@ -71,7 +74,27 @@ app.get('/twitch/oauth/login/:type', async (req, res) => {
|
||||
return res.send('Login successful !')
|
||||
})
|
||||
|
||||
app.listen(port, () => { console.log(`Listening at ${redirect_uri}`) })
|
||||
// Twitch Panel
|
||||
app.get('/twitch/panel/:file', async (req, res) => {
|
||||
let file = req.params.file
|
||||
|
||||
if (file === 'data') {
|
||||
//let { panel_user_id } = req.query
|
||||
let panel_user_id = '44322889'
|
||||
|
||||
//let event_user_id = '55833896'
|
||||
//let event_user_name = 'angelskimi'
|
||||
//await rewardRedemption(event_user_id, event_user_name)
|
||||
|
||||
let panel_data = await getRewardData()
|
||||
let response = { scoreboard: panel_data, user: panel_data.find(entry => entry.user_id === panel_user_id) }
|
||||
|
||||
return res.json(response)
|
||||
}
|
||||
else return res.sendFile(__dirname + '/public/panel/' + file)
|
||||
})
|
||||
|
||||
app.listen(port, () => { console.log(`Express listening at port ${port} !`) })
|
||||
|
||||
|
||||
// CHATBOT
|
||||
@@ -103,6 +126,10 @@ clientChatBot.on('connect', async connection => {
|
||||
connection.sendUTF(`@reply-parent-msg-id=${data.tags.id} ${chatBeginMsg} :Kestuveu @${data.tags['display-name']} ?`)
|
||||
connection.sendUTF(`${chatBeginMsg} :/timeout ${data.tags['display-name']} 60 T'as pas à me parler comme ça !`)
|
||||
}
|
||||
else if (message.toLowerCase().includes('quoi')) {
|
||||
connection.sendUTF(`@reply-parent-msg-id=${data.tags.id} ${chatBeginMsg} :@${data.tags['display-name']} Coubeh !`)
|
||||
connection.sendUTF(`${chatBeginMsg} :/timeout ${data.tags['display-name']} 60 T'as pas à me parler comme ça !`)
|
||||
}
|
||||
else if (message === '!ping') {
|
||||
connection.sendUTF(`@reply-parent-msg-id=${data.tags.id} ${chatBeginMsg} :Pong !`)
|
||||
}
|
||||
@@ -133,8 +160,10 @@ const clientEventSub = new WebSocketClient().on('connect', async connection => {
|
||||
if (data.metadata.message_type === 'session_welcome') {
|
||||
|
||||
// Get broadcaster user id and reward id
|
||||
let broadcaster_user_id = await getUserID(client_id, channel_access_token, channel_name)
|
||||
let reward_id = await getReward(client_id, channel_access_token, broadcaster_user_id)
|
||||
let broadcaster_user_id = await getUserID(client_id, channel_access_token)
|
||||
writeEnv('TWITCH_CHANNEL_BROADCASTER_ID', broadcaster_user_id)
|
||||
let reward_id = await getRewardID(client_id, channel_access_token, broadcaster_user_id, channel_reward_name)
|
||||
writeEnv('TWITCH_CHANNEL_REWARD_ID', reward_id)
|
||||
let topics = {
|
||||
'channel.channel_points_custom_reward_redemption.add': { version: '1', condition: { broadcaster_user_id, reward_id } },
|
||||
'stream.online': { version: '1', condition: { broadcaster_user_id } }
|
||||
@@ -145,7 +174,7 @@ const clientEventSub = new WebSocketClient().on('connect', async connection => {
|
||||
console.log(`Creating ${type}...`)
|
||||
let { version, condition } = topics[type]
|
||||
|
||||
let status = await subscribeToEvents(channel_access_token, data.payload.session.id, client_id, type, version, condition)
|
||||
let status = await subscribeToEvents(client_id, channel_access_token, data.payload.session.id, type, version, condition)
|
||||
if (!status) return console.error(`Failed to create ${type}`)
|
||||
|
||||
else if (status.error) {
|
||||
@@ -158,15 +187,13 @@ const clientEventSub = new WebSocketClient().on('connect', async connection => {
|
||||
|
||||
// Handle notification messages for reward redemption
|
||||
else if (data.metadata.message_type === 'notification') {
|
||||
let{ subscription, event } = data.payload
|
||||
let { subscription, event } = data.payload
|
||||
|
||||
//if (subscription.type === 'channel.channel_points_custom_reward_redemption.add' && event.reward.id === reward_id) {
|
||||
if (subscription.type === 'channel.channel_points_custom_reward_redemption.add') {
|
||||
let { user_id, user_name } = event
|
||||
console.log(`User ${user_name} claimed reward ${event.reward.title} !`)
|
||||
rewardRedemption(user_id, user_name)
|
||||
console.log(`User ${{ user_name } = event} claimed reward ${event.reward.title} !`)
|
||||
await rewardRedemption({ user_id, user_name } = event)
|
||||
}
|
||||
else if (subscription.type === 'stream.online' && event.broadcaster_user_login === channel_name) {
|
||||
else if (subscription.type === 'stream.online') {
|
||||
console.log(`Stream from ${event.broadcaster_user_name} is now online, connecting to chat...`)
|
||||
clientChatBot.connect('wss://irc-ws.chat.twitch.tv:443')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user