add key word command not fin
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const api = require('../../../api-action')
|
||||
const DB = require('../../../database')
|
||||
const axios = require('axios')
|
||||
|
||||
async function run (fn = null, txt, source) {
|
||||
if (!fn || typeof fn !== 'function') return null
|
||||
@@ -447,6 +448,29 @@ const addYoutube = async (txt = '', source = {}, db) => {
|
||||
}
|
||||
}
|
||||
|
||||
const image = async (txt, source, db) => {
|
||||
if (typeof txt !== 'string' || txt.trim().length === 0) return null
|
||||
let imgs = txt.split(';')
|
||||
if (imgs.length !== 2) return null
|
||||
|
||||
try {
|
||||
await axios({
|
||||
url: imgs[0],
|
||||
method: 'head'
|
||||
})
|
||||
await axios({
|
||||
url: imgs[1],
|
||||
method: 'head'
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
return null
|
||||
}
|
||||
return {
|
||||
reply: `$image$${txt}`
|
||||
}
|
||||
}
|
||||
|
||||
const hello = async () => {
|
||||
return {reply: 'Hello World'}
|
||||
}
|
||||
@@ -458,5 +482,6 @@ module.exports = {
|
||||
addtwitch: run.bind(null, addTwitch),
|
||||
deltwitch: run.bind(null, delTwitch),
|
||||
addyoutube: run.bind(null, addYoutube),
|
||||
hello: run.bind(null, hello)
|
||||
hello: run.bind(null, hello),
|
||||
image: run.bind(null, image)
|
||||
}
|
||||
|
||||
@@ -24,9 +24,10 @@ for (let i of actGrpFile) {
|
||||
}
|
||||
|
||||
const runAct = async (cmd, txt = '', source = {}) => {
|
||||
if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null
|
||||
if (!(cmd in cmds)) return null
|
||||
let result = await cmds[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
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
let reply = null
|
||||
|
||||
try {
|
||||
// query normal command
|
||||
let result = await db.query({
|
||||
text: `select "message", "group" from "public"."commands" where "cmd" = $1 and ("group" = '' or "group" = $2)`,
|
||||
values: [cmd, source.groupId]
|
||||
@@ -28,6 +29,8 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
if (m !== null && m.length > 0) {
|
||||
for (let i = 0; i < m.length; i++) {
|
||||
let c = m[i].replace(/^{{/, '').replace(/}}$/, '')
|
||||
let carr = c.split('=')
|
||||
if (carr.length > 1) c = carr
|
||||
let res = await actions(c, arr.slice(1).join(' '), source)
|
||||
content = content.replace(m[i], res || '')
|
||||
}
|
||||
@@ -38,6 +41,52 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (reply === null) {
|
||||
// query keyword commands
|
||||
let keyCMD = await db.query({
|
||||
text: `select "message", "group", "cmd" from "public"."key_commands" where ("group" = '' or "group" = $1)`,
|
||||
values: [source.groupId]
|
||||
})
|
||||
if (keyCMD.rowCount > 0) {
|
||||
let obj = keyCMD.rows.filter(t => t.group === '')
|
||||
let obj2 = keyCMD.rows.filter(t => t.group === source.groupId)
|
||||
obj = obj.map(t => {
|
||||
for (let i of obj2) {
|
||||
if (i.cmd === t.cmd && i.group !== '') return i
|
||||
}
|
||||
return t
|
||||
})
|
||||
let regex = null
|
||||
let txt = ''
|
||||
for (let i of obj) {
|
||||
txt += (txt.length > 0 ? '|' : '') + i.cmd
|
||||
}
|
||||
regex = new RegExp(`(${txt})`)
|
||||
let m = arr.slice(1).join(' ').match(regex)
|
||||
if (m !== null && m.length > 0) {
|
||||
let key = obj.filter(t => t.cmd === m[0])
|
||||
if (key.length > 0) {
|
||||
let content = key[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 carr = c.split('=')
|
||||
if (carr.length > 1) c = carr
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user