innovators-bot2 1.2.5 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +10 -1
  2. package/example.js +79 -6
  3. package/index.js +125 -6
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -146,7 +146,16 @@ client.on('message-deleted', async (data) => {
146
146
  const groups = await client.getAllGroups()
147
147
 
148
148
  // Add participant to group
149
- await client.changeGroupParticipants(groupId, ['1234567890@s.whatsapp.net'], 'add')
149
+ // Note: If adding fails due to user's privacy settings (403),
150
+ // an invitation link is automatically sent to the user instead.
151
+ const result = await client.changeGroupParticipants(groupId, ['1234567890@s.whatsapp.net'], 'add')
152
+
153
+ if (result[0].status === 403 && result[0].invitationSent) {
154
+ console.log('User has privacy settings enabled. Invitation link sent!')
155
+ }
156
+
157
+ // Send a manual group invitation link
158
+ await client.sendGroupInvitation(groupId, '1234567890@s.whatsapp.net', 'Join my group!')
150
159
 
151
160
  // Remove participant
152
161
  await client.changeGroupParticipants(groupId, ['1234567890@s.whatsapp.net'], 'remove')
package/example.js CHANGED
@@ -398,6 +398,7 @@ async function start() {
398
398
  `*šŸ‘„ Group Management*\n` +
399
399
  `• !groups - List all your groups\n` +
400
400
  `• !add <number> - Add participant\n` +
401
+ `• !invite <number> - Send group invite link\n` +
401
402
  `• !remove <number> - Remove participant\n` +
402
403
  `• !promote <number> - Make admin\n` +
403
404
  `• !demote <number> - Remove admin\n\n` +
@@ -588,12 +589,32 @@ async function start() {
588
589
  break
589
590
  }
590
591
 
591
- const number = args.replace(/[^0-9]/g, '') + '@s.whatsapp.net'
592
- if (!number) {
593
- await client.sendMessage(msg.from, 'Please provide a valid phone number')
592
+ const rawNumber = args.replace(/[^0-9]/g, '')
593
+
594
+ // Validate phone number format
595
+ if (!rawNumber || rawNumber.length < 10) {
596
+ await client.sendMessage(msg.from,
597
+ `āŒ Invalid phone number format.\n\n` +
598
+ `āœ… Correct format: !${command.slice(1)} 923001234567\n` +
599
+ `(Include country code without + or spaces)`
600
+ )
601
+ break
602
+ }
603
+
604
+ // Ensure country code is present (check if starts with common codes)
605
+ if (rawNumber.startsWith('0')) {
606
+ await client.sendMessage(msg.from,
607
+ `āŒ Phone number must include country code.\n\n` +
608
+ `Example:\n` +
609
+ `• Pakistan: 923001234567 (not 03001234567)\n` +
610
+ `• India: 919876543210 (not 09876543210)\n` +
611
+ `• USA: 14155551234 (not 4155551234)`
612
+ )
594
613
  break
595
614
  }
596
615
 
616
+ const number = rawNumber + '@s.whatsapp.net'
617
+
597
618
  let action
598
619
  switch (command) {
599
620
  case '!add': action = 'add'; break
@@ -610,14 +631,66 @@ async function start() {
610
631
  demote: 'demoted in'
611
632
  }
612
633
 
613
- if (result[0].status === 200) {
634
+ if (result[0].status == 200) {
614
635
  await client.sendMessage(msg.from, `Successfully ${actionMap[action]} the group`)
636
+ } else if (result[0].status == 403 && result[0].invitationSent) {
637
+ await client.sendMessage(msg.from, `āš ļø Could not add directly due to privacy settings.\nāœ… Group invitation link has been sent to the user instead!`)
615
638
  } else {
616
- await client.sendMessage(msg.from, `Failed to ${action} participant: ${result[0].content}`)
639
+ await client.sendMessage(msg.from, `Failed to ${action} participant: ${result[0].message || result[0].content || result[0].error || 'Unknown error'}`)
617
640
  }
618
641
  } catch (error) {
619
642
  console.error(`Error ${command} participant:`, error)
620
- await client.sendMessage(msg.from, `Failed to ${command.slice(1)} participant`)
643
+ if (error.output?.statusCode === 408) {
644
+ await client.sendMessage(msg.from, `ā±ļø Request timed out. The number might be invalid or not on WhatsApp.`)
645
+ } else {
646
+ await client.sendMessage(msg.from, `Failed to ${command.slice(1)} participant: ${error.message || 'Unknown error'}`)
647
+ }
648
+ }
649
+ break
650
+
651
+
652
+ case '!invite':
653
+ try {
654
+ if (!msg.raw.key.remoteJid.endsWith('@g.us')) {
655
+ await client.sendMessage(msg.from, 'This command can only be used in groups')
656
+ break
657
+ }
658
+
659
+ const rawInviteNumber = args.replace(/[^0-9]/g, '')
660
+
661
+ // Validate phone number format
662
+ if (!rawInviteNumber || rawInviteNumber.length < 10) {
663
+ await client.sendMessage(msg.from,
664
+ `āŒ Invalid phone number format.\n\n` +
665
+ `āœ… Correct format: !invite 923001234567\n` +
666
+ `(Include country code without + or spaces)`
667
+ )
668
+ break
669
+ }
670
+
671
+ // Ensure country code is present
672
+ if (rawInviteNumber.startsWith('0')) {
673
+ await client.sendMessage(msg.from,
674
+ `āŒ Phone number must include country code.\n\n` +
675
+ `Example:\n` +
676
+ `• Pakistan: 923001234567 (not 03001234567)\n` +
677
+ `• India: 919876543210 (not 09876543210)\n` +
678
+ `• USA: 14155551234 (not 4155551234)`
679
+ )
680
+ break
681
+ }
682
+
683
+ const inviteNumber = rawInviteNumber + '@s.whatsapp.net'
684
+
685
+ await client.sendGroupInvitation(msg.raw.key.remoteJid, inviteNumber)
686
+ await client.sendMessage(msg.from, `āœ… Group invitation sent to ${rawInviteNumber}`)
687
+ } catch (error) {
688
+ console.error('Error sending invitation:', error)
689
+ if (error.output?.statusCode === 408) {
690
+ await client.sendMessage(msg.from, `ā±ļø Request timed out. The number might be invalid or not on WhatsApp.`)
691
+ } else {
692
+ await client.sendMessage(msg.from, `Failed to send group invitation: ${error.message || 'Unknown error'}`)
693
+ }
621
694
  }
622
695
  break
623
696
  }
package/index.js CHANGED
@@ -864,12 +864,75 @@ class WhatsAppClient extends EventEmitter {
864
864
 
865
865
  try {
866
866
  for (const participantId of participantIds) {
867
- const result = await this.sock.groupParticipantsUpdate(
868
- groupId,
869
- [participantId],
870
- action
871
- );
872
- results.push(result);
867
+ try {
868
+ let updateResult = await this.sock.groupParticipantsUpdate(
869
+ groupId,
870
+ [participantId],
871
+ action
872
+ );
873
+
874
+ // Baileys may return an array or a single object.
875
+ // We want to handle the first result for this participant.
876
+ let participantResult = Array.isArray(updateResult) ? updateResult[0] : updateResult;
877
+
878
+ // If it's the { [jid]: { code: ... } } format, extract it
879
+ if (participantResult && participantResult[participantId]) {
880
+ participantResult = {
881
+ status: participantResult[participantId].code,
882
+ jid: participantId,
883
+ ...participantResult[participantId]
884
+ };
885
+ }
886
+
887
+ // Normalize status to number
888
+ const status = participantResult?.status || participantResult?.code;
889
+
890
+ if (action === 'add' && (status === 403 || status === '403')) {
891
+ console.log(`Privacy settings prevent adding ${participantId}. Sending invitation link...`);
892
+
893
+ try {
894
+ await this.sendGroupInvitation(groupId, participantId, 'You have been invited to join this group');
895
+ participantResult.invitationSent = true;
896
+ participantResult.message = 'Invitation link sent due to privacy settings';
897
+ participantResult.status = 403;
898
+ } catch (invError) {
899
+ console.error('Failed to send auto-invitation:', invError);
900
+ participantResult.invitationSent = false;
901
+ participantResult.error = 'Privacy restricted, and failed to send invitation link';
902
+ participantResult.status = 403;
903
+ }
904
+ } else if (participantResult) {
905
+ participantResult.status = status ? parseInt(status) : 500;
906
+ }
907
+
908
+ results.push(participantResult || { status: 500, jid: participantId, error: 'Empty result' });
909
+ } catch (participantError) {
910
+ // Handle cases where Baileys might throw instead of returning 403
911
+ const statusCode = participantError.output?.statusCode || participantError.data?.status;
912
+
913
+ if (action === 'add' && (statusCode === 403 || statusCode === '403')) {
914
+ console.log(`Privacy error caught in throw: Sending invitation link...`);
915
+ try {
916
+ await this.sendGroupInvitation(groupId, participantId, 'You have been invited to join this group');
917
+ results.push({
918
+ status: 403,
919
+ jid: participantId,
920
+ invitationSent: true,
921
+ message: 'Invitation link sent due to privacy settings'
922
+ });
923
+ continue;
924
+ } catch (invError) {
925
+ console.error('Failed to send fallback invitation:', invError);
926
+ }
927
+ }
928
+
929
+ console.error(`Error updating participant ${participantId}:`, participantError);
930
+ results.push({
931
+ status: statusCode || 500,
932
+ jid: participantId,
933
+ error: participantError.message || 'Unknown error'
934
+ });
935
+ }
873
936
  }
874
937
  return results;
875
938
  } catch (error) {
@@ -877,6 +940,62 @@ class WhatsAppClient extends EventEmitter {
877
940
  throw error;
878
941
  }
879
942
  }
943
+
944
+ /**
945
+ * Get group name from metadata
946
+ * @param {string} groupId - The group JID
947
+ * @returns {Promise<string>} The group name
948
+ * @private
949
+ */
950
+ async _getGroupName(groupId) {
951
+ try {
952
+ const metadata = await this.getGroupMetadata(groupId);
953
+ return metadata.subject || 'Group';
954
+ } catch (error) {
955
+ console.error('Error getting group name:', error);
956
+ return 'Group';
957
+ }
958
+ }
959
+
960
+ /**
961
+ * Send a group invitation link to a user
962
+ * @param {string} groupId - The group JID
963
+ * @param {string} participantId - The participant JID to send invitation to
964
+ * @param {string} [customMessage] - Optional custom invitation message
965
+ * @returns {Promise<object>} The sent message info
966
+ * @throws {Error} If client is not connected or an error occurs
967
+ */
968
+ async sendGroupInvitation(groupId, participantId, customMessage) {
969
+ if (!this.isConnected) {
970
+ throw new Error('Client is not connected');
971
+ }
972
+
973
+ try {
974
+ // Get group invite code
975
+ const inviteCode = await this.sock.groupInviteCode(groupId);
976
+ const groupName = await this._getGroupName(groupId);
977
+
978
+ // Create the invitation link
979
+ const inviteLink = `https://chat.whatsapp.com/${inviteCode}`;
980
+
981
+ // Create the invitation message
982
+ const message = customMessage
983
+ ? `${customMessage}\n\n${inviteLink}`
984
+ : `šŸ“Ø *Group Invitation*\n\n` +
985
+ `You have been invited to join:\n` +
986
+ `*${groupName}*\n\n` +
987
+ `Click the link below to join:\n${inviteLink}`;
988
+
989
+ // Send as text message
990
+ return await this.sock.sendMessage(participantId, {
991
+ text: message
992
+ }, { ai: true });
993
+ } catch (error) {
994
+ console.error('Error sending group invitation:', error);
995
+ throw error;
996
+ }
997
+ }
998
+
880
999
  /**
881
1000
  * Mark a message as read
882
1001
  * @param {object|string} messageKey - The message key object or message ID
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "innovators-bot2",
3
- "version": "1.2.5",
3
+ "version": "1.2.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {