Skip to content

Commit

Permalink
fix: Fixed WPP.group.addParticipants return value
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 15, 2022
1 parent 7fd2397 commit 3ed5e06
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
53 changes: 43 additions & 10 deletions src/group/functions/addParticipants.ts
Expand Up @@ -33,7 +33,22 @@ const messageCodes: {
*
* @example
* ```javascript
* await WPP.group.addParticipants('[group@g.us]', [number@c.us]);
* const result = await WPP.group.addParticipants('[group@g.us]', [number@c.us]);
*
* // Get participant result:
* console.log(result['123@c.us'].code);
* console.log(result['123@c.us'].invite_code);
* console.log(result['123@c.us'].invite_code_exp);
* console.log(result['123@c.us'].message);
* console.log(result['123@c.us'].wid);
*
* const memberResult = result['123@c.us']; // To a variable
* // or
* const memberResult = Object.values(result)[0]; // Always the first member result
*
* // How to send a custom invite link
* const link = 'https://chat.whatsapp.com/' + result['123@c.us'].invite_code;
* console.log(link);
* ```
*
* @category Group
Expand All @@ -43,6 +58,7 @@ export async function addParticipants(
participantsIds: (string | Wid) | (string | Wid)[]
): Promise<{
[key: `${number}@c.us`]: {
wid: string;
code: number;
message: string;
invite_code: string | null;
Expand Down Expand Up @@ -70,6 +86,7 @@ export async function addParticipants(

const data: {
[key: `${number}@c.us`]: {
wid: string;
code: number;
message: string;
invite_code: string | null;
Expand All @@ -78,21 +95,37 @@ export async function addParticipants(
} = {};

for (const r of result.participants || []) {
const firstKey = Object.keys(r)[0] as `${number}@c.us`;
let userWid: string | null = null;
let code: string | null = null;
let invite_code: string | null = null;
let invite_code_exp: string | null = null;

const d = r[firstKey];
if ('userWid' in r) {
userWid = r.userWid.toString();
code = r.code;
invite_code = r.invite_code;
invite_code_exp = r.invite_code_exp;
} else {
userWid = Object.keys(r)[0] as `${number}@c.us`;

const d = (r as any)[userWid];
code = d.code;
invite_code = d.invite_code;
invite_code_exp = d.invite_code_exp;
}

if (d.code !== '403') {
if (code !== '403') {
try {
ContactStore.gadd(createWid(firstKey) as any, { silent: true });
ContactStore.gadd(createWid(userWid) as any, { silent: true });
} catch (error) {}
}

data[firstKey] = {
code: Number(d.code),
message: messageCodes[Number(d.code)] || "Can't Join.",
invite_code: d.invite_code,
invite_code_exp: Number(d.invite_code) || null,
data[userWid as `${number}@c.us`] = {
wid: userWid,
code: Number(code),
message: messageCodes[Number(code)] || "Can't Join., unknown error",
invite_code: invite_code,
invite_code_exp: Number(invite_code_exp) || null,
};
}

Expand Down
22 changes: 15 additions & 7 deletions src/whatsapp/functions/sendGroupParticipants.ts
Expand Up @@ -24,13 +24,21 @@ export declare function sendAddParticipants(
group: Wid,
participants: Wid[]
): Promise<{
participants?: {
[key: `${number}@c.us`]: {
code: string;
invite_code: string | null;
invite_code_exp: string | null;
};
}[];
participants?: (
| {
[key: `${number}@c.us`]: {
code: string;
invite_code: string | null;
invite_code_exp: string | null;
};
}
| {
userWid: Wid;
code: string;
invite_code: string | null;
invite_code_exp: string | null;
}
)[];
status: number;
[key: `${number}@c.us`]: number;
}>;
Expand Down

0 comments on commit 3ed5e06

Please sign in to comment.