choco-bot
Version:
Whatsapp-bot
67 lines (63 loc) • 2.29 kB
JavaScript
async function broadcast(remainingMessage, msg){
const chat = await msg.getChat();
mess = remainingMessage.trim();
mess2 = mess.split(' ')
const type = mess2[0]
if (type.toLowerCase() == "list"){
const listName = mess2[1]
const messageParts = remainingMessage.split('/')
const bbb = messageParts[1]
if (messageParts.length < 2) {
msg.reply("Usage: `broadcast (type) (listname if using list type) /message`");
} else {
if (lists[listName]) {
const participantsInList = lists[listName];
participantsInList.forEach(async (participantId) => {
if (participantId !== client.info.me.user) {
try {
let media = null;
// Check if the message contains media (image or file)
if (msg.hasMedia) {
// Get the media from the message
media = await msg.downloadMedia();
}
await client.sendMessage(participantId, media, {caption: bbb})
} catch (error) {
console.error(`Error sending message to ${participantId}: ${error}`);
}
}
});
msg.reply(`Broadcast sent to list "${listName}" participants.`);
} else {
msg.reply(`List "${listName}" does not exist.`);
}
}
}else if (type.toLowerCase() == "group"){
if (chat.isGroup){
const participants = chat.participants;
participants.forEach(async (participant) => {
const participantId = participant.id._serialized;
if (participantId !== client.info.me.user) {
try {
let media = null;
if (msg.hasMedia) {
media = await msg.downloadMedia();
}
await client.sendMessage(participantId, {
text: message.slice(4).trim(),
media: media,
});
} catch (error) {
console.error(`Error sending message to ${participantId}: ${error}`);
}
}
});
msg.reply(`Broadcast sent to ${participants.length} participants.`);
}else{
msg.reply('Use in groups alone')
}
}
}
module.exports = {
broadcast,
}