File size: 6,230 Bytes
ca52557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
}