key command fin v1
This commit is contained in:
@@ -7,7 +7,8 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
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 cmdMsg = false
|
||||
if (arr[0][0] === '!') cmdMsg = true
|
||||
let cmd = arr[0].replace(/^!/, '')
|
||||
cmd = cmd.toLowerCase()
|
||||
|
||||
@@ -16,56 +17,70 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
|
||||
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]
|
||||
})
|
||||
if (result.rowCount > 0) {
|
||||
let obj = result.rows.filter(t => t.group === source.groupId)
|
||||
if (obj.length === 0) obj = result.rows[0]
|
||||
else obj = obj[0]
|
||||
let content = obj.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 (cmdMsg) {
|
||||
let result = await db.query({
|
||||
text: `select "message", "group" from "public"."commands" where "cmd" = $1 and ("group" = '' or "group" = $2)`,
|
||||
values: [cmd, source.groupId]
|
||||
})
|
||||
if (result.rowCount > 0) {
|
||||
let obj = result.rows.filter(t => t.group === source.groupId)
|
||||
if (obj.length === 0) obj = result.rows[0]
|
||||
else obj = obj[0]
|
||||
let content = obj.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
|
||||
if (content.trim().length > 0) {
|
||||
reply = {
|
||||
reply: content
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (reply === null) {
|
||||
console.log('enter key command')
|
||||
// query keyword commands
|
||||
let keyCMD = await db.query({
|
||||
text: `select "message", "group", "cmd" from "public"."key_commands" where ("group" = '' or "group" = $1)`,
|
||||
text: `select "message", "group", "key" from "public"."key_commands" where ("group" = '' or "group" = $1)`,
|
||||
values: [source.groupId]
|
||||
})
|
||||
console.log(keyCMD.rows)
|
||||
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
|
||||
if (i.key === t.key && i.group !== '') return i
|
||||
}
|
||||
return t
|
||||
})
|
||||
let tmp = obj2.filter(t => {
|
||||
for (let i of obj) {
|
||||
if (i.key === t.key) return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
obj = [...obj, ...tmp]
|
||||
let regex = null
|
||||
let txt = ''
|
||||
console.log('obj ::: ', obj)
|
||||
for (let i of obj) {
|
||||
txt += (txt.length > 0 ? '|' : '') + i.cmd
|
||||
txt += (txt.length > 0 ? '|' : '') + i.key
|
||||
}
|
||||
regex = new RegExp(`(${txt})`)
|
||||
let m = arr.slice(1).join(' ').match(regex)
|
||||
console.log(regex)
|
||||
let m = text.match(regex)
|
||||
console.log('match :::: ', m)
|
||||
if (m !== null && m.length > 0) {
|
||||
let key = obj.filter(t => t.cmd === m[0])
|
||||
let key = obj.filter(t => t.key === m[0])
|
||||
if (key.length > 0) {
|
||||
let content = key[0].message
|
||||
let m = content.match(/{{(.+?)}}/g)
|
||||
@@ -74,7 +89,7 @@ const parseCMD = async (text = '', source = {}) => {
|
||||
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)
|
||||
let res = await actions(c, text, source)
|
||||
content = content.replace(m[i], res || '')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user