(sock: any, message: any, args: any, context: BotContext)
| 11 | adminOnly: true, |
| 12 | |
| 13 | async handler(sock: any, message: any, args: any, context: BotContext) { |
| 14 | const chatId = context.chatId || message.key.remoteJid; |
| 15 | const _senderId = context.senderId || message.key.participant || message.key.remoteJid; |
| 16 | const isBotAdmin = context.isBotAdmin; |
| 17 | |
| 18 | if (!isBotAdmin) { |
| 19 | await sock.sendMessage(chatId, { |
| 20 | text: '❌ *I need to be an admin to delete messages*' |
| 21 | }, { quoted: message }); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | const text = message.message?.conversation || message.message?.extendedTextMessage?.text || ''; |
| 26 | const parts = text.trim().split(/\s+/); |
| 27 | let countArg = null; |
| 28 | |
| 29 | if (parts.length > 1) { |
| 30 | const maybeNum = parseInt(parts[1], 10); |
| 31 | if (!isNaN(maybeNum) && maybeNum > 0) { |
| 32 | countArg = Math.min(maybeNum, 50); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const ctxInfo = message.message?.extendedTextMessage?.contextInfo || {}; |
| 37 | const repliedParticipant = ctxInfo.participant || null; |
| 38 | const mentioned = Array.isArray(ctxInfo.mentionedJid) && ctxInfo.mentionedJid.length > 0 ? ctxInfo.mentionedJid[0] : null; |
| 39 | |
| 40 | if (countArg === null && repliedParticipant) { |
| 41 | countArg = 1; |
| 42 | } |
| 43 | else if (countArg === null && !repliedParticipant && !mentioned) { |
| 44 | await sock.sendMessage(chatId, { |
| 45 | text: '❌ *Please specify the number of messages to delete*\n\n' + |
| 46 | '*Usage:*\n' + |
| 47 | '• `.del 5` - Delete last 5 messages from group\n' + |
| 48 | '• `.del 3 @user` - Delete last 3 messages from @user\n' + |
| 49 | '• `.del 2` (reply to message) - Delete last 2 messages from replied user' |
| 50 | }, { quoted: message }); |
| 51 | return; |
| 52 | } |
| 53 | else if (countArg === null && mentioned) { |
| 54 | countArg = 1; |
| 55 | } |
| 56 | let targetUser = null; |
| 57 | let repliedMsgId = null; |
| 58 | let deleteGroupMessages = false; |
| 59 | |
| 60 | if (repliedParticipant && ctxInfo.stanzaId) { |
| 61 | targetUser = repliedParticipant; |
| 62 | repliedMsgId = ctxInfo.stanzaId; |
| 63 | } else if (mentioned) { |
| 64 | targetUser = mentioned; |
| 65 | } else { |
| 66 | deleteGroupMessages = true; |
| 67 | } |
| 68 | const chatMessages = Array.isArray((store.messages as any)[chatId]) ? (store.messages as any)[chatId] : []; |
| 69 | const toDelete = []; |
| 70 | const seenIds = new Set(); |
nothing calls this directly
no outgoing calls
no test coverage detected