update bot code

This commit is contained in:
Jay 2018-02-13 13:41:32 +08:00
parent 725791dd4e
commit 79eed290d4

View File

@ -3,21 +3,39 @@ const config = require('../config')
const dbPool = require('./database') const dbPool = require('./database')
const request = require('request') const request = require('request')
const log = require('debug')('BOT:IRC') const log = require('debug')('BOT:IRC')
const { msgSplit } = require('./twitchParser') const {
msgSplit
} = require('./twitchParser')
const ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc') /** @type {WebSocket} */
var ws = null
runBot()
function runBot() {
ws = new WebSocket(`wss://${config.twitch.chat_host}:443`, 'irc')
ws.on('open', handleOpen) ws.on('open', handleOpen)
ws.on('message', handleMessage) ws.on('message', handleMessage)
ws.on('error', (err) => { ws.on('error', handleError)
console.error(err)
})
ws.on('close', (code, reason) => { ws.on('close', handleExit)
console.log('exit', code, reason) }
})
function handleError(err) {
console.error(err)
ws = null
runBot()
// do reconnect
}
function handleExit(code) {
// do reconnect
ws = null
runBot()
}
function handleMessage(data) { function handleMessage(data) {
let d = data.toString() let d = data.toString()