donate notify fin v1

This commit is contained in:
Jay 2018-07-21 00:46:01 +08:00
parent 9d964268e8
commit a767ef3869
5 changed files with 81 additions and 6 deletions

View File

@ -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)

View File

@ -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": []
}

View File

@ -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

View File

@ -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",

3
schema/20180721-1.sql Normal file
View File

@ -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;