2018-06-27 09:50:56 +00:00
|
|
|
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(/^!/, '')
|
2018-06-28 13:26:44 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-06-27 09:50:56 +00:00
|
|
|
let db = await DB.connect()
|
|
|
|
try {
|
2018-06-28 13:26:44 +00:00
|
|
|
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
|
|
|
|
}
|
2018-06-27 09:50:56 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
2018-06-28 13:26:44 +00:00
|
|
|
db.release()
|
|
|
|
return null
|
2018-06-27 09:50:56 +00:00
|
|
|
}
|
|
|
|
db.release()
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseCMD
|