-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
158 lines (156 loc) · 5.91 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Helper from './lib/helper.js'
import { plugins } from './lib/plugins.js'
import { format } from 'util'
import { fileURLToPath } from 'url'
import path, { join } from 'path'
import etc from "./etc.js";
import {
yellow,
bgGreen,
black,
bgMagenta,
bgBlue,
red,
bgCyan
} from 'colorette';
export async function handler(chatUpdate) {
if (!chatUpdate)
return;
let m = chatUpdate
try {
m = m
if (!m)
return
if (!m.fromMe && !Helper.isOwner(m) && etc.opts.self)
return
let usedPrefix
const ___dirname = path.join(path.dirname(fileURLToPath(import.meta.url)), './plugins')
for (let name in plugins) {
let plugin = plugins[name]
if (!plugin)
continue
if (plugin.disabled)
continue
const __filename = join(___dirname, name)
if (typeof plugin.all === 'function') {
try {
await plugin.all.call(this, m, {
chatUpdate,
__dirname: ___dirname,
__filename
})
} catch (e) {
console.error(e)
}
}
const str2Regex = str => str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
let _prefix = plugin.customPrefix ? plugin.customPrefix : this.prefix ? this.prefix : Helper.prefix
let match = (_prefix instanceof RegExp ? // RegExp Mode?
[[_prefix.exec(m.body), _prefix]] :
Array.isArray(_prefix) ? // Array?
_prefix.map(p => {
let re = p instanceof RegExp ? // RegExp in Array?
p :
new RegExp(str2Regex(p))
return [re.exec(m.body), re]
}) :
typeof _prefix === 'string' ? // String?
[[new RegExp(str2Regex(_prefix)).exec(m.body), new RegExp(str2Regex(_prefix))]] :
[[[], new RegExp]]
).find(p => p[1])
if (typeof plugin.before === 'function') {
if (await plugin.before.call(this, m, {
match,
conn: this,
chatUpdate,
__dirname: ___dirname,
__filename
}))
continue
}
if (typeof plugin !== 'function')
continue
if ((usedPrefix = (match[0] || '')[0])) {
let noPrefix = m.body.replace(usedPrefix, '')
let [command, ...args] = noPrefix.trim().split` `.filter(v => v)
args = args || []
let _args = noPrefix.trim().split` `.slice(1)
let text = _args.join` `
command = (command || '').toLowerCase()
let fail = plugin.fail || Helper.dfail // When failed
let isAccept = plugin.command instanceof RegExp ? plugin.command.test(command) : Array.isArray(plugin.command) ? plugin.command.some(cmd => cmd instanceof RegExp ? cmd.test(command) : cmd === command) : typeof plugin.command === 'string' ? plugin.command === command : false
if (!isAccept)
continue
if (plugin.owner && !Helper.isOwner(m)) {
fail('owner', m)
continue
}
if (plugin.group && !(await Helper.isGroup(m))) {
fail('group', m)
continue
} else if (plugin.botAdmin && !(await Helper.isBotAdmin(m))) {
fail('botAdmin', m)
continue
} else if (plugin.admin && !(await Helper.isAdmin(m))) {
fail('admin', m)
continue
}
if (plugin.private && (await Helper.isGroup(m))) {
fail('private', m, this)
continue
}
m.isCommand = true
let extra = {
match,
usedPrefix,
noPrefix,
_args,
args,
command,
text,
conn: this,
chatUpdate,
__dirname: ___dirname,
__filename
}
try {
await plugin.call(this, m, extra)
} catch (e) {
m.error = e
console.error(e)
if (e) {
let text = format(e)
for (let key of Object.values(etc.APIKeys))
text = text.replace(new RegExp(key, 'g'), '#HIDDEN#')
m.reply(text)
}
} finally {
if (typeof plugin.after === 'function') {
try {
await plugin.after.call(this, m, extra)
} catch (e) {
console.error(e)
}
}
}
break
}
}
} catch (e) {
console.error(e)
} finally {
await printMessage(m, this)
}
}
async function printMessage(m, conn) {
const chat = await m.getChat()
const contact = await m.getContact()
console.log(`\n${black(bgGreen('%s'))} from ${black(bgMagenta('~ %s'))} ${black(bgCyan('%s'))} to ${black(bgMagenta('~ %s'))} ${black(bgBlue('%s'))}`,
m.type,
contact.verifiedName || contact.pushname || m._data.notifyName,
contact.id._serialized,
chat.name,
chat.id._serialized
)
console.log(m.error != null ? red(m.body) : m.isCommand ? yellow(m.body) : m.body)
}