diff --git a/example.js b/example.js index 9d1fa1f..c495e80 100644 --- a/example.js +++ b/example.js @@ -169,6 +169,12 @@ client.on('message', async msg => { } else if (msg.body === '!archive') { const chat = await msg.getChat(); chat.archive(); + } else if (msg.body === '!mute') { + const chat = await msg.getChat(); + // mute the chat for 20 seconds + const unmuteDate = new Date(); + unmuteDate.setSeconds(unmuteDate.getSeconds() + 20); + await chat.mute(unmuteDate); } else if (msg.body === '!typing') { const chat = await msg.getChat(); // simulates typing in the chat diff --git a/src/Client.js b/src/Client.js index c242092..1e77f55 100644 --- a/src/Client.js +++ b/src/Client.js @@ -535,6 +535,32 @@ class Client extends EventEmitter { }, chatId); } + /** + * Mutes the Chat until a specified date and returns the new mute state + * @param {string} chatId ID of the chat that will be muted + * @param {Date} unmuteDate Date when the chat will be unmuted + * @returns {Promise} new mute state + */ + async muteChat(chatId, unmuteDate) { + return await this.pupPage.evaluate(async (chatId, timestamp) => { + let chat = await window.Store.Chat.get(chatId); + await chat.mute.mute(timestamp, !0); + }, chatId, unmuteDate.getTime() / 1000); + } + + /** + * Unmutes the Chat and returns the new mute state + * @param {string} chatId ID of the chat that will be unmuted + * @returns {Promise} new mute state + */ + async unmuteChat(chatId) { + return await this.pupPage.evaluate(async chatId => { + let chat = await window.Store.Chat.get(chatId); + await window.Store.Cmd.muteChat(chat, false); + return chat.mute.isMuted; + }, chatId); + } + /** * Returns the contact ID's profile picture URL, if privacy settings allow it * @param {string} contactId the whatsapp user's ID diff --git a/src/structures/Chat.js b/src/structures/Chat.js index f6ba2ab..c01bf52 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -112,6 +112,23 @@ class Chat extends Base { return this.client.unarchiveChat(this.id._serialized); } + /** + * Mutes this chat until a specified date + * @param {Date} unmuteDate Date at which the Chat will be unmuted + * @returns {Promise} new mute state + */ + async mute(unmuteDate) { + return this.client.muteChat(this.id._serialized, unmuteDate); + } + + /** + * Unmutes this chat + * @returns {Promise} new mute state + */ + async unmute() { + return this.client.unmuteChat(this.id._serialized); + } + /** * Loads chat messages, sorted from earliest to latest. * @param {Object} searchOptions Options for searching messages. Right now only limit is supported.