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 DB = require('../../database')
|
||||||
const groupCMD = require('./group')
|
const actions = require('./actions')
|
||||||
|
|
||||||
const cmds = {}
|
|
||||||
|
|
||||||
for (let i in groupCMD) {
|
|
||||||
cmds[i] = groupCMD[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
const parseCMD = async (text = '', source = {}) => {
|
const parseCMD = async (text = '', source = {}) => {
|
||||||
if (typeof text !== 'string' || text.trim().length === 0) return null
|
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
|
if (arr[0][0] !== '!') return null
|
||||||
let cmd = arr[0].replace(/^!/, '')
|
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 db = await DB.connect()
|
||||||
|
let reply = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let text = `select "mrssage" from "public"."commands" where cmd = $1`
|
|
||||||
let values = [cmd]
|
|
||||||
let result = await db.query({
|
let result = await db.query({
|
||||||
text,
|
text: `select "message" from "public"."commands" where "cmd" = $1`,
|
||||||
values
|
values: [cmd]
|
||||||
})
|
})
|
||||||
if (result.rowCount === 0) {
|
if (result.rowCount > 0) {
|
||||||
db.release()
|
let content = result.rows[0].message
|
||||||
return null
|
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) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
db.release()
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
db.release()
|
db.release()
|
||||||
|
return reply
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = parseCMD
|
module.exports = parseCMD
|
||||||
|
Loading…
Reference in New Issue
Block a user