@roidev/kachina-md 2.1.2 ā 2.1.3
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.
- package/lib/client/Client.js +40 -53
- package/package.json +1 -1
package/lib/client/Client.js
CHANGED
|
@@ -3,7 +3,8 @@ import makeWASocket, {
|
|
|
3
3
|
useMultiFileAuthState,
|
|
4
4
|
makeCacheableSignalKeyStore,
|
|
5
5
|
fetchLatestBaileysVersion,
|
|
6
|
-
downloadMediaMessage
|
|
6
|
+
downloadMediaMessage,
|
|
7
|
+
Browsers
|
|
7
8
|
} from 'baileys';
|
|
8
9
|
import { Boom } from '@hapi/boom';
|
|
9
10
|
import pino from 'pino';
|
|
@@ -109,7 +110,7 @@ export class Client extends EventEmitter {
|
|
|
109
110
|
keys: makeCacheableSignalKeyStore(state.keys, this.config.logger)
|
|
110
111
|
},
|
|
111
112
|
logger: this.config.logger,
|
|
112
|
-
browser:
|
|
113
|
+
browser: Browsers.macOS('safari'),
|
|
113
114
|
getMessage: async (key) => {
|
|
114
115
|
if (this.store) {
|
|
115
116
|
const msg = await this.store.loadMessage(key.remoteJid, key.id);
|
|
@@ -125,67 +126,56 @@ export class Client extends EventEmitter {
|
|
|
125
126
|
|
|
126
127
|
this.sock.ev.on('creds.update', saveCreds);
|
|
127
128
|
|
|
128
|
-
// Handle pairing code request (
|
|
129
|
+
// Handle pairing code request (before connection.update listener)
|
|
129
130
|
if (this.config.loginMethod === 'pairing' && !state.creds.registered) {
|
|
130
131
|
if (!this.config.phoneNumber) {
|
|
131
132
|
throw new Error('Phone number is required for pairing method. Example: { phoneNumber: "628123456789" }');
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
const phoneNumber = this.config.phoneNumber.replace(/[^0-9]/g, '');
|
|
135
|
-
|
|
136
136
|
if (phoneNumber.length < 10) {
|
|
137
137
|
throw new Error('Invalid phone number format. Use country code without +. Example: 628123456789');
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
console.log('š Initiating pairing code process...');
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
if (isLastAttempt) {
|
|
176
|
-
console.error('ā Max pairing retries reached. Please try again later.');
|
|
177
|
-
throw error; // Re-throw on last attempt
|
|
178
|
-
} else {
|
|
179
|
-
console.log(`ā³ Retrying in 2 seconds...`);
|
|
180
|
-
}
|
|
142
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
143
|
+
let retryCount = 0;
|
|
144
|
+
const maxRetries = 3;
|
|
145
|
+
let pairingSuccess = false;
|
|
146
|
+
|
|
147
|
+
while (retryCount < maxRetries && !pairingSuccess) {
|
|
148
|
+
try {
|
|
149
|
+
await delay(3000);
|
|
150
|
+
const code = await this.sock.requestPairingCode(phoneNumber);
|
|
151
|
+
this.emit('pairing.code', code);
|
|
152
|
+
|
|
153
|
+
console.log('\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā');
|
|
154
|
+
console.log('ā WhatsApp Pairing Code ā');
|
|
155
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤');
|
|
156
|
+
console.log(`ā Code: ${chalk.bgYellowBright.white(code)} ā`);
|
|
157
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā');
|
|
158
|
+
console.log('\nSteps to pair:');
|
|
159
|
+
console.log('1. Open WhatsApp on your phone');
|
|
160
|
+
console.log('2. Go to Settings > Linked Devices');
|
|
161
|
+
console.log('3. Tap "Link a Device"');
|
|
162
|
+
console.log('4. Enter the code above\n');
|
|
163
|
+
|
|
164
|
+
pairingSuccess = true;
|
|
165
|
+
break;
|
|
166
|
+
} catch (err) {
|
|
167
|
+
retryCount++;
|
|
168
|
+
console.error(`Pairing attempt ${retryCount}/${maxRetries} failed:`, err.message);
|
|
169
|
+
this.emit('pairing.error', err);
|
|
170
|
+
|
|
171
|
+
if (retryCount >= maxRetries) {
|
|
172
|
+
console.error('ā Max pairing retries reached. Please try again later.');
|
|
173
|
+
throw err;
|
|
181
174
|
}
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
175
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
176
|
+
await delay(2000);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
189
179
|
}
|
|
190
180
|
|
|
191
181
|
this.sock.ev.on('connection.update', async (update) => {
|
|
@@ -233,6 +223,7 @@ export class Client extends EventEmitter {
|
|
|
233
223
|
async handleConnectionUpdate(update) {
|
|
234
224
|
const { connection, lastDisconnect, qr } = update;
|
|
235
225
|
|
|
226
|
+
// Handle QR code for QR login method
|
|
236
227
|
if (qr && this.config.loginMethod === 'qr') {
|
|
237
228
|
qrcode.generate(qr, { small: true });
|
|
238
229
|
}
|
|
@@ -253,15 +244,11 @@ export class Client extends EventEmitter {
|
|
|
253
244
|
this.user = this.sock.user;
|
|
254
245
|
this.emit('ready', this.user);
|
|
255
246
|
|
|
256
|
-
// Log success message
|
|
257
247
|
if (this.config.loginMethod === 'pairing') {
|
|
258
248
|
console.log('\nā Successfully connected via pairing code!\n');
|
|
259
249
|
}
|
|
260
250
|
} else if (connection === 'connecting') {
|
|
261
251
|
this.emit('connecting');
|
|
262
|
-
if (this.config.loginMethod === 'pairing') {
|
|
263
|
-
console.log('Waiting for pairing code confirmation...');
|
|
264
|
-
}
|
|
265
252
|
}
|
|
266
253
|
}
|
|
267
254
|
|