-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
109 lines (95 loc) · 2.84 KB
/
Copy pathhandler.js
File metadata and controls
109 lines (95 loc) · 2.84 KB
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
function isMatch(pattern, command) {
if (!pattern) return false;
if (typeof pattern === "string") return pattern === command;
if (Array.isArray(pattern)) return pattern.includes(command);
if (pattern instanceof RegExp) return pattern.test(command);
return false;
}
function extractBody(m) {
const msg = m.message;
if (!msg) return "";
let interactiveId = "";
const params =
msg.interactiveResponseMessage?.nativeFlowResponseMessage?.paramsJson;
if (params) {
try {
const parsed = JSON.parse(params);
interactiveId = parsed.id || "";
} catch {
interactiveId = "";
}
}
return (
msg.conversation ||
msg.extendedTextMessage?.text ||
msg.imageMessage?.caption ||
msg.videoMessage?.caption ||
msg.documentMessage?.caption ||
msg.buttonsResponseMessage?.selectedButtonId ||
msg.buttonsResponseMessage?.selectedDisplayText ||
msg.listResponseMessage?.singleSelectReply?.selectedRowId ||
msg.templateButtonReplyMessage?.selectedId ||
interactiveId ||
""
);
}
export async function runCommand(conn, m, plugins) {
const prefix = ".";
const body = extractBody(m);
if (!body) return;
if (!body.startsWith(prefix)) return;
const input = body.slice(prefix.length).trim();
const [command, ...args] = input.split(/\s+/);
const text = args.join(" ");
const isButtonResponse =
m.message?.buttonsResponseMessage ||
m.message?.interactiveResponseMessage ||
m.message?.listResponseMessage ||
m.message?.templateButtonReplyMessage;
if (isButtonResponse) {
console.log(`[Button] ${m.sender} → ${body}`);
}
for (const name in plugins) {
const plugin = plugins[name];
if (!plugin) continue;
const matched =
isMatch(plugin.command, command) || isMatch(plugin.alias, command);
if (!matched) continue;
const needsArgs = plugin.required && args.length === 0 && !text;
if (needsArgs) {
const helpText = plugin.help
? `Format salah!\n${plugin.help}`
: `Format salah! Gunakan ${prefix}${command} <argumen>`;
await conn
.sendMessage(m.chat, { text: helpText }, { quoted: m })
.catch((err) => console.error(`[runCommand] gagal kirim help "${name}":`, err));
return;
}
try {
await plugin(m, {
conn,
args,
text,
command,
});
} catch (err) {
console.error(`[runCommand] plugin error "${name}":`, err);
}
return;
}
console.log(
`[runCommand] Tidak ada plugin cocok untuk command: "${command}"`,
);
}
export async function runEvent(conn, event, plugins) {
for (const name in plugins) {
const plugin = plugins[name];
if (!plugin) continue;
if (plugin.on !== event.type) continue;
try {
await plugin(event, { conn });
} catch (err) {
console.error(`event plugin error "${name}":`, err);
}
}
}