choco-bot
Version:
Whatsapp-bot
33 lines (28 loc) • 1.26 kB
JavaScript
const fs = require('fs');
async function addToListById(chatId, listName, msg) {
const lists = require('../lists.json');
if (!lists[listName]) {
msg.reply(`List "${listName}" does not exist.`);
return;
}
try {
const chat = await msg.client.getChatById(chatId);
const participants = chat.participants;
const participantIds = participants.map((participant) => participant.id._serialized);
const uniqueParticipantIds = [...new Set(participantIds)];
const newParticipants = uniqueParticipantIds.filter((participantId) => !lists[listName].includes(participantId));
if (newParticipants.length > 0) {
lists[listName] = [...lists[listName], ...newParticipants]; // Add new participants
fs.writeFileSync('lists.json', JSON.stringify(lists, null, 2)); // Update the JSON file
msg.reply(`${newParticipants.length} participants added to list "${listName}". Total: ${lists[listName].length}`);
} else {
msg.reply(`No new participants to add to list "${listName}".`);
}
} catch (error) {
console.error('Error fetching participants:', error);
msg.reply('Error fetching participants. Please check the chat ID.');
}
}
module.exports = {
addToListById,
};