twitch-bot/libs/twitchParser.js

97 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2018-02-12 16:34:26 +00:00
const dbPool = require('./database')
2018-02-13 15:30:09 +00:00
const config = require('../config')
2018-02-12 15:08:42 +00:00
/**
2018-02-12 16:34:26 +00:00
* @param {WebSocket} ws
2018-02-12 15:08:42 +00:00
* @param {string} msg
*/
2018-02-12 16:34:26 +00:00
const msgSplit = async function (ws, msg) {
2018-02-12 15:08:42 +00:00
if (!msg || typeof msg !== 'string') return null
let txtarr = msg.split(' ')
if (txtarr.length > 2) {
2018-02-13 15:30:09 +00:00
// server heartbeat message
2018-02-12 15:08:42 +00:00
if (/^ping$/i.test(txtarr[0])) {
2018-02-13 15:30:09 +00:00
// reply server heartbeat
2018-02-12 16:34:26 +00:00
ws.send(`PONG ${txtarr[1]}`)
return
2018-02-12 15:08:42 +00:00
}
// chat message
if (/^privmsg$/i.test(txtarr[1])) {
2018-02-13 15:30:09 +00:00
// message format
// :user!user@user.tmi.twitch.tv PRIVMSG #channel :message
2018-02-12 15:08:42 +00:00
let user = txtarr[0].split('!')[0].substr(1)
2018-02-13 15:30:09 +00:00
if (user === config.twitch.bot_account) return
2018-02-12 15:08:42 +00:00
let channel = txtarr[2]
2018-02-13 15:30:09 +00:00
// get message
2018-02-12 15:08:42 +00:00
txtarr = txtarr.slice(3, txtarr.length)
2018-02-13 15:30:09 +00:00
// remove message first :
2018-02-12 15:08:42 +00:00
let m = txtarr.join(' ').substr(1)
2018-02-12 16:34:26 +00:00
let result = await parseCMD(user, channel, m)
if (result && typeof result === 'string') {
ws.send(`PRIVMSG ${channel} :${result}`)
2018-02-12 15:08:42 +00:00
}
}
}
}
2018-02-12 16:34:26 +00:00
/**
*
* @param {string} user
* @param {string} channel
* @param {string} msg
*/
const parseCMD = async function (user, channel, msg) {
let db = await dbPool.connect()
let m = null
try {
let txt = msg.trim().split(' ')
2018-02-13 15:30:09 +00:00
// get channel name without hashtag
2018-02-12 16:34:26 +00:00
let ch = channel[0] === '#' ? channel.substr(1) : channel
if (txt.length < 1) {
await db.end()
return null
}
// add cmd
if (/^!\+/.test(txt[0])) {
let cmd = txt[0].substr(2)
let tmpmsg = txt.slice(1, txt.length).join(' ')
let query = `insert into "public"."commands" ("command", "channel", "message") values
($1, $2, $3)
on conflict ("command", "channel") do update set
"message" = $3,
2018-02-13 05:16:30 +00:00
"active" = true,
"mtime" = now()`
2018-02-12 16:34:26 +00:00
let param = [cmd, ch, tmpmsg]
await db.query(query, param)
2018-02-13 15:30:09 +00:00
m = `command "${cmd}" has added`
// remove cmd
2018-02-12 16:34:26 +00:00
} else if (/^!-/.test(txt[0])) {
let cmd = txt[0].substr(2)
2018-02-13 05:16:30 +00:00
let query = `update "public"."commands" set "active" = $1, "mtime" = now() where "command" = $2 and "channel" = $3`
2018-02-12 16:34:26 +00:00
let param = [false, cmd, ch]
await db.query(query, param)
m = `command "${cmd}" has removed`
2018-02-13 15:30:09 +00:00
// run cmd
2018-02-12 16:34:26 +00:00
} else if (/^!/.test(txt[0])) {
let cmd = txt[0].substr(1)
let query = `select "message", "command" from "public"."commands" where "command" = $1 and "channel" = $2 and "active" = $3 limit 1`
let param = [cmd, ch, true]
let result = await db.query(query, param)
if (result === null || result.rows.length === 0) return null
m = result.rows[0].message
}
} catch (err) {
console.error(err)
} finally {
await db.end()
}
return m
}
2018-02-12 15:08:42 +00:00
module.exports = {
msgSplit
}