diff --git a/libs/twitch-bot/index.js b/libs/twitch-bot/index.js index 3f5f07f..7b4ea69 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 () { @@ -64,20 +65,32 @@ class WS { }) // release pool connection - await db.replace() + 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 }