add comment

This commit is contained in:
Jay 2018-02-13 23:30:09 +08:00
parent 79eed290d4
commit efbbed07ed
5 changed files with 36 additions and 14 deletions

View File

@ -8,7 +8,8 @@ module.exports = {
redirect_uri: process.env.REDIRECT_URI || '',
client_id: process.env.CLIENT_ID || '',
secret: process.env.SECRET || '',
refresh_token: process.env.REFRESH_TOKEN || ''
refresh_token: process.env.REFRESH_TOKEN || '',
bot_account: process.env.BOT_ACCOUNT || ''
},
database: {
host: process.env.DB_HOST || 'localhost',

View File

@ -3,4 +3,4 @@ require('dotenv').config()
// load server
require('./app')
// load chatbot
require('./libs/botClient')
require('./libs/botClient')

View File

@ -10,7 +10,9 @@ const {
/** @type {WebSocket} */
var ws = null
runBot()
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')
@ -38,16 +40,25 @@ function handleExit(code) {
}
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])
msgSplit(ws, darr[i]).then(() => {})
// 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
@ -67,6 +78,7 @@ async function handleOpen() {
}
}
body = await new Promise((resolve, reject) => {
// get new token with refresh_token
request(options, function (error, response, body) {
if (error) {
reject(error)
@ -82,14 +94,19 @@ async function handleOpen() {
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) {
@ -99,4 +116,4 @@ async function handleOpen() {
}
}
module.exports = ws
module.exports = ws

View File

@ -1,8 +0,0 @@
class MessageQueue {
constructor () {
this.queue = {}
}
}
module.exports = new MessageQueue()

View File

@ -1,4 +1,5 @@
const dbPool = require('./database')
const config = require('../config')
/**
* @param {WebSocket} ws
* @param {string} msg
@ -8,16 +9,23 @@ const msgSplit = async function (ws, msg) {
let txtarr = msg.split(' ')
if (txtarr.length > 2) {
// server heartbeat message
if (/^ping$/i.test(txtarr[0])) {
// reply server heartbeat
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 === 'mtfosbot') return
if (user === config.twitch.bot_account) return
let channel = txtarr[2]
// get message
txtarr = txtarr.slice(3, txtarr.length)
// remove message first :
let m = txtarr.join(' ').substr(1)
let result = await parseCMD(user, channel, m)
@ -39,6 +47,7 @@ const parseCMD = async function (user, channel, msg) {
let m = null
try {
let txt = msg.trim().split(' ')
// get channel name without hashtag
let ch = channel[0] === '#' ? channel.substr(1) : channel
if (txt.length < 1) {
await db.end()
@ -56,12 +65,15 @@ const parseCMD = async function (user, channel, msg) {
"mtime" = now()`
let param = [cmd, ch, tmpmsg]
await db.query(query, param)
m = `command "${cmd}" has added`
// remove cmd
} else if (/^!-/.test(txt[0])) {
let cmd = txt[0].substr(2)
let query = `update "public"."commands" set "active" = $1, "mtime" = now() where "command" = $2 and "channel" = $3`
let param = [false, cmd, ch]
await db.query(query, param)
m = `command "${cmd}" has removed`
// run cmd
} 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`