@roidev/kachina-md 2.1.1 ā 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 +31 -17
- 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';
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
createSticker,
|
|
16
17
|
StickerTypes
|
|
17
18
|
} from '../helpers/sticker.js';
|
|
19
|
+
import chalk from 'chalk';
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* @typedef {Object} ClientOptions
|
|
@@ -108,7 +110,7 @@ export class Client extends EventEmitter {
|
|
|
108
110
|
keys: makeCacheableSignalKeyStore(state.keys, this.config.logger)
|
|
109
111
|
},
|
|
110
112
|
logger: this.config.logger,
|
|
111
|
-
browser:
|
|
113
|
+
browser: Browsers.macOS('safari'),
|
|
112
114
|
getMessage: async (key) => {
|
|
113
115
|
if (this.store) {
|
|
114
116
|
const msg = await this.store.loadMessage(key.remoteJid, key.id);
|
|
@@ -124,41 +126,56 @@ export class Client extends EventEmitter {
|
|
|
124
126
|
|
|
125
127
|
this.sock.ev.on('creds.update', saveCreds);
|
|
126
128
|
|
|
127
|
-
// Handle pairing code request (
|
|
129
|
+
// Handle pairing code request (before connection.update listener)
|
|
128
130
|
if (this.config.loginMethod === 'pairing' && !state.creds.registered) {
|
|
129
131
|
if (!this.config.phoneNumber) {
|
|
130
132
|
throw new Error('Phone number is required for pairing method. Example: { phoneNumber: "628123456789" }');
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
// Format phone number (remove + and spaces)
|
|
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
|
-
|
|
141
|
-
|
|
140
|
+
console.log('š Initiating pairing code process...');
|
|
141
|
+
|
|
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) {
|
|
142
148
|
try {
|
|
149
|
+
await delay(3000);
|
|
143
150
|
const code = await this.sock.requestPairingCode(phoneNumber);
|
|
144
151
|
this.emit('pairing.code', code);
|
|
145
152
|
|
|
146
|
-
// Log to console with formatting
|
|
147
153
|
console.log('\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā');
|
|
148
154
|
console.log('ā WhatsApp Pairing Code ā');
|
|
149
155
|
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤');
|
|
150
|
-
console.log(`ā Code: ${code}
|
|
156
|
+
console.log(`ā Code: ${chalk.bgYellowBright.white(code)} ā`);
|
|
151
157
|
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā');
|
|
152
158
|
console.log('\nSteps to pair:');
|
|
153
159
|
console.log('1. Open WhatsApp on your phone');
|
|
154
160
|
console.log('2. Go to Settings > Linked Devices');
|
|
155
161
|
console.log('3. Tap "Link a Device"');
|
|
156
162
|
console.log('4. Enter the code above\n');
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
await delay(2000);
|
|
160
177
|
}
|
|
161
|
-
}
|
|
178
|
+
}
|
|
162
179
|
}
|
|
163
180
|
|
|
164
181
|
this.sock.ev.on('connection.update', async (update) => {
|
|
@@ -206,6 +223,7 @@ export class Client extends EventEmitter {
|
|
|
206
223
|
async handleConnectionUpdate(update) {
|
|
207
224
|
const { connection, lastDisconnect, qr } = update;
|
|
208
225
|
|
|
226
|
+
// Handle QR code for QR login method
|
|
209
227
|
if (qr && this.config.loginMethod === 'qr') {
|
|
210
228
|
qrcode.generate(qr, { small: true });
|
|
211
229
|
}
|
|
@@ -226,15 +244,11 @@ export class Client extends EventEmitter {
|
|
|
226
244
|
this.user = this.sock.user;
|
|
227
245
|
this.emit('ready', this.user);
|
|
228
246
|
|
|
229
|
-
// Log success message
|
|
230
247
|
if (this.config.loginMethod === 'pairing') {
|
|
231
248
|
console.log('\nā Successfully connected via pairing code!\n');
|
|
232
249
|
}
|
|
233
250
|
} else if (connection === 'connecting') {
|
|
234
251
|
this.emit('connecting');
|
|
235
|
-
if (this.config.loginMethod === 'pairing') {
|
|
236
|
-
console.log('Waiting for pairing code confirmation...');
|
|
237
|
-
}
|
|
238
252
|
}
|
|
239
253
|
}
|
|
240
254
|
|