(sock, chatId, message, senderId)
| 2 | const store = require('../lib/lightweight_store'); |
| 3 | |
| 4 | async function deleteCommand(sock, chatId, message, senderId) { |
| 5 | try { |
| 6 | const { isSenderAdmin, isBotAdmin } = await isAdmin(sock, chatId, senderId); |
| 7 | |
| 8 | if (!isBotAdmin) { |
| 9 | await sock.sendMessage(chatId, { text: 'I need to be an admin to delete messages.' }, { quoted: message }); |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | if (!isSenderAdmin) { |
| 14 | await sock.sendMessage(chatId, { text: 'Only admins can use the .delete command.' }, { quoted: message }); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | // Determine target user and count |
| 19 | const text = message.message?.conversation || message.message?.extendedTextMessage?.text || ''; |
| 20 | const parts = text.trim().split(/\s+/); |
| 21 | let countArg = null; |
| 22 | |
| 23 | // Check if a number is provided |
| 24 | if (parts.length > 1) { |
| 25 | const maybeNum = parseInt(parts[1], 10); |
| 26 | if (!isNaN(maybeNum) && maybeNum > 0) { |
| 27 | countArg = Math.min(maybeNum, 50); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Check if user is replying to a message |
| 32 | const ctxInfo = message.message?.extendedTextMessage?.contextInfo || {}; |
| 33 | const repliedParticipant = ctxInfo.participant || null; |
| 34 | const mentioned = Array.isArray(ctxInfo.mentionedJid) && ctxInfo.mentionedJid.length > 0 ? ctxInfo.mentionedJid[0] : null; |
| 35 | |
| 36 | // If no number provided but replying to a message, default to 1 |
| 37 | if (countArg === null && repliedParticipant) { |
| 38 | countArg = 1; |
| 39 | } |
| 40 | // If no number provided and not replying/mentioning, show usage message |
| 41 | else if (countArg === null && !repliedParticipant && !mentioned) { |
| 42 | await sock.sendMessage(chatId, { |
| 43 | text: '❌ Please specify the number of messages to delete.\n\nUsage:\n• `.del 5` - Delete last 5 messages from group\n• `.del 3 @user` - Delete last 3 messages from @user\n• `.del 2` (reply to message) - Delete last 2 messages from replied user' |
| 44 | }, { quoted: message }); |
| 45 | return; |
| 46 | } |
| 47 | // If no number provided but mentioning a user, default to 1 |
| 48 | else if (countArg === null && mentioned) { |
| 49 | countArg = 1; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // Determine target user: replied > mentioned; if neither, delete last N messages from group |
| 54 | let targetUser = null; |
| 55 | let repliedMsgId = null; |
| 56 | let deleteGroupMessages = false; |
| 57 | |
| 58 | if (repliedParticipant && ctxInfo.stanzaId) { |
| 59 | targetUser = repliedParticipant; |
| 60 | repliedMsgId = ctxInfo.stanzaId; |
| 61 | } else if (mentioned) { |
no test coverage detected