mtfosbot/libs/line-message/commands/actions/index.js
2018-07-13 00:29:41 +08:00

35 lines
877 B
JavaScript

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' && !Array.isArray(cmd)) || cmd.length === 0) return null
let c = typeof cmd === 'string' ? cmd : cmd[0]
if (!(c in cmds)) return null
let result = await cmds[c](typeof cmd === 'string' ? txt : cmd[1] || '', source)
return result.reply
}
module.exports = runAct