add sleep

This commit is contained in:
Rajeh Taher
2022-07-07 18:49:19 +03:00
committed by GitHub
parent 6ae8c42816
commit 02ad3e0fd0
3 changed files with 38 additions and 17 deletions

2
index.d.ts vendored
View File

@@ -1062,7 +1062,7 @@ declare namespace WAWebJS {
/** Adds or removes a list of participants by ID to the group */ /** Adds or removes a list of participants by ID to the group */
export type ChangeGroupParticipants = export type ChangeGroupParticipants =
(participantIds: Array<string>) => Promise<{ (participantIds: Array<string>, sleep?: number) => Promise<{
status: number; status: number;
participants: Array<{ participants: Array<{
[key: string]: { [key: string]: {

View File

@@ -1,6 +1,7 @@
'use strict'; 'use strict';
const Chat = require('./Chat'); const Chat = require('./Chat');
const Util = require('../util/Util')
/** /**
* Group participant information * Group participant information
@@ -55,70 +56,86 @@ class GroupChat extends Chat {
/** /**
* Adds a list of participants by ID to the group * Adds a list of participants by ID to the group
* @param {Array<string>} participantIds * @param {Array<string>} participantIds
* @param {?number} sleep optional, amount to sleep in milliseconds before adding the next participant
* @returns {Promise<Object>} * @returns {Promise<Object>}
*/ */
async addParticipants(participantIds) { async addParticipants(participantIds, sleep = null) {
return await this.client.pupPage.evaluate(async (chatId, participantIds) => { return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
const chatWid = window.Store.WidFactory.createWid(chatId); const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
const status = []; const status = [];
for (const participantWid of participantWids) { for (const participantWid of participantWids) {
status.push(await window.Store.GroupParticipants.sendAddParticipants(chatWid, participantWid)); status.push(await window.Store.GroupParticipants.sendAddParticipants(chatWid, participantWid));
if (sleep) {
await Util.sleep(sleep)
}
} }
return status; return status;
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds, sleep);
} }
/** /**
* Removes a list of participants by ID to the group * Removes a list of participants by ID to the group
* @param {Array<string>} participantIds * @param {Array<string>} participantIds
* @param {?number} sleep optional, amount to sleep in milliseconds before removing the next participant
* @returns {Promise<Object>} * @returns {Promise<Object>}
*/ */
async removeParticipants(participantIds) { async removeParticipants(participantIds, sleep = null) {
return await this.client.pupPage.evaluate(async (chatId, participantIds) => { return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
const chatWid = window.Store.WidFactory.createWid(chatId); const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
const status = []; const status = [];
for (const participantWid of participantWids) { for (const participantWid of participantWids) {
status.push(await window.Store.GroupParticipants.sendRemoveParticipants(chatWid, participantWid)); status.push(await window.Store.GroupParticipants.sendRemoveParticipants(chatWid, participantWid));
if (sleep) {
await Util.sleep(sleep)
}
} }
return status; return status;
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds, sleep);
} }
/** /**
* Promotes participants by IDs to admins * Promotes participants by IDs to admins
* @param {Array<string>} participantIds * @param {Array<string>} 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 * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
*/ */
async promoteParticipants(participantIds) { async promoteParticipants(participantIds, sleep = null) {
return await this.client.pupPage.evaluate(async (chatId, participantIds) => { return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
const chatWid = window.Store.WidFactory.createWid(chatId); const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
const status = []; const status = [];
for (const participantWid of participantWids) { for (const participantWid of participantWids) {
status.push(await window.Store.GroupParticipants.sendPromoteParticipants(chatWid, participantWid)); status.push(await window.Store.GroupParticipants.sendPromoteParticipants(chatWid, participantWid));
if (sleep) {
await Util.sleep(sleep)
}
} }
return status; return status;
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds, sleep);
} }
/** /**
* Demotes participants by IDs to regular users * Demotes participants by IDs to regular users
* @param {Array<string>} participantIds * @param {Array<string>} 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 * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
*/ */
async demoteParticipants(participantIds) { async demoteParticipants(participantIds, sleep = null) {
return await this.client.pupPage.evaluate(async (chatId, participantIds) => { return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
const chatWid = window.Store.WidFactory.createWid(chatId); const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
const status = []; const status = [];
for (const participantWid of participantWids) { for (const participantWid of participantWids) {
status.push(await window.Store.GroupParticipants.sendDemoteParticipants(chatWid, participantWid)); status.push(await window.Store.GroupParticipants.sendDemoteParticipants(chatWid, participantWid));
if (sleep) {
await Util.sleep(sleep)
}
} }
return status; return status;
}, this.id._serialized, participantIds); }, this.id._serialized, participantIds, sleep);
} }
/** /**

View File

@@ -181,6 +181,10 @@ class Util {
static setFfmpegPath(path) { static setFfmpegPath(path) {
ffmpeg.setFfmpegPath(path); ffmpeg.setFfmpegPath(path);
} }
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
} }
module.exports = Util; module.exports = Util;