(sock, message, args, context)
| 6 | usage: '.broadcast <message>', |
| 7 | ownerOnly: true, |
| 8 | async handler(sock, message, args, context) { |
| 9 | const chatId = context.chatId || message.key.remoteJid; |
| 10 | const channelInfo = context.channelInfo || {}; |
| 11 | const text = args.join(' ').trim(); |
| 12 | if (!text) { |
| 13 | return await sock.sendMessage(chatId, { |
| 14 | text: `*📢 BROADCAST*\n\n*Usage:* .broadcast <message>\n\n*Example:*\n.broadcast Hello everyone! Bot will be down for maintenance at 10 PM.\n\n_Sends to all groups the bot is in. Has a 1 second delay between each group to avoid ban._`, |
| 15 | ...channelInfo |
| 16 | }, { quoted: message }); |
| 17 | } |
| 18 | let groups = []; |
| 19 | try { |
| 20 | const allChats = Object.keys(sock.store?.chats || {}); |
| 21 | groups = allChats.filter(jid => jid.endsWith('@g.us')); |
| 22 | } |
| 23 | catch (e) { |
| 24 | console.error('[BROADCAST] Error getting groups:', e.message); |
| 25 | } |
| 26 | if (groups.length === 0) { |
| 27 | return await sock.sendMessage(chatId, { |
| 28 | text: '❌ No groups found. Make sure the bot is in at least one group.', |
| 29 | ...channelInfo |
| 30 | }, { quoted: message }); |
| 31 | } |
| 32 | await sock.sendMessage(chatId, { |
| 33 | text: `📢 *Broadcasting to ${groups.length} group(s)...*\n\nThis may take a moment.`, |
| 34 | ...channelInfo |
| 35 | }, { quoted: message }); |
| 36 | const broadcastText = `📢 *BROADCAST MESSAGE*\n\n${text}`; |
| 37 | let sent = 0; |
| 38 | let failed = 0; |
| 39 | for (const groupJid of groups) { |
| 40 | try { |
| 41 | await sock.sendMessage(groupJid, { |
| 42 | text: broadcastText, |
| 43 | contextInfo: { |
| 44 | forwardingScore: 1, |
| 45 | isForwarded: true, |
| 46 | forwardedNewsletterMessageInfo: { |
| 47 | newsletterJid: '120363319098372999@newsletter', |
| 48 | newsletterName: 'GlobalTechInc', |
| 49 | serverMessageId: -1 |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | sent++; |
| 54 | } |
| 55 | catch (e) { |
| 56 | console.error(`[BROADCAST] Failed to send to ${groupJid}: ${e.message}`); |
| 57 | failed++; |
| 58 | } |
| 59 | // 1 second delay between sends to avoid WhatsApp rate limiting |
| 60 | await new Promise(r => setTimeout(r, 1000)); |
| 61 | } |
| 62 | await sock.sendMessage(chatId, { |
| 63 | text: `✅ *Broadcast Complete!*\n\n📤 Sent: ${sent}\n❌ Failed: ${failed}\n📊 Total: ${groups.length}`, |
| 64 | ...channelInfo |
| 65 | }, { quoted: message }); |
nothing calls this directly
no outgoing calls
no test coverage detected