mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 04:29:15 +00:00
feat: Pin and unpin Chats (#166)
* Added Pinning - chat.pin() to pin - chat.unpin() to unpin Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
@@ -166,9 +166,12 @@ client.on('message', async msg => {
|
|||||||
} else {
|
} else {
|
||||||
msg.reply('I can only delete my own messages');
|
msg.reply('I can only delete my own messages');
|
||||||
}
|
}
|
||||||
|
} else if (msg.body === '!pin') {
|
||||||
|
const chat = await msg.getChat();
|
||||||
|
await chat.pin();
|
||||||
} else if (msg.body === '!archive') {
|
} else if (msg.body === '!archive') {
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
chat.archive();
|
await chat.archive();
|
||||||
} else if (msg.body === '!mute') {
|
} else if (msg.body === '!mute') {
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
// mute the chat for 20 seconds
|
// mute the chat for 20 seconds
|
||||||
|
|||||||
12
index.d.ts
vendored
12
index.d.ts
vendored
@@ -17,7 +17,13 @@ declare namespace WAWebJS {
|
|||||||
getInviteInfo(inviteCode: string): Promise<object>
|
getInviteInfo(inviteCode: string): Promise<object>
|
||||||
|
|
||||||
/** Enables and returns the archive state of the Chat */
|
/** Enables and returns the archive state of the Chat */
|
||||||
archiveChat(): Promise<boolean>
|
archiveChat(chatId: string): Promise<boolean>
|
||||||
|
|
||||||
|
/** Pins the Chat and returns its new Pin state */
|
||||||
|
pinChat(chatId: string): Promise<boolean>
|
||||||
|
|
||||||
|
/** Unpins the Chat and returns its new Pin state */
|
||||||
|
unpinChat(chatId: string): Promise<boolean>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new group
|
* Create a new group
|
||||||
@@ -679,6 +685,10 @@ declare namespace WAWebJS {
|
|||||||
|
|
||||||
/** Archives this chat */
|
/** Archives this chat */
|
||||||
archive: () => Promise<void>,
|
archive: () => Promise<void>,
|
||||||
|
/** Pins this chat and returns its new Pin state */
|
||||||
|
pin: () => Promise<boolean>,
|
||||||
|
/** Unpins this chat and returns its new Pin state */
|
||||||
|
unpin: () => Promise<boolean>,
|
||||||
/** Clears all messages from the chat */
|
/** Clears all messages from the chat */
|
||||||
clearMessages: () => Promise<boolean>,
|
clearMessages: () => Promise<boolean>,
|
||||||
/** Stops typing or recording in chat immediately. */
|
/** Stops typing or recording in chat immediately. */
|
||||||
|
|||||||
@@ -610,6 +610,43 @@ class Client extends EventEmitter {
|
|||||||
}, chatId);
|
}, chatId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pins the Chat
|
||||||
|
* @returns {Promise<boolean>} New pin state. Could be false if the max number of pinned chats was reached.
|
||||||
|
*/
|
||||||
|
async pinChat(chatId) {
|
||||||
|
return this.pupPage.evaluate(async chatId => {
|
||||||
|
let chat = window.Store.Chat.get(chatId);
|
||||||
|
if (chat.pin) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const MAX_PIN_COUNT = 3;
|
||||||
|
if (window.Store.Chat.models.length > MAX_PIN_COUNT) {
|
||||||
|
let maxPinned = window.Store.Chat.models[MAX_PIN_COUNT - 1].pin;
|
||||||
|
if (maxPinned) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await window.Store.Cmd.pinChat(chat, true);
|
||||||
|
return true;
|
||||||
|
}, chatId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unpins the Chat
|
||||||
|
* @returns {Promise<boolean>} New pin state
|
||||||
|
*/
|
||||||
|
async unpinChat(chatId) {
|
||||||
|
return this.pupPage.evaluate(async chatId => {
|
||||||
|
let chat = window.Store.Chat.get(chatId);
|
||||||
|
if (!chat.pin) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
await window.Store.Cmd.pinChat(chat, false);
|
||||||
|
return false;
|
||||||
|
}, chatId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutes the Chat until a specified date
|
* Mutes the Chat until a specified date
|
||||||
* @param {string} chatId ID of the chat that will be muted
|
* @param {string} chatId ID of the chat that will be muted
|
||||||
|
|||||||
@@ -57,6 +57,12 @@ class Chat extends Base {
|
|||||||
*/
|
*/
|
||||||
this.archived = data.archive;
|
this.archived = data.archive;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if the Chat is pinned
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
this.pinned = !!data.pin;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates if the chat is muted or not
|
* Indicates if the chat is muted or not
|
||||||
* @type {number}
|
* @type {number}
|
||||||
@@ -124,6 +130,22 @@ class Chat extends Base {
|
|||||||
return this.client.unarchiveChat(this.id._serialized);
|
return this.client.unarchiveChat(this.id._serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pins this chat
|
||||||
|
* @returns {Promise<boolean>} New pin state. Could be false if the max number of pinned chats was reached.
|
||||||
|
*/
|
||||||
|
async pin() {
|
||||||
|
return this.client.pinChat(this.id._serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unpins this chat
|
||||||
|
* @returns {Promise<boolean>} New pin state
|
||||||
|
*/
|
||||||
|
async unpin() {
|
||||||
|
return this.client.unpinChat(this.id._serialized);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutes this chat until a specified date
|
* Mutes this chat until a specified date
|
||||||
* @param {Date} unmuteDate Date at which the Chat will be unmuted
|
* @param {Date} unmuteDate Date at which the Chat will be unmuted
|
||||||
|
|||||||
Reference in New Issue
Block a user