diff --git a/README.md b/README.md index 8bb1ee6..a6e4a54 100644 --- a/README.md +++ b/README.md @@ -480,7 +480,7 @@ Baileys is written, keeping in mind, that you may require other custom functiona First, enable the logging of unhandled messages from WhatsApp by setting ``` ts -conn.logger.level = 'unhandled' +conn.logger.level = 'debug' ``` This will enable you to see all sorts of messages WhatsApp sends in the console. Some examples: diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index 7a013b0..21928b1 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -48,7 +48,7 @@ export class WAConnection extends EventEmitter { waitForChats: true, maxRetries: 10, connectCooldownMs: 4000, - phoneResponseTime: 10_000, + phoneResponseTime: 15_000, alwaysUseTakeover: true } /** When to auto-reconnect */ diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 3699250..1805e7f 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -93,8 +93,8 @@ export class WAConnection extends Base { let waitForChats: Promise<{[k: string]: Partial}> // add wait for chats promise if required if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) { - const {waitForChats: wPromise, cancelChats} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage) - waitForChats = wPromise + const {wait, cancelChats} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage) + waitForChats = wait rejections.push (cancelChats) } try { @@ -143,17 +143,17 @@ export class WAConnection extends Base { const chats = new KeyedDB(this.chatOrderingKey, c => c.jid) const contacts = {} + let receivedChats = false let receivedContacts = false let receivedMessages = false let resolveTask: () => void let rejectTask: (e: Error) => void - const checkForResolution = () => receivedContacts && receivedMessages && resolveTask () + const checkForResolution = () => receivedContacts && receivedChats && receivedMessages && resolveTask () // wait for messages to load const messagesUpdate = json => { this.startDebouncedTimeout () // restart debounced timeout - receivedMessages = true - + const isLast = json[1].last || waitOnlyForLast const messages = json[2] as WANode[] @@ -171,6 +171,7 @@ export class WAConnection extends Base { } }) } + if (isLast) receivedMessages = true // if received contacts before messages if (isLast && receivedContacts) checkForResolution () } @@ -195,10 +196,10 @@ export class WAConnection extends Base { }) this.logger.info (`received ${json[2].length} chats`) - if (json[2].length === 0) { - receivedMessages = true - checkForResolution () - } + receivedChats = true + + if (json[2].length === 0) receivedMessages = true + checkForResolution () } const contactsUpdate = json => { if (json[1].duplicate || !json[2]) return @@ -233,7 +234,7 @@ export class WAConnection extends Base { this.off(DEF_CALLBACK_PREFIX + 'response,type:contacts', contactsUpdate) } // wait for the chats & contacts to load - const waitForChats = async () => { + const wait = (async () => { try { registerCallbacks () @@ -266,8 +267,8 @@ export class WAConnection extends Base { } finally { deregisterCallbacks () } - } - return { waitForChats: waitForChats (), cancelChats: () => rejectTask (CancelledError()) } + })() + return { wait, cancelChats: () => rejectTask (CancelledError()) } } private onMessageRecieved(message: string | Buffer) { if (message[0] === '!') {