twitch-bot/libs/botClient.js
2018-02-13 23:30:09 +08:00

120 lines
2.7 KiB
JavaScript

const WebSocket = require('ws')
const config = require('../config')
const dbPool = require('./database')
const request = require('request')
const log = require('debug')('BOT:IRC')
const {
msgSplit
} = require('./twitchParser')
/** @type {WebSocket} */
var ws = null
if (config.twitch.refresh_token && typeof config.twitch.refresh_token === 'string' && config.twitch.refresh_token.length > 0) {
runBot()
}
function runBot() {
ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
ws.on('open', handleOpen)
ws.on('message', handleMessage)
ws.on('error', handleError)
ws.on('close', handleExit)
}
function handleError(err) {
console.error(err)
ws = null
runBot()
// do reconnect
}
function handleExit(code) {
// do reconnect
ws = null
runBot()
}
function handleMessage(data) {
// socket message convert to string
let d = data.toString()
// split message with \n
// filter array remove empty element
// replace \r to empty
let darr = d.split(/\n/).filter(t => t).map(t => t.replace(/\r$/, ''))
for (let i in darr) {
log(darr[i])
// parse message
msgSplit(ws, darr[i]).then(() => { /* pass */ })
}
}
/**
* WebSocket Open Action
*/
async function handleOpen() {
// from pool get db connection
let db = await dbPool.connect()
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) => {
// get new token with refresh_token
request(options, function (error, response, body) {
if (error) {
reject(error)
return
}
if (typeof body === 'string') body = JSON.parse(body)
resolve(body)
})
})
} catch (err) {
console.log(err)
}
if (body === null || !('access_token' in body)) throw new Error('access token not found')
// login to twitch irc
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)
// release pool connection
await db.end()
if (result !== null && result.rows.length > 0) {
for (let row of result.rows) {
if ('name' in row) {
ws.send('JOIN #' + row.name)
}
}
}
}
module.exports = ws