(sock, chatId, senderId)
| 1 | // isAdmin.js |
| 2 | async function isAdmin(sock, chatId, senderId) { |
| 3 | try { |
| 4 | const metadata = await sock.groupMetadata(chatId); |
| 5 | const participants = metadata.participants || []; |
| 6 | |
| 7 | // Extract bot's pure phone number |
| 8 | const botId = sock.user?.id || ''; |
| 9 | const botLid = sock.user?.lid || ''; |
| 10 | const botNumber = botId.includes(':') ? botId.split(':')[0] : (botId.includes('@') ? botId.split('@')[0] : botId); |
| 11 | const botIdWithoutSuffix = botId.includes('@') ? botId.split('@')[0] : botId; |
| 12 | |
| 13 | // Extract numeric part from bot LID (remove session identifier like :4) |
| 14 | // botLid format: "30997433344120:4@lid" -> extract "30997433344120" |
| 15 | const botLidNumeric = botLid.includes(':') ? botLid.split(':')[0] : (botLid.includes('@') ? botLid.split('@')[0] : botLid); |
| 16 | const botLidWithoutSuffix = botLid.includes('@') ? botLid.split('@')[0] : botLid; |
| 17 | |
| 18 | const senderNumber = senderId.includes(':') ? senderId.split(':')[0] : (senderId.includes('@') ? senderId.split('@')[0] : senderId); |
| 19 | const senderIdWithoutSuffix = senderId.includes('@') ? senderId.split('@')[0] : senderId; |
| 20 | |
| 21 | // Check if bot is admin |
| 22 | const isBotAdmin = participants.some(p => { |
| 23 | // Check multiple possible ID formats |
| 24 | const pPhoneNumber = p.phoneNumber ? p.phoneNumber.split('@')[0] : ''; |
| 25 | const pId = p.id ? p.id.split('@')[0] : ''; |
| 26 | const pLid = p.lid ? p.lid.split('@')[0] : ''; |
| 27 | const pFullId = p.id || ''; |
| 28 | const pFullLid = p.lid || ''; |
| 29 | |
| 30 | // Extract numeric part from participant LID (remove session identifier if present) |
| 31 | const pLidNumeric = pLid.includes(':') ? pLid.split(':')[0] : pLid; |
| 32 | |
| 33 | // Match against bot ID in multiple ways |
| 34 | const botMatches = ( |
| 35 | botId === pFullId || // Direct ID match |
| 36 | botId === pFullLid || // Direct LID match (new Baileys format) |
| 37 | botLid === pFullLid || // Bot LID vs participant LID (full match) |
| 38 | botLidNumeric === pLidNumeric || // Bot LID numeric vs participant LID numeric (KEY FIX) |
| 39 | botLidWithoutSuffix === pLid || // Bot LID without suffix vs participant LID |
| 40 | botNumber === pPhoneNumber || // Phone number match |
| 41 | botNumber === pId || // ID portion match |
| 42 | botIdWithoutSuffix === pPhoneNumber || // Bot ID phone vs participant phone |
| 43 | botIdWithoutSuffix === pId || // Bot ID phone vs participant ID |
| 44 | (botLid && botLid.split('@')[0].split(':')[0] === pLid) // Bot LID numeric portion match |
| 45 | ); |
| 46 | |
| 47 | return botMatches && (p.admin === 'admin' || p.admin === 'superadmin'); |
| 48 | }); |
| 49 | |
| 50 | // Check if sender is admin |
| 51 | const isSenderAdmin = participants.some(p => { |
| 52 | // Check multiple possible ID formats |
| 53 | const pPhoneNumber = p.phoneNumber ? p.phoneNumber.split('@')[0] : ''; |
| 54 | const pId = p.id ? p.id.split('@')[0] : ''; |
| 55 | const pLid = p.lid ? p.lid.split('@')[0] : ''; |
| 56 | const pFullId = p.id || ''; |
| 57 | const pFullLid = p.lid || ''; |
| 58 | |
| 59 | // Match against sender ID in multiple ways |
| 60 | const senderMatches = ( |
no outgoing calls
no test coverage detected