mtfosbot/libs/line-message/commands/actions/index.js

35 lines
877 B
JavaScript
Raw Permalink Normal View History

2018-06-28 15:41:16 +00:00
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 = {}) => {
2018-07-12 16:29:41 +00:00
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)
2018-07-10 15:48:26 +00:00
return result.reply
2018-06-28 15:41:16 +00:00
}
module.exports = runAct