From af3cb135bf9143a3a552c9183cb3afd47eb789b1 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 28 Jun 2018 23:41:16 +0800 Subject: [PATCH] update , not fin , line commands --- .../commands/{ => actions}/group.js | 0 libs/line-message/commands/actions/index.js | 30 +++++++++++ libs/line-message/commands/index.js | 52 ++++++++----------- 3 files changed, 52 insertions(+), 30 deletions(-) rename libs/line-message/commands/{ => actions}/group.js (100%) create mode 100644 libs/line-message/commands/actions/index.js diff --git a/libs/line-message/commands/group.js b/libs/line-message/commands/actions/group.js similarity index 100% rename from libs/line-message/commands/group.js rename to libs/line-message/commands/actions/group.js diff --git a/libs/line-message/commands/actions/index.js b/libs/line-message/commands/actions/index.js new file mode 100644 index 0000000..12ec612 --- /dev/null +++ b/libs/line-message/commands/actions/index.js @@ -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 diff --git a/libs/line-message/commands/index.js b/libs/line-message/commands/index.js index 0837fc4..97d1201 100644 --- a/libs/line-message/commands/index.js +++ b/libs/line-message/commands/index.js @@ -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