From b99d28c5c6c9a2def73c1baf6671bfa0fa1e5f02 Mon Sep 17 00:00:00 2001 From: Gulg <65360617+GulgDev@users.noreply.github.com> Date: Sun, 28 Apr 2024 18:22:07 +0500 Subject: [PATCH] Add members settings (#2788) * Update GroupChat.js * Update index.d.ts * style: clarify method doc * refactor: add additional check * fix: provide valid error name --------- Co-authored-by: alechkos <93551621+alechkos@users.noreply.github.com> --- index.d.ts | 6 ++++++ src/structures/GroupChat.js | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/index.d.ts b/index.d.ts index 329a419..6c1f703 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1523,6 +1523,12 @@ declare namespace WAWebJS { setSubject: (subject: string) => Promise; /** Updates the group description */ setDescription: (description: string) => Promise; + /** + * Updates the group setting to allow only admins to add members to the group. + * @param {boolean} [adminsOnly=true] Enable or disable this option + * @returns {Promise} Returns true if the setting was properly updated. This can return false if the user does not have the necessary permissions. + */ + setAddMembersAdminsOnly: (adminsOnly?: boolean) => Promise; /** Updates the group settings to only allow admins to send messages * @param {boolean} [adminsOnly=true] Enable or disable this option * @returns {Promise} Returns true if the setting was properly updated. This can return false if the user does not have the necessary permissions. diff --git a/src/structures/GroupChat.js b/src/structures/GroupChat.js index d974f9d..4ae5b19 100644 --- a/src/structures/GroupChat.js +++ b/src/structures/GroupChat.js @@ -281,6 +281,27 @@ class GroupChat extends Chat { return true; } + /** + * Updates the group setting to allow only admins to add members to the group. + * @param {boolean} [adminsOnly=true] Enable or disable this option + * @returns {Promise} Returns true if the setting was properly updated. This can return false if the user does not have the necessary permissions. + */ + async setAddMembersAdminsOnly(adminsOnly=true) { + const success = await this.client.pupPage.evaluate(async (groupId, adminsOnly) => { + const chatWid = window.Store.WidFactory.createWid(groupId); + try { + const response = await window.Store.GroupUtils.setGroupMemberAddMode(chatWid, 'member_add_mode', adminsOnly ? 0 : 1); + return response.name === 'SetMemberAddModeResponseSuccess'; + } catch (err) { + if(err.name === 'SmaxParsingFailure') return false; + throw err; + } + }, this.id._serialized, adminsOnly); + + success && (this.groupMetadata.memberAddMode = adminsOnly ? 'admin_add' : 'all_member_add'); + return success; + } + /** * Updates the group settings to only allow admins to send messages. * @param {boolean} [adminsOnly=true] Enable or disable this option