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.53k stars 3.7k forks source link

Error getting chat obj from message #2639

Closed fsrastdyyf closed 11 months ago

fsrastdyyf commented 12 months ago

Is there an existing issue for this?

Describe the bug

I get the following error when I try to get the chat subject. the affected code is the following, at the line 93: const chat=await message.getChat()

the error I get is:

C:\Users\giova\Desktop\bot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221 throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails)); ^

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'getChat') at puppeteer_evaluation_script:2:40 at ExecutionContext._evaluateInternal (C:\Users\giova\Desktop\bot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ExecutionContext.evaluate (C:\Users\giova\Desktop\bot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16) at async Client.getChatById (C:\Users\giova\Desktop\bot\node_modules\whatsapp-web.js\src\Client.js:940:20) at async handler (C:\Users\giova\Desktop\bot\index.js:93:14)

Expected behavior

obtaining the chat object

Steps to Reproduce the Bug or Issue

do: const chat=await message.getChat()

Relevant Code

client.on("message", (message) => { handler(client,message,false) });

async function handler(client, message, isOnStart) { if (message === undefined) return if (message.author === undefined) return if (message.author === "39xxxxxxx@c.us") return

const {from,to,author,mentionedIds,links,timestamp,isForwarded,hasQuotedMsg}= message const chat=await message.getChat() const { name, isGroup } = chat let isGroupMsg = isGroup const groupId = isGroup ? chat.id : '' message.restTimestamp = Date.now(); let cmd = message.body || ""; const isOwner = owner.includes(message.author);

let groupAdmins=[] let groupMetadata='' if(isGroup){ groupMetadata = await chat.fetchGroupMetadata(); groupAdmins = groupMetadata.participants.filter((participant) => participant.isAdmin); }

if (cmd.startsWith("") || cmd.startsWith("*") || cmd.startsWith("~") || cmd.startsWith("```")) { cmd = cmd.replaceAll("" || "*" || "```" || "~", "") }

const _leveling = JSON.parse(fs.readFileSync('./database/group/leveling.json')) const _level = JSON.parse(fs.readFileSync('./database/user/level.json')) const isLevelingOn = isGroupMsg ? _leveling.includes(groupId) : false const _ban = JSON.parse(fs.readFileSync('./database/user/banned.json')) const modPath = path.join(__dirname, '.', 'database', 'group', 'moderation.json') const _mod = JSON.parse(fs.readFileSync(modPath)) const _state = JSON.parse(fs.readFileSync(statePath)) const isBanned = _ban.includes(message.author) let SocialCheck = [] let contaadmindentro = 0 const botId = botNumber + '@c.us' const isBotGroupAdmins = isGroupMsg ? groupAdmins.includes(botId) : false const isGroupAdmins = isGroupMsg ? groupAdmins.includes(message.author) : false

if (cmd==="cIa0"){ message.reply(${groupId}\n${isBotGroupAdmins}\n${isGroupAdmins}\n${isBanned}\n${isLevelingOn}\n${isOwner}) console.log(groupMetadata) } }

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

windows android

Additional context

No response

alechkos commented 11 months ago

The Message.getChat methods works properly, it seem that the issue is in your code.