MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / handler

Function handler

plugins/rank.ts:46–110  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

44 groupOnly: true,
45
46 async handler(sock: any, message: any, args: any, context: BotContext) {
47 const chatId = context.chatId || message.key.remoteJid
48
49 try {
50 const messageCounts = await loadMessageCounts()
51 const groupCounts = messageCounts[chatId] || {}
52
53 // Build lid -> real JID map from group participants
54 const lidMap: Record<string, string> = {};
55 let meta: any = null;
56 try {
57 meta = await sock.groupMetadata(chatId);
58 for (const p of meta.participants) {
59 if (p.lid) lidMap[p.lid] = p.id;
60 if (p.id) lidMap[p.id] = p.id;
61 }
62 } catch {}
63
64 // Resolve lid JIDs in groupCounts
65 const resolvedCounts: Record<string, number> = {};
66 for (const [uid, count] of Object.entries(groupCounts)) {
67 const resolved = lidMap[uid] || uid;
68 resolvedCounts[resolved] = (resolvedCounts[resolved] || 0) + (count as number);
69 }
70
71 const sortedMembers = Object.entries(resolvedCounts)
72 .sort(([, a], [, b]) => (b as number) - (a as number))
73 .slice(0, 5)
74
75 if (sortedMembers.length === 0) {
76 await sock.sendMessage(chatId, {
77 text: '📊 *No message activity recorded yet*\n\nStart chatting to appear on the leaderboard!'
78 }, { quoted: message })
79 return
80 }
81
82 const medals = ['🥇', '🥈', '🥉', '4️⃣', '5️⃣']
83 let messageText = '🏆 *TOP MEMBERS LEADERBOARD*\n\n'
84
85 for (let index = 0; index < sortedMembers.length; index++) {
86 const [userId, count] = sortedMembers[index];
87 // Try all sources for name
88 const c = sock.store?.contacts?.[userId];
89 const participant = meta?.participants?.find((p: any) => p.id === userId || p.lid === userId);
90 const username = c?.name || c?.notify
91 || participant?.notify || participant?.name
92 || await sock.getName(userId)
93 || (userId.includes('@s.whatsapp.net') ? '+' + userId.replace('@s.whatsapp.net', '') : 'Unknown');
94 messageText += `${medals[index]} @${username}\n💬 ${count} messages\n\n`;
95 }
96
97 messageText += '_Keep chatting to climb the ranks!_'
98
99 await sock.sendMessage(chatId, {
100 text: messageText,
101 mentions: sortedMembers.map(([userId]) => userId)
102 }, { quoted: message })
103

Callers

nothing calls this directly

Calls 1

loadMessageCountsFunction · 0.70

Tested by

no test coverage detected