diff --git a/src/bot.ts b/src/bot.ts index a95843c..d14b830 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -3,6 +3,7 @@ */ import pkg from "whatsapp-web.js"; import qrcode from "qrcode-terminal"; +import chalk from "chalk"; import { handleMessages } from "./services/messageHandler.service.js"; import { getBanner } from "./utils/banner.js"; import { getStorageDir } from "./storage/configStore.js"; @@ -11,6 +12,33 @@ type ClientType = import("whatsapp-web.js").Client; const { Client, LocalAuth } = pkg; +// ========================================== +// GLOBAL SAFETY NET FOR SILENT BACKGROUND CRASHES +// ========================================== +process.on("unhandledRejection", (reason: unknown) => { + console.error('\n' + chalk.red.bold('āœ• Critical Application Error (Unhandled Rejection)!')); + + if (reason instanceof Error) { + console.error(chalk.dim(`Details: ${reason.message}`)); + } else { + console.error(chalk.dim(`Details: ${String(reason)}`)); + } + + console.error(chalk.yellow('\nPossible Root Causes:')); + console.error(' • Network is completely offline (Wi-Fi turned off).'); + console.error(' • Puppeteer hit a silent timeout waiting for WhatsApp Web.'); + + console.error(chalk.cyan('\nšŸ‘‰ Action: Check your connection and restart with: ') + chalk.green('npm run dev\n')); + process.exit(1); +}); + +process.on("uncaughtException", (error: Error) => { + console.error('\n' + chalk.red.bold('āœ• Unexpected System Exception!')); + console.error(chalk.dim(`Error: ${error.message}`)); + process.exit(1); +}); +// ========================================== + export class WhatsAppBot { private client: ClientType; private username: string; @@ -34,7 +62,7 @@ export class WhatsAppBot { private initializeEvents() { this.client.on("qr", (qr) => { - console.log("Scan QR to login:"); + console.log(chalk.cyan("\nšŸ“± Action Required: Scan the QR code to log in:")); qrcode.generate(qr, { small: true }); }); @@ -42,35 +70,65 @@ export class WhatsAppBot { try { console.clear(); await getBanner(this.agentName, this.username); + console.log(chalk.green.bold("\nšŸš€ chat-buddy is live and monitoring messages!")); } catch (err) { - console.log(err); + console.error(chalk.red("Failed to load welcome banner:"), err); } }); this.client.on("auth_failure", (msg) => { - console.log("Auth failed:", msg); + console.error('\n' + chalk.red.bold('āœ• Authentication Failed!')); + console.error(chalk.yellow(`Details: ${msg}`)); + console.error(chalk.cyan('šŸ‘‰ Solution: Your session might be invalid. Try clearing your local cache or storage directory, then restart.')); + process.exit(1); }); this.client.on("disconnected", (reason) => { - console.log("Disconnected:", reason); - console.log("Reconnecting..."); - this.client.initialize(); + console.warn('\n' + chalk.yellow.bold('āš ļø WhatsApp Connection Lost!')); + console.error(chalk.dim(`Reason: ${reason}`)); + + console.error(chalk.cyan('\nšŸ› ļø How to recover:')); + console.error(` 1. Verify your computer's internet connection.`); + console.error(` 2. Rerun the command to re-authenticate: ${chalk.green('npm run dev')}\n`); + + process.exit(0); }); this.client.on("message", async (message) => { try { await handleMessages(message, this.username, this.agentName); } catch (err) { - console.log("Message error:", err); + console.error(chalk.red("\nāŒ Message processing encountered an error:"), err); } }); } - public start() { - this.client.initialize().catch((err) => { - console.log(err); - }); + /** + * Starts the WhatsApp client and captures initialization failures + */ + public async start(): Promise { + console.log(chalk.blue("ā³ Initializing WhatsApp client connection...")); + + try { + await this.client.initialize(); + } catch (err: unknown) { + console.error('\n' + chalk.red.bold('āœ• Client Initialization Failed!')); + + if (err instanceof Error) { + console.error(chalk.dim(`Error Message: ${err.message}`)); + } else { + console.error(chalk.dim(`Error Details: ${String(err)}`)); + } + + console.error(chalk.yellow('\nPossible Root Causes:')); + console.error(' • Network is completely unreachable (Wi-Fi turned off).'); + console.error(' • Chromium/Puppeteer timed out trying to reach the WhatsApp server.'); + + console.error(chalk.cyan('\nšŸ‘‰ Action: Please check your internet connection and rerun the command.\n')); + + process.exit(1); + } } } -export const botRebootTime = Date.now(); +export const botRebootTime = Date.now(); \ No newline at end of file