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
14.95k stars 3.55k forks source link

.getChats() returning non-joined group chat #2655

Closed devsakae closed 8 months ago

devsakae commented 9 months ago

Is there an existing issue for this?

Describe the bug

My bot was joining groups freely (until I configured to only do it if join request was made by my personal number).

So, it happened to be invited, automatically joined, stayed for a couple minutes, and was removed by an admin of "group X", when I saw it. Then I manually erased the group on the WhatsApp (Business) App.

Now, when my bot starts, as it fetches all chats with .getChats(), it still shows that group on the list, despite it's not active, not joined, not even on the chat list of my WPB app.

I even removed .wwebjs_cache, thinking that would be a cache problem, but the problem persists.

Expected behavior

Expect to not show old or not joined groups, but .getChats() are showing a specifically one

Steps to Reproduce the Bug or Issue

  1. Run client
  2. Run and print (async) client.getChats()
  3. It shows one group that client has (auto) joined and removed by an admin

Relevant Code

client.on('ready', async () => {
  const allChats = await client.getChats();
  console.log(allChats); // THIS SHOWS GROUP X
  await Promise.all(allChats
    .filter((group) => !group.isReadOnly && group.isGroup)
    .map(async (group) => {
      if (Object.hasOwn(config.grupos, group.id_serialized) && config.groups[group.id_serialized]?.palpiteiros.length > 0) return '';
      config.grupos[group.id._serialized] = {
        palpiteiros: [],
      };
      console.log('✔️ ', group.name); // THIS PRINTS GROUP X NAME
      await group.sendSeen();
      await group.clearMessages();
    }));
  }
)

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

OS: Ubuntu 22.04 Phone OS: Android and iOS wweb.js: github:alechkos/whatsapp-web.js#channels Node: 20.9.0

Additional context

No response

devsakae commented 9 months ago

This is Group X (personal info was REMOVED_BY_DEVSAKAE)

GroupChat {
    groupMetadata: {
      id: [Object],
      creation: 1401890927,
      owner: [Object],
      subject: 'REMOVED_BY_DEVSAKAE',
      subjectTime: 1700836188,
      desc: 'REMOVED_BY_DEVSAKAE',
      descId: '1691477925-477',
      descTime: 0,
      restrict: false,
      announce: false,
      noFrequentlyForwarded: false,
      ephemeralDuration: 0,
      membershipApprovalMode: false,
      reportToAdminMode: false,
      size: 9,
      support: false,
      suspended: false,
      terminated: false,
      uniqueShortNameMap: {},
      isLidAddressingMode: false,
      isParentGroup: false,
      isParentGroupClosed: false,
      defaultSubgroup: false,
      generalSubgroup: false,
      generalChatAutoAddDisabled: false,
      allowNonAdminSubGroupCreation: false,
      lastActivityTimestamp: 0,
      lastSeenActivityTimestamp: 0,
      incognito: false,
      participants: [Array],
      pendingParticipants: [],
      pastParticipants: [],
      membershipApprovalRequests: [],
      subgroupSuggestions: []
    },
    id: {
      server: 'g.us',
      user: 'REMOVED_BY_DEVSAKAE',
      _serialized: 'REMOVED_BY_DEVSAKAE@g.us'
    },
    name: 'REMOVED_BY_DEVSAKAE',
    isGroup: true,
    isReadOnly: false,
    unreadCount: 0,
    timestamp: 1701256209,
    archived: undefined,
    pinned: false,
    isMuted: false,
    muteExpiration: 0,
    lastMessage: Message {
      _data: [Object],
      mediaKey: undefined,
      id: [Object],
      ack: undefined,
      hasMedia: false,
      body: '',
      type: 'e2e_notification',
      timestamp: 1701262798,
      from: 'REMOVED_BY_DEVSAKAE@g.us',
      to: 'REMOVED_BY_DEVSAKAE@c.us',
      author: undefined,
      deviceType: 'android',
      isForwarded: false,
      forwardingScore: 0,
      isStatus: false,
      isStarred: false,
      broadcast: undefined,
      fromMe: false,
      hasQuotedMsg: false,
      hasReaction: false,
      duration: undefined,
      location: undefined,
      vCards: [],
      inviteV4: undefined,
      mentionedIds: [],
      orderId: undefined,
      token: undefined,
      isGif: false,
      isEphemeral: undefined,
      links: []
    }
  }
alechkos commented 8 months ago

Client.getChats returns chat that are stored in Chat Store, also groups that you were removed from or left You can use Chat.delete to remove the chat/group from the Store

devsakae commented 8 months ago

Thanks for this.

Wouldn't be nice to execute this delete by default when you are remove from a group chat?