diff --git a/index.d.ts b/index.d.ts index a242bf4..b2316d1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1062,7 +1062,7 @@ declare namespace WAWebJS { /** Adds or removes a list of participants by ID to the group */ export type ChangeGroupParticipants = - (participantIds: Array) => Promise<{ + (participantIds: Array, sleep?: number) => Promise<{ status: number; participants: Array<{ [key: string]: { diff --git a/src/structures/GroupChat.js b/src/structures/GroupChat.js index 542c81e..bdde837 100644 --- a/src/structures/GroupChat.js +++ b/src/structures/GroupChat.js @@ -1,6 +1,7 @@ 'use strict'; const Chat = require('./Chat'); +const Util = require('../util/Util') /** * Group participant information @@ -55,70 +56,86 @@ class GroupChat extends Chat { /** * Adds a list of participants by ID to the group - * @param {Array} participantIds + * @param {Array} participantIds + * @param {?number} sleep optional, amount to sleep in milliseconds before adding the next participant * @returns {Promise} */ - async addParticipants(participantIds) { - return await this.client.pupPage.evaluate(async (chatId, participantIds) => { + async addParticipants(participantIds, sleep = null) { + return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => { const chatWid = window.Store.WidFactory.createWid(chatId); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const status = []; for (const participantWid of participantWids) { status.push(await window.Store.GroupParticipants.sendAddParticipants(chatWid, participantWid)); + if (sleep) { + await Util.sleep(sleep) + } } return status; - }, this.id._serialized, participantIds); + }, this.id._serialized, participantIds, sleep); } /** * Removes a list of participants by ID to the group - * @param {Array} participantIds + * @param {Array} participantIds + * @param {?number} sleep optional, amount to sleep in milliseconds before removing the next participant * @returns {Promise} */ - async removeParticipants(participantIds) { - return await this.client.pupPage.evaluate(async (chatId, participantIds) => { + async removeParticipants(participantIds, sleep = null) { + return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => { const chatWid = window.Store.WidFactory.createWid(chatId); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const status = []; for (const participantWid of participantWids) { status.push(await window.Store.GroupParticipants.sendRemoveParticipants(chatWid, participantWid)); + if (sleep) { + await Util.sleep(sleep) + } } return status; - }, this.id._serialized, participantIds); + }, this.id._serialized, participantIds, sleep); } /** * Promotes participants by IDs to admins - * @param {Array} participantIds + * @param {Array} participantIds + * @param {?number} sleep optional, amount to sleep in milliseconds before promoting the next participant * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful */ - async promoteParticipants(participantIds) { - return await this.client.pupPage.evaluate(async (chatId, participantIds) => { + async promoteParticipants(participantIds, sleep = null) { + return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => { const chatWid = window.Store.WidFactory.createWid(chatId); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const status = []; for (const participantWid of participantWids) { status.push(await window.Store.GroupParticipants.sendPromoteParticipants(chatWid, participantWid)); + if (sleep) { + await Util.sleep(sleep) + } } return status; - }, this.id._serialized, participantIds); + }, this.id._serialized, participantIds, sleep); } /** * Demotes participants by IDs to regular users - * @param {Array} participantIds + * @param {Array} participantIds + * @param {?number} sleep optional, amount to sleep in milliseconds before demoting the next participant * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful */ - async demoteParticipants(participantIds) { - return await this.client.pupPage.evaluate(async (chatId, participantIds) => { + async demoteParticipants(participantIds, sleep = null) { + return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => { const chatWid = window.Store.WidFactory.createWid(chatId); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const status = []; for (const participantWid of participantWids) { status.push(await window.Store.GroupParticipants.sendDemoteParticipants(chatWid, participantWid)); + if (sleep) { + await Util.sleep(sleep) + } } return status; - }, this.id._serialized, participantIds); + }, this.id._serialized, participantIds, sleep); } /** diff --git a/src/util/Util.js b/src/util/Util.js index 39a8a01..c071312 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -181,6 +181,10 @@ class Util { static setFfmpegPath(path) { ffmpeg.setFfmpegPath(path); } + + static sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } } module.exports = Util;