diff --git a/background.js b/background.js index 1413a1c..ce55f6c 100644 --- a/background.js +++ b/background.js @@ -280,9 +280,10 @@ const checkDonate = async (loginName = null, opayid = null) => { }) if ('data' in result && Array.isArray(result.data) && result.data.length > 0) { let ids = result.data.map(t => t.donateid) + let inparams = ids.map((t, idx) => `$${(idx + 1)}`) let dbres = await db.query({ - text: `select * from "public"."donate_list" where "donate_id" in ($1)`, - values: [ids] + text: `select * from "public"."donate_list" where "donate_id" in (${inparams.join(',')})`, + values: [...ids] }) let list = result.data.filter(t => { for (let i of dbres.rows) { @@ -290,7 +291,24 @@ const checkDonate = async (loginName = null, opayid = null) => { } return true }) - + for (let i of list) { + let text = `insert into "public"."donate_list" ("opayid", "donate_id", "price", "text", "name") values + ($1, $2, $3, $4, $5)` + let values = [opayid, i.donateid, i.amount || 0, i.msg || '', i.name || ''] + try { + await db.query({ + text, + values + }) + let msg = `/me 感謝 ${i.name} 贊助了 ${i.amount} 元, ${i.msg}` + event.emit('twitchSend', { + msg, + channel: loginName + }) + } catch (err) { + console.log(err) + } + } } } catch (err) { console.log(err) diff --git a/bin/dbVersion.json b/bin/dbVersion.json index 3a2ad2d..0ab91fd 100644 --- a/bin/dbVersion.json +++ b/bin/dbVersion.json @@ -10,7 +10,8 @@ {"file": "20180712-1.sql", "version": 8}, {"file": "20180716-1.sql", "version": 9}, {"file": "20180718-1.sql", "version": 10}, - {"file": "20180719-1.sql", "version": 11} + {"file": "20180719-1.sql", "version": 11}, + {"file": "20180721-1.sql", "version": 12} ], "test": [] } \ No newline at end of file diff --git a/libs/twitch-bot/index.js b/libs/twitch-bot/index.js index f94fa33..e0b893a 100644 --- a/libs/twitch-bot/index.js +++ b/libs/twitch-bot/index.js @@ -5,17 +5,30 @@ const { msgSplit } = require('./parser') const event = require('@root/event') +// const _ = require('lodash') class WS { constructor () { /** @type {WebSocket} */ this.ws = null + // cache struct + // cache = { + // [channel]: { + // time: timestamp, + // list: [] + // } + // } + this.cache = {} + this.tick = null this.join = [] + this.tick = setInterval(this.runTick.bind(this), 1000) event.on('twitchJoin', (channel) => { this.joinChannel(channel) }) event.on('twitchSend', data => { - this.sendMsg(data.channel, data.message) + if (!('msg' in data) || typeof data.msg !== 'string') return + if (!('channel' in data) || typeof data.channel !== 'string') return + this.queueMsg(data.channel, data.msg) }) } @@ -66,7 +79,7 @@ class WS { this.ws.send('CAP REQ :twitch.tv/membership :twitch.tv/commands') // 取得要加入的頻道列表 - let text = `select "name" from "public"."twitch_channel where "join" = true` + let text = `select "name" from "public"."twitch_channel" where "join" = true` let result = await db.query({ text }) @@ -85,6 +98,45 @@ class WS { } } + queueMsg (channel, msg) { + if (typeof channel !== 'string' || channel.trim().length === 0) return + if (typeof msg !== 'string' || msg.trim().length === 0) return + if (channel in this.cache) { + if ('list' in this.cache[channel]) { + this.cache[channel].list = [...this.cache[channel].list, msg] + } else { + this.cache[channel].list = [msg] + } + } else { + this.cache[channel] = { + time: -1, + list: [msg] + } + } + } + + runTick () { + if (this.cache === null || typeof this.cache !== 'object') return + let time = Date.now() + for (let i in this.cache) { + if ('time' in this.cache[i] && this.cache[i].time <= (time - 1500)) { + if ('list' in this.cache[i] && Array.isArray(this.cache[i].list) && this.cache[i].list.length > 0) { + let msg = this.cache[i].list[0] + this.cache[i].list = [...this.cache[i].list.slice(1)] + this.sendMsg(i, msg) + } + this.cache[i].time = time + } else { + if ('list' in this.cache[i] && Array.isArray(this.cache[i].list) && this.cache[i].list.length > 0) { + let msg = this.cache[i].list[0] + this.cache[i].list = [...this.cache[i].list.slice(1)] + this.sendMsg(i, msg) + } + this.cache[i].time = time + } + } + } + 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 diff --git a/package.json b/package.json index a8d7ca0..9011edc 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "koa-body": "^4.0.3", "koa-logger": "^3.2.0", "koa-router": "^7.4.0", + "lodash": "^4.17.10", "module-alias": "^2.1.0", "pg": "^7.4.3", "raw-body": "^2.3.3", diff --git a/schema/20180721-1.sql b/schema/20180721-1.sql new file mode 100644 index 0000000..fcb9dcd --- /dev/null +++ b/schema/20180721-1.sql @@ -0,0 +1,3 @@ +ALTER TABLE public.donate_list DROP CONSTRAINT donate_list_pkey; +ALTER TABLE public.donate_list ADD CONSTRAINT donate_list_opayid_donate_id_pk PRIMARY KEY (opayid, donate_id); +ALTER TABLE public.donate_list DROP id; \ No newline at end of file