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.21k stars 3.63k forks source link

isAdmin returning "TypeError: Cannot read properties of undefined (reading 'isAdmin')" #2165

Closed TVARecordings closed 10 months ago

TVARecordings commented 1 year ago

Is there an existing issue for this?

Describe the bug

Visual Code Studio. NodeJS/Java etc all up-to-date.

Hello, i've got a open-source WA-Bot running, which sometimes (not everytime) returns "TypeError: Cannot read properties of undefined (reading 'isAdmin') at module.exports [as messageHandler] (D:\rzkytmgr Bot-SadBotBeforeAfterUpdate\handler\messageHandler.js:28:99)"

The line (28) is: "const isAdmin = groupMetadata ? groupMetadata.participants.find((res) => res.id === sender.id).isAdmin : undefined;"

This error only appears randomly after a 'command' has been issued, but does not error every time. In fact, it doesn't relate to a specific command either, nor a certain person or chat. It's out of the blue.

The error does not limit the running of the command, it runs anyway.

EDIT: It also appears when i start the bot with the command 'npm start'

Expected behavior

Expected: The error to not appear.

Steps to Reproduce the Bug or Issue

I really have no clue.

Relevant Code

module.exports = async (client, message) => { try { const msgAmount = await client.getAmountOfLoadedMessages(); if (msgAmount > 3000) await client.cutMsgCache();

const { id, body, mimetype, type, t, from, sender, content, caption, author, isGroupMsg, chat, quotedMsg, quotedMsgObj, mentionedJidList } = message;
const { name, shortName, pushname, formattedName } = sender;
const { formattedTitle, isGroup, contact, groupMetadata } = chat;

const botOwner = set.owner;
const botGroup = set.support;
const botPrefix = set.prefix;
const botNumber = (await client.getHostNumber()) + '@c.us';

const isAdmin = groupMetadata ? groupMetadata.participants.find((res) => res.id === sender.id).isAdmin : undefined;
const isOwner = groupMetadata ? groupMetadata.participants.find((res) => res.id === sender.id).isSuperAdmin : undefined;
const isBotAdmin = groupMetadata ? groupMetadata.participants.find((res) => res.id === botNumber).isAdmin : undefined;

(And more lines, but private)

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: W10, Visual Code Studio, NodeJS 20.0.0, Ubuntu latest version.

Additional context

No response

Goldn7799 commented 1 year ago

you can use const { participants } = chat to get lists of user Example to get is sender admin or not

const { participants, isGroup } = chat;
const isSenderAdmin = (isGroup) ? participants.find((participant) => participant.id. === sender.id).isAdmin : false;
yzyz17 commented 1 year ago

the slution : const contact = await message.getContact(); let num=contact.number; const chat = await message.getChat(); let IsAdmin=false; if(chat.isGroup){ for(let participant of chat.participants){ if(participant.id.user === num){ IsAdmin = participant.isAdmin; } }} console.log(IsAdmin) // if Admin will be true