update , not fin , line commands
This commit is contained in:
parent
1fdd26e27c
commit
af3cb135bf
30
libs/line-message/commands/actions/index.js
Normal file
30
libs/line-message/commands/actions/index.js
Normal file
@ -0,0 +1,30 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const cmds = {}
|
||||
|
||||
let list = fs.readdirSync(path.resolve(__dirname))
|
||||
let actGrpFile = list.filter(t => {
|
||||
if (t.startsWith('index')) return false
|
||||
let stat = fs.statSync(path.resolve(__dirname, t))
|
||||
return stat.isFile
|
||||
})
|
||||
|
||||
let allowExt = /^\.(js)$/i
|
||||
for (let i of actGrpFile) {
|
||||
let ext = path.extname(i)
|
||||
let fp = path.resolve(__dirname, i)
|
||||
|
||||
if (!allowExt.test(ext)) continue
|
||||
|
||||
let tmp = require(fp)
|
||||
for (let j in tmp) {
|
||||
cmds[j] = tmp[j]
|
||||
}
|
||||
}
|
||||
|
||||
const runAct = async (cmd, txt = '', source = {}) => {
|
||||
if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null
|
||||
}
|
||||
|
||||
module.exports = runAct
|
@ -1,11 +1,5 @@
|
||||
const DB = require('../../database')
|
||||
const groupCMD = require('./group')
|
||||
|
||||
const cmds = {}
|
||||
|
||||
for (let i in groupCMD) {
|
||||
cmds[i] = groupCMD[i]
|
||||
}
|
||||
const actions = require('./actions')
|
||||
|
||||
const parseCMD = async (text = '', source = {}) => {
|
||||
if (typeof text !== 'string' || text.trim().length === 0) return null
|
||||
@ -16,38 +10,36 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
if (arr[0][0] !== '!') return null
|
||||
let cmd = arr[0].replace(/^!/, '')
|
||||
|
||||
// find default command
|
||||
if (cmd in cmds) {
|
||||
let db = await DB.connect()
|
||||
let result = null
|
||||
try {
|
||||
result = await cmds[cmd](arr.slice(1).join(' '), source, db)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
// if (result === null) return null
|
||||
db.release()
|
||||
return result
|
||||
}
|
||||
|
||||
let db = await DB.connect()
|
||||
let reply = null
|
||||
|
||||
try {
|
||||
let text = `select "mrssage" from "public"."commands" where cmd = $1`
|
||||
let values = [cmd]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
text: `select "message" from "public"."commands" where "cmd" = $1`,
|
||||
values: [cmd]
|
||||
})
|
||||
if (result.rowCount === 0) {
|
||||
db.release()
|
||||
return null
|
||||
if (result.rowCount > 0) {
|
||||
let content = result.rows[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 res = await actions(c, arr.slice(1).join(' '), source)
|
||||
content = content.replace(m[i], res || '')
|
||||
}
|
||||
}
|
||||
if (content.trim().length > 0) {
|
||||
reply = {
|
||||
reply: content
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
db.release()
|
||||
return null
|
||||
}
|
||||
|
||||
db.release()
|
||||
return reply
|
||||
}
|
||||
|
||||
module.exports = parseCMD
|
||||
|
Loading…
Reference in New Issue
Block a user