mtfosbot/libs/twitch-bot/parser.js
2018-07-25 22:20:59 +08:00

38 lines
1.0 KiB
JavaScript

const DB = require('@libs/database') // eslint-disable-line
const config = require('@config/index')
/**
* @param {WebSocket} ws
* @param {string} msg
*/
const msgSplit = async function (ws, msg) {
if (!msg || typeof msg !== 'string') return null
let txtarr = msg.trim().split(' ')
if (txtarr.length >= 2) {
// server heartbeat message
if (/^ping$/i.test(txtarr[0])) {
// reply server heartbeat
console.log(`IRC::: < ${`PONG ${txtarr[1]}`}`)
ws.send(`PONG ${txtarr[1]}`)
return
}
// chat message
if (/^privmsg$/i.test(txtarr[1])) {
// message format
// :user!user@user.tmi.twitch.tv PRIVMSG #channel :message
let user = txtarr[0].split('!')[0].substr(1)
if (user === config.twitch.bot_account) return
let channel = txtarr[2] // eslint-disable-line
// get message
txtarr = txtarr.slice(3, txtarr.length)
// remove message first :
let m = txtarr.join(' ').substr(1) // eslint-disable-line
}
}
}
module.exports = {
msgSplit
}