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.05k stars 589 forks source link

Type mismatch on ParticipantChangedEventModel #2036

Closed phaalonso closed 2 years ago

phaalonso commented 2 years ago

Are you using the latest version of the library?

Current Behavior

In the ParticipantChanged event, the typing of the event don't correspond to the one being send to it.

While it's says that it will be of the type Id, it is receiving strings. Furthermore, making the need to cast the variable to unknown and then the type needed in the function, like this:

this.client.onGlobalParticipantsChanged(async change => {
    if (change.action != 'add') return;

    let newMembers: Id[];

    if (Array.isArray(change.who))
        newMembers = [...change.who];
    else
        newMembers = [change.who];
    }

    console.log(newMembers);
    newMembers.forEach(userId => {
        const id = userId.toString() as ContactId;
        if (!id.startsWith('55')) {
            client.getChatById(change.chat as unknown as `${number}@c.us`).then(chat => {
                if (chat.canSend) {
                    client.sendText(chatId, 'Esse grupo é restrito para usuários pertencentes do Brasil');
                }
            });
            client.removeParticipant(change.chat as unknown as GroupChatId, id)
                .catch(err => logger.exception(err, 'Remove +55 from group'));
        } else {
            verifyBlackList(id);
        }
    });
});

Without casting, this error will happen:

Argument of type 'Id' is not assignable to parameter of type '`${number}@c.us`'.

Type used in typescript:

export interface ParticipantChangedEventModel {
  by: Id,
  action: groupChangeEvent,
  who: [Id]
  chat: Id
}

export interface Id {
  server: string;
  user: string;
  _serialized: string;
}

Expected Behavior

The types of ParticipantEvent be equals to the ones send to the user, and is expected that they are compatible with the clients functions

Steps To Reproduce

  1. In a normal bot, use client.onParticipantChanged or client.onGlobalParticipantsChanged
  2. Log the variable used in the callback
  3. Check if type send to the callback is the same as the type that Typescript knows
  4. Check if it can be used in others functions

Mode

My own code

create() code

create({
    headless: false,
    killProcessOnBrowserClose: true,
    killClientOnLogout: true,
})

DEBUG INFO

WA_VERSION: 2.2132.6                                                                         
PAGE_UA: 'WhatsApp/2.2108.8 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' │                                           
BROWSER_VERSION 'Chrome/93.0.4577.0'

Environment

- OS: Pop!_OS 21.04 x86_64 
- Node: v15.14.0
- npm: 7.7.6

Screenshots

No response

Anything else?

No response

smashah commented 2 years ago

@github-actions run

⚡ Release! ⚡ ```js (async () => { function exec(cmd) { console.log(execSync(cmd).toString()); } // Config const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; const gitUserName = "github-actions[bot]"; exec(`echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc`); exec(`git config --global user.email "${gitUserEmail}"`); exec(`git config --global user.name "${gitUserName}"`); exec(`npm i -D`); exec(`npm run release-ci`); //comment on the issue var result = execSync(`npx auto-changelog -o ./tempchangelog.txt --commit-limit false --template ./compact-keepachangelog.hbs --stdout`).toString(); await postComment(result); //create changelog image exec(`npm run release-image`); exec(`git commit -a -m 'updated release-image'`); exec(`git push --force`); })(); ```
smashah commented 2 years ago

Changelog

🚀 Release 4.17.2 (2021-08-23)