pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.05k stars 3.58k forks source link

client.emit("message",message) is not working #2637

Closed shifoc closed 9 months ago

shifoc commented 9 months ago

Is there an existing issue for this?

Describe the bug

client.emit("message",message) is not sending the message to client.on('message', message => { console.log(message.body); });

Expected behavior

Send the message to client.on("message"

Steps to Reproduce the Bug or Issue


console.log("Getting unread messages...");

    for (const chat of await client.getChats()) {

        const unreadMessages = await chat.fetchMessages({
            limit: chat.unreadCount,
        })

        for (const message of unreadMessages) {
            client.emit('message', message);
        }
    }
    console.log("Done getting unread messages !");

client.on('message', message => {
    console.log(message.body);
});

Relevant Code

No response

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Ubuntu Android v1.23.0

Additional context

No response

Agusteando commented 9 months ago

Do you receive any other messages?

shifoc commented 9 months ago

Yes everything else works fine

shifoc commented 9 months ago

I fixed it by positioning client.on at the top