mtfosbot/libs/line-message/commands/index.js

115 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-07-13 05:30:54 +00:00
const DB = require('@libs/database')
2018-06-28 15:41:16 +00:00
const actions = require('./actions')
2018-06-27 09:50:56 +00:00
const parseCMD = async (text = '', source = {}) => {
if (typeof text !== 'string' || text.trim().length === 0) return null
if (!source || typeof source !== 'object' || !('type' in source)) return null
let txt = text.trim()
let arr = txt.split(' ').map(t => t.trim())
if (arr.length === 0) return null
2018-07-13 08:07:49 +00:00
let cmdMsg = false
if (arr[0][0] === '!') cmdMsg = true
2018-06-27 09:50:56 +00:00
let cmd = arr[0].replace(/^!/, '')
cmd = cmd.toLowerCase()
2018-06-28 13:26:44 +00:00
2018-06-27 09:50:56 +00:00
let db = await DB.connect()
2018-06-28 15:41:16 +00:00
let reply = null
2018-06-27 09:50:56 +00:00
try {
2018-07-12 16:29:41 +00:00
// query normal command
2018-07-13 08:07:49 +00:00
if (cmdMsg) {
let result = await db.query({
text: `select "message", "group" from "public"."commands" where "cmd" = $1 and ("group" = '' or "group" = $2)`,
values: [cmd, source.groupId]
})
if (result.rowCount > 0) {
let obj = result.rows.filter(t => t.group === source.groupId)
if (obj.length === 0) obj = result.rows[0]
else obj = obj[0]
let content = obj.message
let m = content.match(/{{(.+?)}}/g)
if (m !== null && m.length > 0) {
for (let i = 0; i < m.length; i++) {
let c = m[i].replace(/^{{/, '').replace(/}}$/, '')
let carr = c.split('=')
if (carr.length > 1) c = carr
let res = await actions(c, arr.slice(1).join(' '), source)
content = content.replace(m[i], res || '')
}
2018-06-28 15:41:16 +00:00
}
2018-07-13 08:07:49 +00:00
if (content.trim().length > 0) {
reply = {
reply: content
}
2018-06-28 15:41:16 +00:00
}
}
2018-06-28 13:26:44 +00:00
}
2018-07-12 16:29:41 +00:00
if (reply === null) {
2018-07-13 08:07:49 +00:00
console.log('enter key command')
2018-07-12 16:29:41 +00:00
// query keyword commands
let keyCMD = await db.query({
2018-07-13 08:07:49 +00:00
text: `select "message", "group", "key" from "public"."key_commands" where ("group" = '' or "group" = $1)`,
2018-07-12 16:29:41 +00:00
values: [source.groupId]
})
2018-07-13 08:07:49 +00:00
console.log(keyCMD.rows)
2018-07-12 16:29:41 +00:00
if (keyCMD.rowCount > 0) {
let obj = keyCMD.rows.filter(t => t.group === '')
let obj2 = keyCMD.rows.filter(t => t.group === source.groupId)
obj = obj.map(t => {
for (let i of obj2) {
2018-07-13 08:07:49 +00:00
if (i.key === t.key && i.group !== '') return i
2018-07-12 16:29:41 +00:00
}
return t
})
2018-07-13 08:07:49 +00:00
let tmp = obj2.filter(t => {
for (let i of obj) {
if (i.key === t.key) return false
}
return true
})
obj = [...obj, ...tmp]
2018-07-12 16:29:41 +00:00
let regex = null
let txt = ''
2018-07-13 08:07:49 +00:00
console.log('obj ::: ', obj)
2018-07-12 16:29:41 +00:00
for (let i of obj) {
2018-08-21 07:12:47 +00:00
let key = i.key.replace(/(\+|\[|\]|\*|\$|\^|\(|\))/g, '\\$1')
txt += (txt.length > 0 ? '|' : '') + key
2018-07-12 16:29:41 +00:00
}
2018-07-13 16:19:33 +00:00
regex = new RegExp(`^(${txt})$`)
2018-07-13 08:07:49 +00:00
console.log(regex)
let m = text.match(regex)
console.log('match :::: ', m)
2018-07-12 16:29:41 +00:00
if (m !== null && m.length > 0) {
2018-07-13 08:07:49 +00:00
let key = obj.filter(t => t.key === m[0])
2018-07-12 16:29:41 +00:00
if (key.length > 0) {
let content = key[0].message
let m = content.match(/{{(.+?)}}/g)
if (m !== null && m.length > 0) {
for (let i = 0; i < m.length; i++) {
let c = m[i].replace(/^{{/, '').replace(/}}$/, '')
let carr = c.split('=')
if (carr.length > 1) c = carr
2018-07-13 08:07:49 +00:00
let res = await actions(c, text, source)
2018-07-12 16:29:41 +00:00
content = content.replace(m[i], res || '')
}
}
if (content.trim().length > 0) {
reply = {
reply: content
}
}
}
}
}
}
2018-06-27 09:50:56 +00:00
} catch (err) {
console.log(err)
}
2018-06-28 15:41:16 +00:00
2018-06-27 09:50:56 +00:00
db.release()
2018-06-28 15:41:16 +00:00
return reply
2018-06-27 09:50:56 +00:00
}
module.exports = parseCMD