mtfosbot/libs/line-message/commands/index.js
2018-06-28 21:26:44 +08:00

54 lines
1.2 KiB
JavaScript

const DB = require('../../database')
const groupCMD = require('./group')
const cmds = {}
for (let i in groupCMD) {
cmds[i] = groupCMD[i]
}
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
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()
try {
let text = `select "mrssage" from "public"."commands" where cmd = $1`
let values = [cmd]
let result = await db.query({
text,
values
})
if (result.rowCount === 0) {
db.release()
return null
}
} catch (err) {
console.log(err)
db.release()
return null
}
db.release()
}
module.exports = parseCMD