2018-02-12 15:08:42 +00:00
|
|
|
const WebSocket = require('ws')
|
|
|
|
const config = require('../config')
|
2018-02-12 16:34:26 +00:00
|
|
|
const dbPool = require('./database')
|
|
|
|
const request = require('request')
|
|
|
|
const log = require('debug')('BOT:IRC')
|
2018-02-13 05:41:32 +00:00
|
|
|
const {
|
|
|
|
msgSplit
|
|
|
|
} = require('./twitchParser')
|
2018-02-12 15:08:42 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
/** @type {WebSocket} */
|
|
|
|
var ws = null
|
2018-02-12 15:08:42 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
runBot()
|
2018-02-13 05:16:30 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
function runBot() {
|
|
|
|
ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
|
2018-02-13 05:16:30 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
ws.on('open', handleOpen)
|
|
|
|
|
|
|
|
ws.on('message', handleMessage)
|
|
|
|
|
|
|
|
ws.on('error', handleError)
|
|
|
|
|
|
|
|
ws.on('close', handleExit)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleError(err) {
|
2018-02-13 05:16:30 +00:00
|
|
|
console.error(err)
|
2018-02-13 05:41:32 +00:00
|
|
|
ws = null
|
|
|
|
runBot()
|
|
|
|
// do reconnect
|
|
|
|
}
|
2018-02-13 05:16:30 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
function handleExit(code) {
|
|
|
|
// do reconnect
|
|
|
|
ws = null
|
|
|
|
runBot()
|
|
|
|
}
|
2018-02-13 05:16:30 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
function handleMessage(data) {
|
2018-02-13 05:16:30 +00:00
|
|
|
let d = data.toString()
|
|
|
|
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
|
|
|
|
|
|
|
|
for (let i in darr) {
|
|
|
|
log(darr[i])
|
2018-02-13 05:41:32 +00:00
|
|
|
msgSplit(ws, darr[i]).then(() => {})
|
2018-02-13 05:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
async function handleOpen() {
|
2018-02-12 16:34:26 +00:00
|
|
|
let db = await dbPool.connect()
|
2018-02-12 15:08:42 +00:00
|
|
|
|
2018-02-12 16:34:26 +00:00
|
|
|
let body = null
|
|
|
|
try {
|
|
|
|
let options = {
|
|
|
|
method: 'POST',
|
|
|
|
url: config.twitch.token_url,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'Accept': 'application/json'
|
|
|
|
},
|
|
|
|
form: {
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
refresh_token: config.twitch.refresh_token,
|
|
|
|
client_id: config.twitch.client_id,
|
|
|
|
client_secret: config.twitch.secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body = await new Promise((resolve, reject) => {
|
|
|
|
request(options, function (error, response, body) {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
return
|
|
|
|
}
|
2018-02-13 05:16:30 +00:00
|
|
|
if (typeof body === 'string') body = JSON.parse(body)
|
2018-02-12 16:34:26 +00:00
|
|
|
resolve(body)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
2018-02-13 05:41:32 +00:00
|
|
|
|
2018-02-12 16:34:26 +00:00
|
|
|
if (body === null || !('access_token' in body)) throw new Error('access token not found')
|
|
|
|
|
|
|
|
ws.send('PASS oauth:' + body.access_token)
|
|
|
|
ws.send('NICK mtfosbot')
|
|
|
|
ws.send('CAP REQ :twitch.tv/membership :twitch.tv/commands')
|
|
|
|
|
|
|
|
let query = `select "name", "join" from "public"."channels" where "join" = $1`
|
|
|
|
let param = [true]
|
|
|
|
let result = await db.query(query, param)
|
2018-02-13 05:16:30 +00:00
|
|
|
await db.end()
|
2018-02-12 16:34:26 +00:00
|
|
|
if (result !== null && result.rows.length > 0) {
|
|
|
|
for (let row of result.rows) {
|
|
|
|
if ('name' in row) {
|
|
|
|
ws.send('JOIN #' + row.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 05:16:30 +00:00
|
|
|
}
|
2018-02-12 15:08:42 +00:00
|
|
|
|
2018-02-13 05:41:32 +00:00
|
|
|
module.exports = ws
|