void / bc.js
github-actions[bot]
Automated sync from GitHub
ca52557
Raw History Blame Contribute Delete
6.23 kB
import moment from 'moment-timezone'
import { downloadMediaMessage } from '@whiskeysockets/baileys'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import CONFIG from './config.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
export const seenStatusIds = new Set()
export function logStatus(participant, messageId, pushName, statusType = 'unknown', caption = '') {
const timestamp = moment().tz(CONFIG.TZ).format('DD/MM/YYYY HH:mm:ss')
const userPhone = getStatusUserPhone(participant)
const formattedPhone = formatPhoneNumber(userPhone)
console.log(`\n[πŸ“… ${timestamp}] [πŸ“Š STATUS UPDATE]`)
console.log(`β”œβ”€ πŸ‘€ From: ${pushName || 'Unknown'} (${formattedPhone})`)
console.log(`β”œβ”€ πŸ”‘ JID: ${participant}`)
console.log(`β”œβ”€ πŸ†” Message ID: ${messageId}`)
console.log(`β”œβ”€ πŸ“Ž Type: ${statusType}`)
if (caption) console.log(`β”œβ”€ πŸ“ Content: ${caption.substring(0, 100)}`)
console.log(`β”œβ”€ πŸ‘οΈ Auto View: ${CONFIG.AUTO_VIEW === true ? 'βœ… ON' : '❌ OFF'}`)
console.log(`β”œβ”€ ❀️ Auto Like: ${CONFIG.AUTO_LIKE === true ? 'βœ… ON' : '❌ OFF'}`)
console.log(`└─ πŸ“– Auto Read: ${CONFIG.AUTO_READ === true ? 'βœ… ON' : '❌ OFF'}\n`)
}
export function getStatusUserPhone(statusJid) {
if (!statusJid) return 'Unknown'
return statusJid.split('@')[0]
}
function formatPhoneNumber(phone) {
if (!phone || phone === 'Unknown') return 'Unknown'
const cleaned = phone.replace(/[^\d+]/g, '')
return cleaned.startsWith('+') ? cleaned : `+${cleaned}`
}
export async function processStatusMessage(msg, sock) {
if (!msg.message || msg.key.remoteJid !== 'status@broadcast') return
if (msg.key.fromMe === true) return
const id = msg.key.id
if (seenStatusIds.has(id)) return
const isLid = msg.key.addressingMode === 'lid'
const resolvedKey = isLid
? { ...msg.key, participant: msg.key.remoteJidAlt || msg.key.participant }
: msg.key
let statusPosterJid = msg.key.participant || msg.key.remoteJidAlt || 'Unknown'
let statusType = 'unknown'
let statusCaption = ''
try {
if (msg.message?.imageMessage) {
statusType = 'image'
statusCaption = msg.message.imageMessage.caption || ''
} else if (msg.message?.videoMessage) {
statusType = 'video'
statusCaption = msg.message.videoMessage.caption || ''
} else if (msg.message?.conversation) {
statusType = 'text'
statusCaption = msg.message.conversation
} else if (msg.message?.extendedTextMessage) {
statusType = 'text'
statusCaption = msg.message.extendedTextMessage.text || ''
} else if (msg.message?.audioMessage) {
statusType = 'audio'
} else if (msg.message?.documentMessage) {
statusType = 'document'
}
if (CONFIG.AUTO_VIEW === true) {
await sock.readMessages([resolvedKey])
console.log(`βœ… Viewed status: ${statusPosterJid.split('@')[0]} [${id}]`)
}
if (CONFIG.AUTO_LIKE === true) {
setTimeout(async () => {
try {
await sock.sendMessage('status@broadcast', {
react: { text: '🀍', key: msg.key }
})
console.log(`🀍 Liked status from: ${statusPosterJid}`)
} catch (reactErr) {
console.error(`❌ Failed to like status: ${reactErr.message}`)
}
}, 2000)
}
seenStatusIds.add(id)
if (seenStatusIds.size > 1000) {
const firstKey = seenStatusIds.values().next().value
seenStatusIds.delete(firstKey)
}
let senderName = msg.pushName || 'Unknown'
if (statusPosterJid !== 'Unknown') {
try {
const contact = await sock.onWhatsApp(statusPosterJid)
if (contact && contact[0]) {
senderName = contact[0].notify || senderName
}
} catch {}
}
logStatus(statusPosterJid, id, senderName, statusType, statusCaption)
} catch (err) {
console.error('[AUTO-VIEW] Error viewing status:', err)
}
}
export async function handleStatusReply(msg, sock, senderJid) {
const tempDir = '/tmp/flash-md-temp'
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true })
}
try {
const contextInfo = msg.message?.extendedTextMessage?.contextInfo
const quotedMsg = contextInfo?.quotedMessage
const commandText = msg.message?.conversation || msg.message?.extendedTextMessage?.text || ''
const normalizedCommand = commandText.toLowerCase().trim()
const statusCommands = ['share', 'send', 'tuma', 'nitumie']
const isStatusReply = contextInfo?.participant && quotedMsg
if (statusCommands.includes(normalizedCommand) && isStatusReply) {
const recipientJid = senderJid
let sendMsg
const quotedMsgWrapper = { message: quotedMsg }
if (quotedMsg.imageMessage) {
const buffer = await downloadMediaMessage(quotedMsgWrapper, 'buffer', {}, { logger: console })
const filePath = path.join(tempDir, `${Date.now()}-status-image.jpg`)
fs.writeFileSync(filePath, buffer)
sendMsg = { image: { url: filePath }, caption: 'πŸ“Έ Sent by *Flash-Md-V3* !' }
} else if (quotedMsg.videoMessage) {
const buffer = await downloadMediaMessage(quotedMsgWrapper, 'buffer', {}, { logger: console })
const filePath = path.join(tempDir, `${Date.now()}-status-video.mp4`)
fs.writeFileSync(filePath, buffer)
sendMsg = { video: { url: filePath }, caption: 'πŸŽ₯ Sent by *Flash-Md-V3* !' }
} else if (quotedMsg.stickerMessage) {
const buffer = await downloadMediaMessage(quotedMsgWrapper, 'buffer', {}, { logger: console })
const filePath = path.join(tempDir, `${Date.now()}-status-sticker.webp`)
fs.writeFileSync(filePath, buffer)
sendMsg = { sticker: { url: filePath } }
} else {
return false
}
await sock.sendMessage(recipientJid, sendMsg, { quoted: msg })
const fileUrl = sendMsg.image?.url || sendMsg.video?.url || sendMsg.sticker?.url
if (fileUrl) {
try { await fs.promises.unlink(fileUrl) } catch {}
}
return true
}
} catch (err) {
console.error(`Error in status reply: ${err.message}`)
}
return false
}