MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / handler

Function handler

plugins/broadcastdm.js:8–67  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

6 usage: '.broadcastdm <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 DM*\n\n*Usage:* .broadcastdm <message>\n\n*Example:*\n.broadcastdm Hey! Check out our new features!\n\n_Sends to all contacts in the bot's contact list. Has a 1.5s delay between each to avoid ban._`,
15 ...channelInfo
16 }, { quoted: message });
17 }
18 let contacts = [];
19 try {
20 const allContacts = Object.keys(sock.store?.contacts || {});
21 contacts = allContacts.filter(jid => jid.endsWith('@s.whatsapp.net') &&
22 jid !== sock.user?.id);
23 }
24 catch (e) {
25 console.error('[BROADCASTDM] Error getting contacts:', e.message);
26 }
27 if (contacts.length === 0) {
28 return await sock.sendMessage(chatId, {
29 text: '❌ No contacts found in the bot\'s contact list.',
30 ...channelInfo
31 }, { quoted: message });
32 }
33 await sock.sendMessage(chatId, {
34 text: `📩 *Broadcasting to ${contacts.length} contact(s)...*\n\nThis may take a moment.`,
35 ...channelInfo
36 }, { quoted: message });
37 const broadcastText = `📩 *MESSAGE*\n\n${text}`;
38 let sent = 0;
39 let failed = 0;
40 for (const contactJid of contacts) {
41 try {
42 await sock.sendMessage(contactJid, {
43 text: broadcastText,
44 contextInfo: {
45 forwardingScore: 1,
46 isForwarded: true,
47 forwardedNewsletterMessageInfo: {
48 newsletterJid: '120363319098372999@newsletter',
49 newsletterName: 'GlobalTechInc',
50 serverMessageId: -1
51 }
52 }
53 });
54 sent++;
55 }
56 catch (e) {
57 console.error(`[BROADCASTDM] Failed to send to ${contactJid}: ${e.message}`);
58 failed++;
59 }
60 // 1.5 second delay between DMs
61 await new Promise(r => setTimeout(r, 1500));
62 }
63 await sock.sendMessage(chatId, {
64 text: `✅ *DM Broadcast Complete!*\n\n📤 Sent: ${sent}\n❌ Failed: ${failed}\n📊 Total: ${contacts.length}`,
65 ...channelInfo

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected