mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 12:39:20 +00:00
Update GroupChat.js
This commit is contained in:
@@ -53,20 +53,18 @@ class GroupChat extends Chat {
|
|||||||
get participants() {
|
get participants() {
|
||||||
return this.groupMetadata.participants;
|
return this.groupMetadata.participants;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a list of participants by ID to the group
|
* Internal function to change a number of participant's state..
|
||||||
* @param {Array<string>} participantIds
|
* @param {string} type (promote|demote|add|remove)
|
||||||
* @param {?number} sleep optional, amount to sleep in milliseconds before adding the next participant
|
|
||||||
* @returns {Promise<Object>}
|
|
||||||
*/
|
*/
|
||||||
async addParticipants(participantIds, sleep = null) {
|
async _changeParticipants(participantIds, type, sleep = null) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
|
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.['send'+type.charAt(0).toUpperCase() + type.slice(1)+'Participants'](chatWid, [participantWid]));
|
||||||
if (sleep) {
|
if (sleep) {
|
||||||
await Util.sleep(sleep);
|
await Util.sleep(sleep);
|
||||||
}
|
}
|
||||||
@@ -75,67 +73,44 @@ class GroupChat extends Chat {
|
|||||||
}, this.id._serialized, participantIds, sleep);
|
}, this.id._serialized, participantIds, sleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a list of participants by ID to the group
|
||||||
|
* @param {Array<string>} participantIds
|
||||||
|
* @param {?number} sleep optional, amount to sleep in milliseconds before adding the next participant
|
||||||
|
* @returns {Promise<Array<Object>>}
|
||||||
|
*/
|
||||||
|
async addParticipants(participantIds, sleep = null) {
|
||||||
|
return this._changeParticipants(participantIds, 'add', 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
|
* @param {?number} sleep optional, amount to sleep in milliseconds before removing the next participant
|
||||||
* @returns {Promise<Object>}
|
* @returns {Promise<Array<Object>>}
|
||||||
*/
|
*/
|
||||||
async removeParticipants(participantIds, sleep = null) {
|
async removeParticipants(participantIds, sleep = null) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
|
return return this._changeParticipants(participantIds, 'remove', 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, sleep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes participants by IDs to admins
|
* Promote participants to admins by IDs
|
||||||
* @param {Array<string>} participantIds
|
* @param {Array<string>} participantIds
|
||||||
* @param {?number} sleep optional, amount to sleep in milliseconds before promoting the next participant
|
* @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<Array<Object>>}
|
||||||
*/
|
*/
|
||||||
async promoteParticipants(participantIds, sleep = null) {
|
async promoteParticipants(participantIds, sleep = null) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
|
return return this._changeParticipants(participantIds, 'promote', 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, sleep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Demotes participants by IDs to regular users
|
* Demotes admins to regular participants by IDs
|
||||||
* @param {Array<string>} participantIds
|
* @param {Array<string>} participantIds
|
||||||
* @param {?number} sleep optional, amount to sleep in milliseconds before demoting the next participant
|
* @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<Array<Object>>}
|
||||||
*/
|
*/
|
||||||
async demoteParticipants(participantIds, sleep = null) {
|
async demoteParticipants(participantIds, sleep = null) {
|
||||||
return await this.client.pupPage.evaluate(async (chatId, participantIds, sleep) => {
|
return return this._changeParticipants(participantIds, 'demote', 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, sleep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user