From aec6232cc610e81c5df50f729c61342cd2b9be8e Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 16 Jul 2018 10:47:43 +0800 Subject: [PATCH 1/2] fix db release --- libs/twitch-bot/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/twitch-bot/index.js b/libs/twitch-bot/index.js index 7a446fe..bef0f3d 100644 --- a/libs/twitch-bot/index.js +++ b/libs/twitch-bot/index.js @@ -63,7 +63,7 @@ class WS { }) // release pool connection - await db.replace() + await db.release() if (result !== null && result.rowCount > 0) { for (let row of result.rows) { From cde2fb61953516a042d606dc126cb869f7eb5933 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 16 Jul 2018 16:51:31 +0800 Subject: [PATCH 2/2] modify ws class --- libs/twitch-bot/index.js | 13 +++++++++++++ libs/twitch-bot/parser.js | 10 +--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libs/twitch-bot/index.js b/libs/twitch-bot/index.js index bef0f3d..e5ec64d 100644 --- a/libs/twitch-bot/index.js +++ b/libs/twitch-bot/index.js @@ -9,6 +9,7 @@ class WS { constructor () { /** @type {WebSocket} */ this.ws = null + this.join = [] } runBot () { @@ -65,18 +66,30 @@ class WS { // release pool connection await db.release() + this.joinChannel = [] if (result !== null && result.rowCount > 0) { for (let row of result.rows) { if ('name' in row) { this.ws.send('JOIN #' + row.name) + this.join.push(row.name) } } } } + sendMsg (channel = null, message = null) { + if (this.ws === null || !('send' in this.ws) || typeof this.ws.send !== 'function') return null + if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null + if (message === null || typeof message !== 'string' || message.trim().length === 0) return null + if (this.join.indexOf(channel) === -1) return null + this.ws.send(`PRIVMSG #${channel} :${message}`) + } + joinChannel (channel = null) { + if (this.ws === null || !('send' in this.ws) || typeof this.ws.send !== 'function') return null if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null this.ws.send(`JOIN #${channel.trim()}`) + this.join.push(channel.trim()) } } diff --git a/libs/twitch-bot/parser.js b/libs/twitch-bot/parser.js index c885362..f772083 100644 --- a/libs/twitch-bot/parser.js +++ b/libs/twitch-bot/parser.js @@ -31,14 +31,6 @@ const msgSplit = async function (ws, msg) { } } -const sendMsg = async (ws, channel = null, msg = null) => { - if (!ws || !('send' in ws) || typeof ws.send !== 'function') return null - if (channel === null || typeof channel !== 'string' || channel.trim().length === 0) return null - if (msg === null || typeof msg !== 'string' || msg.trim().length === 0) return null - ws.send(`PRIVMSG #${channel} :${msg}`) -} - module.exports = { - msgSplit, - sendMsg + msgSplit }