diff --git a/example.js b/example.js index 8a1173c..e922579 100644 --- a/example.js +++ b/example.js @@ -376,14 +376,35 @@ client.on('message', async msg => { requesterIds: ['number1@c.us', 'number2@c.us'], sleep: null }); + } else { + /** + * Pins a message in a chat, a method takes a number in seconds for the message to be pinned. + * WhatsApp default values for duration to pass to the method are: + * 1. 86400 for 24 hours + * 2. 604800 for 7 days + * 3. 2592000 for 30 days + * You can pass your own value: + */ + const result = await msg.pin(60); // Will pin a message for 1 minute + console.log(result); // True if the operation completed successfully, false otherwise } }); -client.on('message_create', (msg) => { +client.on('message_create', async (msg) => { // Fired on all message creations, including your own if (msg.fromMe) { // do stuff here } + + // Unpins a message + if (msg.fromMe && msg.body.startsWith('!unpin')) { + const pinnedMsg = await msg.getQuotedMessage(); + if (pinnedMsg) { + // Will unpin a message + const result = await pinnedMsg.unpin(); + console.log(result); // True if the operation completed successfully, false otherwise + } + } }); client.on('message_ciphertext', (msg) => { diff --git a/index.d.ts b/index.d.ts index ac1e5ee..864ec63 100644 --- a/index.d.ts +++ b/index.d.ts @@ -935,6 +935,10 @@ declare namespace WAWebJS { star: () => Promise, /** Unstar this message */ unstar: () => Promise, + /** Pins the message (group admins can pin messages of all group members) */ + pin: (duration: number) => Promise, + /** Unpins the message (group admins can unpin messages of all group members) */ + unpin: () => Promise, /** Get information about message delivery status */ getInfo: () => Promise, /** diff --git a/src/structures/Message.js b/src/structures/Message.js index a18fa1b..ba5c7c4 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -522,6 +522,27 @@ class Message extends Base { }, this.id._serialized); } + /** + * Pins the message (group admins can pin messages of all group members) + * @param {number} duration The duration in seconds the message will be pinned in a chat + * @returns {Promise} Returns true if the operation completed successfully, false otherwise + */ + async pin(duration) { + return await this.client.pupPage.evaluate(async (msgId, duration) => { + return await window.WWebJS.pinUnpinMsgAction(msgId, 1, duration); + }, this.id._serialized, duration); + } + + /** + * Unpins the message (group admins can unpin messages of all group members) + * @returns {Promise} Returns true if the operation completed successfully, false otherwise + */ + async unpin() { + return await this.client.pupPage.evaluate(async (msgId) => { + return await window.WWebJS.pinUnpinMsgAction(msgId, 2); + }, this.id._serialized); + } + /** * Message Info * @typedef {Object} MessageInfo diff --git a/src/util/Injected.js b/src/util/Injected.js index 27b48e4..eeb49f5 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -58,6 +58,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.LidUtils = window.mR.findModule('getCurrentLid')[0]; window.Store.WidToJid = window.mR.findModule('widToUserJid')[0]; window.Store.JidToWid = window.mR.findModule('userJidToUserWid')[0]; + window.Store.pinUnpinMsg = window.mR.findModule('sendPinInChatMsg')[0].sendPinInChatMsg; /* eslint-disable no-undef, no-cond-assign */ window.Store.QueryExist = ((m = window.mR.findModule('queryExists')[0]) ? m.queryExists : window.mR.findModule('queryExist')[0].queryWidExists); @@ -1083,4 +1084,12 @@ exports.LoadUtils = () => { return []; } }; + + window.WWebJS.pinUnpinMsgAction = async (msgId, action, duration) => { + const message = window.Store.Msg.get(msgId); + if (!message) return false; + const response = await window.Store.pinUnpinMsg(message, action, duration); + if (response.messageSendResult === 'OK') return true; + return false; + }; };