Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client.getUnreadMessages(true, false, true); returns TypeError: Cannot read properties of undefined (reading 'id') #3207

Open
1 task done
TVARecordings opened this issue May 8, 2024 · 0 comments

Comments

@TVARecordings
Copy link

TVARecordings commented May 8, 2024

Are you using the latest version of the library?

  • I have checked and am using the latest version of the library.

What type of session are you experiencing this issue on?

Multi-device and I have set multiDevice to true in my config OR am using the --multi-device flag

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

Business account

Mode

EASY API/CLI

Current Behavior

Error output:

...

CODE

conLog(`Fetching unreadMessages`);
const unreadMessages = await Client.getUnreadMessages(true, false, true);
conLog(`Finished fetching unreadMessages`);

const processMessages = async (messages) => {
  let processedCount = 0;
  const totalMessages = messages.length;

  for (const message of messages) {
    try {
      const progressPercentage = ((processedCount / totalMessages) * 100).toFixed(2);
      console.log(`Processing message #${processedCount} of #${totalMessages}`);
      const startTimeMsg = performance.now();
      await Client.sendSeen(message.chatId);
      const Contact = await Client.getContact(message.sender.id);
      await Client.sendSeen(message.chatId);

      if (message.type === 'image' || message.type === 'video' || message.type === 'chat') {
       if ((message.caption && message.caption.startsWith('!')) || (message.body && message.body.startsWith('!'))) {
          await Handler.messageHandler(Client, message, startTimeMsg);
          chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, message.body, message.t, progressPercentage);
          await UpdateUserKarma(Client, message.sender.id, message);
        } else {
          let logMessage = '';
          if (message.type === 'image') {
            logMessage = `Picture 📸`;
          } else if (message.type === 'video') {
            logMessage = `Video 📹`;
          } else {
            logMessage = message.body;
          }
          chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, logMessage, message.t, progressPercentage);
          await UpdateUserKarma(Client, message.sender.id, message);
        }
      } else if (message.type === 'sticker') {
        chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, `Sticker`, message.t, progressPercentage);
        await UpdateUserKarma(Client, message.sender.id, message);
      } else {
        chatLog(message.chat.name ? message.chat.name : message.chatId || 'A chat.name?', Contact.name || Contact.pushname || Contact.shortName || 'A senders name?', `${message.type || 'Unknown message.type'} `, message.t || '', progressPercentage);
      }

      processedCount++;        
    } catch (error) {
      errorLog(`\n${error}`);
      console.error(`---${error}---\n${JSON.stringify(message,false,2)}\n`)
    }
  }

  announce(`Finished processing older messages.`);
};

try {
  announce(`Started processing older messages.`);
  await processMessages(unreadMessages);
} catch (error) {
  errorLog(`Error processing unread messages! ${error}`);
  console.error(`Error processing unread messages! ${error}\n---Message---\n${message}`);
}





const LoadedMessages = await Client.getAmountOfLoadedMessages()
conLog(`A total of ${LoadedMessages} messages are in cache. `)
 if (LoadedMessages >= 750) {
  announce(`Cutting Message Cache, there were ${LoadedMessages} loaded message in cache.`)
  const newLoadedMessages = await Client.cutMsgCache()
  announce(`Cutted Message Cache (${LoadedMessages}), amount of loaded message now: ${newLoadedMessages}`)
} else {
  announce(`Amount of loaded messages: ${LoadedMessages}`)
}

Expected Behavior

Didn't expect this error.

This error started appearing since the framework update of WA-Web

Steps To Reproduce

conLog(`Fetching unreadMessages`);
const unreadMessages = await Client.getUnreadMessages(true, false, true);
conLog(`Finished fetching unreadMessages`);

const processMessages = async (messages) => {
  let processedCount = 0;
  const totalMessages = messages.length;

  for (const message of messages) {
    try {
      const progressPercentage = ((processedCount / totalMessages) * 100).toFixed(2);
      console.log(`Processing message #${processedCount} of #${totalMessages}`);
      const startTimeMsg = performance.now();
      await Client.sendSeen(message.chatId);
      const Contact = await Client.getContact(message.sender.id);
      await Client.sendSeen(message.chatId);

      if (message.type === 'image' || message.type === 'video' || message.type === 'chat') {
       if ((message.caption && message.caption.startsWith('!')) || (message.body && message.body.startsWith('!'))) {
          await Handler.messageHandler(Client, message, startTimeMsg);
          chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, message.body, message.t, progressPercentage);
          await UpdateUserKarma(Client, message.sender.id, message);
        } else {
          let logMessage = '';
          if (message.type === 'image') {
            logMessage = `Picture 📸`;
          } else if (message.type === 'video') {
            logMessage = `Video 📹`;
          } else {
            logMessage = message.body;
          }
          chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, logMessage, message.t, progressPercentage);
          await UpdateUserKarma(Client, message.sender.id, message);
        }
      } else if (message.type === 'sticker') {
        chatLog(message.chat.name ? message.chat.name : message.chatId, Contact.name || Contact.pushname || Contact.shortName, `Sticker`, message.t, progressPercentage);
        await UpdateUserKarma(Client, message.sender.id, message);
      } else {
        chatLog(message.chat.name ? message.chat.name : message.chatId || 'A chat.name?', Contact.name || Contact.pushname || Contact.shortName || 'A senders name?', `${message.type || 'Unknown message.type'} `, message.t || '', progressPercentage);
      }

      processedCount++;        
    } catch (error) {
      errorLog(`\n${error}`);
      console.error(`---${error}---\n${JSON.stringify(message,false,2)}\n`)
    }
  }

  announce(`Finished processing older messages.`);
};

try {
  announce(`Started processing older messages.`);
  await processMessages(unreadMessages);
} catch (error) {
  errorLog(`Error processing unread messages! ${error}`);
  console.error(`Error processing unread messages! ${error}\n---Message---\n${message}`);
}





const LoadedMessages = await Client.getAmountOfLoadedMessages()
conLog(`A total of ${LoadedMessages} messages are in cache. `)
 if (LoadedMessages >= 750) {
  announce(`Cutting Message Cache, there were ${LoadedMessages} loaded message in cache.`)
  const newLoadedMessages = await Client.cutMsgCache()
  announce(`Cutted Message Cache (${LoadedMessages}), amount of loaded message now: ${newLoadedMessages}`)
} else {
  announce(`Amount of loaded messages: ${LoadedMessages}`)
}

create() code OR full CLI command + CONFIG

const configObject = {
  sessionId: 'Mine',
  authTimeout: 0,
  cacheEnabled: true,
  useChrome: true,
  cachedPatch: true, 
  devtools: false,
  //chromiumArgs: ['--no-sandbox'],
  disableSpins: true,
  headless: true, //new
  popup: false,
  qrTimeout: 0,
  messagePreprocessor: "AUTO_DECRYPT", //"AUTO_DECRYPT","SCRUB"
  linkParser: "https://link.openwa.cloud/api",
  logInternalEvents: false, //ONLY TURN THIS ON IF ASKED TO!
  discord: 'Mine',
  ezqr: true,
  callTimeout: 1200000, // 600000
  licenseKey: "Mine", 
  screenshotOnInitializationBrowserError: false,
  hostNotificationLang: 'nl-nl',
  killProcessOnBrowserClose: true,
  killProcessOnTimeout: false,
  logConsole: true,
  logConsoleErrors: true,
  logFile: true,
  onError: "LOG_AND_FALSE",
  blockCrashLogs: true,
};

DEBUG INFO

{
  "WA_VERSION": "2.3000.1013355796",
  "WA_AUTOMATE_VERSION": "4.71.9 UPDATE AVAILABLE: 4.71.10",
  "BROWSER_VERSION": "Chrome/124.0.6367.119",
  "START_TS": 1715190172121,
  "RAM_INFO": "Total: 68.62 GB | Free: 53.89 GB",
  "PPTR_VERSION": "19.11.1",
  "LATEST_VERSION": false,
  "CLI": false
}

Environment

- OS: Windows 10
- Node: 20.11.1
- npm: 10.2.4

Screenshots/Logs

No response

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant