Repository navigation
Expand file tree
/
Copy pathhandler.js
More file actions
138 lines (117 loc) · 5.37 KB
/
Copy pathhandler.js
File metadata and controls
138 lines (117 loc) · 5.37 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
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
// By DranxX Creative
import { getPlugin, getAllPlugins } from './lib/pluginLoader.js';
import { isBanned } from './lib/banStore.js';
import config from './lib/config.js';
const ACTIVE_AI = new Map();
// ── Group metadata cache (same as drxai-js) ──────────────────────────
const groupMetadataCache = new Map();
const GROUP_METADATA_TTL_MS = 86400000; // 24h
const registeredSocks = new WeakSet();
async function getGroupMetadata(sock, chatJid) {
if (!registeredSocks.has(sock)) {
registeredSocks.add(sock);
sock.ev.on('group-participants.update', (update) => {
groupMetadataCache.delete(update.id);
});
sock.ev.on('groups.update', (updates) => {
for (const u of updates) groupMetadataCache.delete(u.id);
});
}
const cached = groupMetadataCache.get(chatJid);
const now = Date.now();
if (cached && (now - cached.timestamp < GROUP_METADATA_TTL_MS)) {
return cached.data;
}
try {
const data = await sock.groupMetadata(chatJid);
groupMetadataCache.set(chatJid, { data, timestamp: now });
return data;
} catch (err) {
if (cached) return cached.data;
throw err;
}
}
// ── LID→phone cache ──────────────────────────────────────────────────
const lidPhoneCache = new Map();
async function resolveLidPhone(sock, jid) {
if (!jid?.endsWith('@lid')) return null;
if (lidPhoneCache.has(jid)) return lidPhoneCache.get(jid);
try {
const pn = await sock.signalRepository?.lidMapping?.getPNForLID(jid);
if (pn) { lidPhoneCache.set(jid, pn); return pn; }
} catch {}
lidPhoneCache.set(jid, null);
return null;
}
// ── Helpers ──────────────────────────────────────────────────────────
function cleanPhone(jid) { return (jid || '').replace(/@.*$/, '').replace(/:.*$/, ''); }
// ── Main handler ─────────────────────────────────────────────────────
export async function msgHandler(upsert, sock, message) {
const _receivedAt = Date.now();
try {
if (!message?.sender) return;
const text = message.text || '';
const isGroup = message.isGroup;
const sender = message.sender;
let groupMetadata = null;
if (isGroup) {
try { groupMetadata = await getGroupMetadata(sock, message.chat); } catch {}
}
let cleanSender = cleanPhone(sender).replace(/\D/g, '');
if (sender.endsWith('@lid')) {
let resolved = null;
if (groupMetadata?.participants) {
const p = groupMetadata.participants.find(part => part.id && (part.id === sender || part.id.startsWith(cleanPhone(sender) + '@')));
if (p?.phoneNumber) resolved = p.phoneNumber;
}
if (!resolved) {
resolved = await resolveLidPhone(sock, sender);
}
if (resolved) cleanSender = cleanPhone(resolved).replace(/\D/g, '');
}
const ownerNum = String(config.owner || '').replace(/\D/g, '');
const botNum = String(config.bot || '').replace(/\D/g, '');
const isOwner = message.key.fromMe || cleanSender === ownerNum || cleanSender === botNum;
for (const p of getAllPlugins()) {
if (p.before) try { await p.before(message, { sock, text, isGroup, isOwner, config, budy: text }); } catch {}
}
if (!text) return;
const status = config.status || 'public';
if (status === 'self' && cleanSender !== botNum && cleanSender !== ownerNum) return;
if (status === 'ponly' && isGroup) return;
if (status === 'gonly' && !isGroup) return;
// Ban check (silent)
if (!isOwner) {
const senderLid = sender.endsWith('@lid') ? sender : null;
const banResult = isBanned(sender, senderLid, groupMetadata);
if (banResult.banned) return;
}
const prefix = config.prefix || '.';
let cmdName = '', args = [];
if (text.startsWith(prefix)) {
const parts = text.slice(prefix.length).trim().split(/\s+/);
cmdName = parts[0]?.toLowerCase() || '';
args = parts.slice(1);
} else if (text.startsWith('$ ') && isOwner) {
cmdName = '$'; args = [text.slice(2).trim()];
}
if (!cmdName) return;
const plugin = getPlugin(cmdName);
if (!plugin) return;
const ukey = cleanPhone(sender);
if (plugin.category === 'ai') {
const act = ACTIVE_AI.get(ukey);
if (act && Date.now() - act < 300000) return message.reply('⏳ AI sedang memproses. Tunggu selesai dulu ya.');
ACTIVE_AI.set(ukey, Date.now());
}
const ctx = { sock, message, prefix, args, q: args.join(' '), budy: text, commandName: cmdName, sender, isGroup, isOwner, ownerNum, botNum, groupMetadata, _receivedAt, senderPhone: cleanSender, pushname: message.pushName || '' };
// Execution log
const chatType = isGroup ? 'group' : 'dm';
const logLine = `[${cmdName}] ${cleanSender} (${chatType})` + (args.length ? ' args: ' + args.join(' ') : '');
console.log(logLine);
try { await plugin.handler(message, ctx); } catch (err) {
console.error('[' + cmdName + ']', err.message);
await message.reply('Error: ' + err.message).catch(() => {});
} finally { if (plugin.category === 'ai') ACTIVE_AI.delete(ukey); }
} catch (err) { console.error('[handler]', err.message); }
}