@roidev/kachina-md 2.1.1 → 2.1.2
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 +49 -22
- package/package.json +1 -1
package/lib/client/Client.js
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
createSticker,
|
|
16
16
|
StickerTypes
|
|
17
17
|
} from '../helpers/sticker.js';
|
|
18
|
+
import chalk from 'chalk';
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* @typedef {Object} ClientOptions
|
|
@@ -130,35 +131,61 @@ export class Client extends EventEmitter {
|
|
|
130
131
|
throw new Error('Phone number is required for pairing method. Example: { phoneNumber: "628123456789" }');
|
|
131
132
|
}
|
|
132
133
|
|
|
133
|
-
// Format phone number (remove + and spaces)
|
|
134
134
|
const phoneNumber = this.config.phoneNumber.replace(/[^0-9]/g, '');
|
|
135
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
140
|
+
console.log('🔐 Initiating pairing code process...');
|
|
141
|
+
|
|
142
|
+
// Request pairing code with retry mechanism
|
|
143
|
+
const requestPairingWithRetry = async () => {
|
|
144
|
+
const maxRetries = 3;
|
|
145
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
146
|
+
|
|
147
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
148
|
+
try {
|
|
149
|
+
// Wait for socket to be ready
|
|
150
|
+
await delay(attempt === 0 ? 5000 : 2000);
|
|
151
|
+
|
|
152
|
+
const code = await this.sock.requestPairingCode(phoneNumber);
|
|
153
|
+
this.emit('pairing.code', code);
|
|
154
|
+
|
|
155
|
+
// Log to console with formatting
|
|
156
|
+
console.log('\n┌─────────────────────────────────────┐');
|
|
157
|
+
console.log('│ WhatsApp Pairing Code │');
|
|
158
|
+
console.log('├─────────────────────────────────────┤');
|
|
159
|
+
console.log(`│ Code: ${chalk.bgYellowBright.white(code)} │`);
|
|
160
|
+
console.log('└─────────────────────────────────────┘');
|
|
161
|
+
console.log('\nSteps to pair:');
|
|
162
|
+
console.log('1. Open WhatsApp on your phone');
|
|
163
|
+
console.log('2. Go to Settings > Linked Devices');
|
|
164
|
+
console.log('3. Tap "Link a Device"');
|
|
165
|
+
console.log('4. Enter the code above\n');
|
|
166
|
+
console.log('⚠️ Make sure the phone number matches your WhatsApp account');
|
|
167
|
+
|
|
168
|
+
return;
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const isLastAttempt = attempt === maxRetries - 1;
|
|
171
|
+
console.error(`Pairing attempt ${attempt + 1}/${maxRetries} failed:`, error.message);
|
|
172
|
+
|
|
173
|
+
this.emit('pairing.error', error);
|
|
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
|
+
}
|
|
181
|
+
}
|
|
160
182
|
}
|
|
161
|
-
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// Execute pairing request (non-blocking)
|
|
186
|
+
requestPairingWithRetry().catch(err => {
|
|
187
|
+
console.error('Failed to complete pairing process:', err.message);
|
|
188
|
+
});
|
|
162
189
|
}
|
|
163
190
|
|
|
164
191
|
this.sock.ev.on('connection.update', async (update) => {
|