innovators-bot2 1.1.8 → 1.1.10

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 (2) hide show
  1. package/index.js +116 -6
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -13,6 +13,7 @@ const path = require('path');
13
13
  const mime = require('mime');
14
14
  const figlet = require('figlet');
15
15
  const readline = require('readline');
16
+ const NodeCache = require('node-cache'); // Added for group metadata caching
16
17
 
17
18
  let rl;
18
19
  const question = (text) => {
@@ -82,6 +83,8 @@ class WhatsAppClient extends EventEmitter {
82
83
  this._connectionState = 'disconnected' // Track connection state
83
84
  this.authmethod = config.authmethod || 'qr'; // 'qr' or 'pairing'
84
85
  this._reconnectDelay = 5000; // 5 seconds
86
+ // Initialize the group metadata cache at the class level
87
+ this.groupMetadataCache = new NodeCache({ stdTTL: 600, checkperiod: 120 }); // 10 minutes TTL
85
88
  }
86
89
 
87
90
  async connect() {
@@ -93,7 +96,7 @@ class WhatsAppClient extends EventEmitter {
93
96
  }
94
97
 
95
98
  const { version, isLatest } = await fetchLatestBaileysVersion();
96
- console.log(`Using WA version v${version.join('.')}, is latest: ${isLatest}`);
99
+ //console.log(`Using WA version v${version.join('.')}, is latest: ${isLatest}`);
97
100
 
98
101
  const { state, saveCreds } = await useMultiFileAuthState(this.sessionName)
99
102
  const logger = P({ level: 'silent' })
@@ -106,6 +109,26 @@ class WhatsAppClient extends EventEmitter {
106
109
  generateHighQualityLinkPreview: true,
107
110
  linkPreviewImageThumbnailWidth: 192,
108
111
  emitOwnEvents: true,
112
+ version,
113
+ // Implement cachedGroupMetadata to prevent rate limiting when sending messages to groups
114
+ cachedGroupMetadata: async (jid) => {
115
+ // Check if we have cached metadata for this group
116
+ const cached = this.groupMetadataCache.get(jid);
117
+ if (cached) {
118
+ return cached;
119
+ }
120
+
121
+ // If not cached, fetch the group metadata
122
+ try {
123
+ const metadata = await this.sock.groupMetadata(jid);
124
+ // Cache the metadata for future use
125
+ this.groupMetadataCache.set(jid, metadata);
126
+ return metadata;
127
+ } catch (error) {
128
+ console.error(`Error fetching metadata for group ${jid}:`, error);
129
+ return null;
130
+ }
131
+ }
109
132
  });
110
133
 
111
134
  // Initialize store for LID/PN mapping
@@ -338,11 +361,40 @@ class WhatsAppClient extends EventEmitter {
338
361
  // Handle credential updates
339
362
  this.sock.ev.on('creds.update', saveCreds);
340
363
 
341
- } catch (error) {
342
- console.error('Error in connect:', error);
343
- this.emit('error', error);
344
- throw error;
345
- }
364
+ // Handle group events to keep the cache updated
365
+ this.sock.ev.on('groups.upsert', (groups) => {
366
+ for (const group of groups) {
367
+ this.updateGroupMetadataCache(group.id, group);
368
+ }
369
+ });
370
+
371
+ this.sock.ev.on('groups.update', (groups) => {
372
+ for (const group of groups) {
373
+ // Get existing cache and update it with new information
374
+ const cached = this.groupMetadataCache.get(group.id);
375
+ if (cached) {
376
+ // Merge the updated fields with existing cached data
377
+ const updated = { ...cached, ...group };
378
+ this.updateGroupMetadataCache(group.id, updated);
379
+ }
380
+ }
381
+ });
382
+
383
+ this.sock.ev.on('group-participants.update', async (update) => {
384
+ // When participants change, refresh the group metadata
385
+ try {
386
+ const metadata = await this.sock.groupMetadata(update.id);
387
+ this.updateGroupMetadataCache(update.id, metadata);
388
+ } catch (error) {
389
+ console.error(`Error refreshing metadata for group ${update.id}:`, error);
390
+ }
391
+ });
392
+
393
+ } catch (error) {
394
+ console.error('Error in connect:', error);
395
+ this.emit('error', error);
396
+ throw error;
397
+ }
346
398
  }
347
399
 
348
400
  /**
@@ -946,6 +998,64 @@ class WhatsAppClient extends EventEmitter {
946
998
  }
947
999
  }
948
1000
 
1001
+ /**
1002
+ * Get group metadata with caching
1003
+ * @param {string} jid - The group JID
1004
+ * @returns {Promise<object>} The group metadata
1005
+ */
1006
+ async getGroupMetadata(jid) {
1007
+ if (!this.isConnected) {
1008
+ throw new Error('Client is not connected');
1009
+ }
1010
+
1011
+ // Check cache first
1012
+ const cached = this.groupMetadataCache.get(jid);
1013
+ if (cached) {
1014
+ return cached;
1015
+ }
1016
+
1017
+ // Fetch from WhatsApp if not cached
1018
+ try {
1019
+ const metadata = await this.sock.groupMetadata(jid);
1020
+ // Cache the result
1021
+ this.groupMetadataCache.set(jid, metadata);
1022
+ return metadata;
1023
+ } catch (error) {
1024
+ console.error(`Error fetching metadata for group ${jid}:`, error);
1025
+ throw error;
1026
+ }
1027
+ }
1028
+
1029
+ /**
1030
+ * Update group metadata in cache
1031
+ * @param {string} jid - The group JID
1032
+ * @param {object} metadata - The group metadata
1033
+ */
1034
+ updateGroupMetadataCache(jid, metadata) {
1035
+ if (this.groupMetadataCache) {
1036
+ this.groupMetadataCache.set(jid, metadata);
1037
+ }
1038
+ }
1039
+
1040
+ /**
1041
+ * Clear group metadata from cache
1042
+ * @param {string} jid - The group JID
1043
+ */
1044
+ clearGroupMetadataCache(jid) {
1045
+ if (this.groupMetadataCache) {
1046
+ this.groupMetadataCache.del(jid);
1047
+ }
1048
+ }
1049
+
1050
+ /**
1051
+ * Clear all group metadata from cache
1052
+ */
1053
+ clearAllGroupMetadataCache() {
1054
+ if (this.groupMetadataCache) {
1055
+ this.groupMetadataCache.flushAll();
1056
+ }
1057
+ }
1058
+
949
1059
  /**
950
1060
  * Log out from WhatsApp
951
1061
  * @returns {Promise<boolean>} True if logout was successful
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "innovators-bot2",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -35,6 +35,7 @@
35
35
  "pino": "^9.6.0",
36
36
  "qrcode-terminal": "^0.12.0",
37
37
  "figlet": "^1.8.0",
38
- "mime": "^3.0.0"
38
+ "mime": "^3.0.0",
39
+ "node-cache": "^5.1.2"
39
40
  }
40
41
  }