(sock: any, message: any, args: any, context: BotContext)
| 12 | usage: '.clearsession', |
| 13 | |
| 14 | async handler(sock: any, message: any, args: any, context: BotContext) { |
| 15 | const chatId = context.chatId || message.key.remoteJid; |
| 16 | try { |
| 17 | const senderId = message.key.participant || message.key.remoteJid; |
| 18 | const isOwner = await isOwnerOrSudo(senderId, sock, chatId); |
| 19 | |
| 20 | if (!message.key.fromMe && !isOwner) { |
| 21 | return await sock.sendMessage(chatId, { text: '*This command can only be used by the owner!*', ...channelInfo }); |
| 22 | } |
| 23 | |
| 24 | const sessionDir = path.join(process.cwd(), 'session'); |
| 25 | if (!fs.existsSync(sessionDir)) { |
| 26 | return await sock.sendMessage(chatId, { text: '*Session directory not found!*', ...channelInfo }); |
| 27 | } |
| 28 | |
| 29 | let filesCleared = 0; |
| 30 | let errors = 0; |
| 31 | const errorDetails = []; |
| 32 | |
| 33 | await sock.sendMessage(chatId, { text: '🔍 Optimizing session files for better performance...', ...channelInfo }); |
| 34 | |
| 35 | const files = fs.readdirSync(sessionDir); |
| 36 | let appStateSyncCount = 0; |
| 37 | let preKeyCount = 0; |
| 38 | |
| 39 | for (const file of files) { |
| 40 | if (file.startsWith('app-state-sync-')) appStateSyncCount++; |
| 41 | if (file.startsWith('pre-key-')) preKeyCount++; |
| 42 | } |
| 43 | |
| 44 | for (const file of files) { |
| 45 | if (file === 'creds.json') continue; |
| 46 | if (file.startsWith('app-state-sync-key-')) continue; |
| 47 | try { |
| 48 | fs.unlinkSync(path.join(sessionDir, file)); |
| 49 | filesCleared++; |
| 50 | } catch(err: any) { |
| 51 | errors++; |
| 52 | errorDetails.push(`Failed to delete ${file}: ${err.message}`); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const msgText = `✅ Session files cleared successfully!\n\n` + |
| 57 | `📊 Statistics:\n` + |
| 58 | `• Total files cleared: ${filesCleared}\n` + |
| 59 | `• App state sync files: ${appStateSyncCount}\n` + |
| 60 | `• Pre-key files: ${preKeyCount}\n` + |
| 61 | (errors > 0 ? `\n⚠️ Errors encountered: ${errors}\n${errorDetails.join('\n')}` : ''); |
| 62 | |
| 63 | await sock.sendMessage(chatId, { text: msgText, ...channelInfo }); |
| 64 | |
| 65 | } catch { |
| 66 | await sock.sendMessage(chatId, { text: '❌ Failed to clear session files!', ...channelInfo }); |
| 67 | } |
| 68 | } |
| 69 | }; |
nothing calls this directly
no test coverage detected