open-wa / wa-automate-nodejs

💬 🤖 The most reliable tool for chatbots with advanced features. Be sure to 🌟 this repository for updates!
https://docs.openwa.dev/
Other
3.11k stars 589 forks source link

onGlobalParticipantsChanged gets called everytime. #2533

Closed Kuroosh closed 2 years ago

Kuroosh commented 2 years ago

Are you using the latest version of the library?

What type of session are you experiencing this issue on?

Legacy (Normal)

What type of host account are you experiencing this issue on?

Business account

Mode

My own code

Current Behavior

When i start the bot.. nothing happens.. but if i write a message in the Group.. it instantly calls onGlobalParticipantsChanged all the time.

Expected Behavior

it should not spam onGlobalParticipantsChanged event as soon someone write something..

it calls it btw with the groupChangeEvent.add... it never calls the remove action .. idk why.

Steps To Reproduce

wa.create({
    sessionId: 'VENOX_BOT',
    multiDevice: true, //required to enable multiDevice support
    authTimeout: 60, //wait only 60 seconds to get a connection with the host account device
    blockCrashLogs: true,
    disableSpins: true,
    headless: true,
    hostNotificationLang: wa.NotificationLanguage.DEDE,
    logConsole: false,
    popup: true,
    qrTimeout: 0, //0 means it will wait forever for you to scan the qr code
}).then(client => {
    SOVIET = client;
    setTimeout(() => onStart(), 250);
});
async function onStart() {
    try {
        console.log(color(figlet.textSync('SOVIET BOT', 'Larry 3D'), chalk.cyan));

        loadDatabaseTables();

        SOVIET.onMessage(message => onMessage(message));

        SOVIET.onGlobalParticipantsChanged(async (event: ParticipantChangedEventModel) => {
            switch (event.action) {
                case groupChangeEvent.add:
                    return await onParticipantJoinGroup(event);
                case groupChangeEvent.remove:
                    return await onParticipantLeaveGroup(event);
            }
        });
    } catch (exception) {
        console.log(exception);
    }
}

create() code

`already on top.`

DEBUG INFO

- Debug info: {
  "WA_VERSION": "2.2206.5",
  "PAGE_UA": "WhatsApp/2.2147.16 Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
  "WA_AUTOMATE_VERSION": "4.31.7",
  "BROWSER_VERSION": "HeadlessChrome/99.0.4844.0",
  "OS": "Windows Server 2019",
  "START_TS": 1645904527613
}

Environment

- OS: Windows Server 2019
- Node: v16.13.2
- npm: npm@8.1.2

Screenshots/Logs

No response

Anything else?

No response

danilocodes commented 2 years ago

Same here, every first group message after a restart emits onGloblalParticipantsChanged for every member in group.

smashah commented 2 years ago

@danilocodes first group message after a restart

Ok so you start a session, a message comes in for that group, and then it participant change events for all members?

This is probably because the group metadata is being lazy loaded into the session.

Try waitForRipeSession: true and see if it improves the situation.

The solution to this would be to map over all groups and get the metadata before registering the listener

danilocodes commented 2 years ago

Try waitForRipeSession: true and see if it improves the situation.

This works perfectly, it took a bit longer to load with this but it's ok as long as it's stable THANKS!

Kuroosh commented 2 years ago

Works fine, thanks <3 ♥