choco-bot
Version:
Whatsapp-bot
49 lines (43 loc) • 1.63 kB
JavaScript
const lists = require('../lists.json');
async function invitetogroup(remainingMessage, msg) {
const messageParts = remainingMessage.split(' ');
const GroupChat = await msg.getChat();
if (messageParts.length !== 3) {
msg.reply("Usage: `invite <number> from <listname>`");
return;
}
const numberOfParticipants = parseInt(messageParts[0]);
const listName = messageParts[2];
if (!lists[listName]) {
msg.reply(`List "${listName}" does not exist.`);
return;
}
const participantsInList = lists[listName];
if (!Array.isArray(participantsInList)) {
msg.reply(`Invalid data for list "${listName}".`);
return;
}
if (participantsInList.length === 0) {
msg.reply(`List "${listName}" is empty. There are no participants to invite.`);
return;
}
if (participantsInList.length < numberOfParticipants) {
msg.reply(`Not enough participants in list "${listName}" to invite ${numberOfParticipants} members.`);
return;
}
const participantsToInvite = participantsInList.slice(0, numberOfParticipants);
const group = GroupChat;
try {
group.addParticipants(participantsToInvite);
console.log(`Added ${participantsToInvite.length} participants to the group.`);
} catch (error) {
console.error(`Error adding participants: ${error}`);
msg.reply(`Error adding participants: ${error}`);
return;
}
participantsInList.splice(0, numberOfParticipants);
msg.reply(`Successfully invited ${participantsToInvite.length} members from list "${listName}" to the group.`);
}
module.exports = {
invitetogroup,
};