From efbbed07edc50c0ce8e764a574890eb0244a0d84 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 13 Feb 2018 23:30:09 +0800 Subject: [PATCH] add comment --- config/index.js | 3 ++- index.js | 2 +- libs/botClient.js | 23 ++++++++++++++++++++--- libs/messageQueue.js | 8 -------- libs/twitchParser.js | 14 +++++++++++++- 5 files changed, 36 insertions(+), 14 deletions(-) delete mode 100644 libs/messageQueue.js diff --git a/config/index.js b/config/index.js index f3e43fc..9eceec8 100644 --- a/config/index.js +++ b/config/index.js @@ -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', diff --git a/index.js b/index.js index ce76e98..a9542a0 100644 --- a/index.js +++ b/index.js @@ -3,4 +3,4 @@ require('dotenv').config() // load server require('./app') // load chatbot -require('./libs/botClient') \ No newline at end of file +require('./libs/botClient') diff --git a/libs/botClient.js b/libs/botClient.js index cad4e4d..cb71bd8 100644 --- a/libs/botClient.js +++ b/libs/botClient.js @@ -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 \ No newline at end of file +module.exports = ws diff --git a/libs/messageQueue.js b/libs/messageQueue.js deleted file mode 100644 index 49c5875..0000000 --- a/libs/messageQueue.js +++ /dev/null @@ -1,8 +0,0 @@ - -class MessageQueue { - constructor () { - this.queue = {} - } -} - -module.exports = new MessageQueue() diff --git a/libs/twitchParser.js b/libs/twitchParser.js index 1637808..84f4531 100644 --- a/libs/twitchParser.js +++ b/libs/twitchParser.js @@ -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`