const DB = require("./db"); const cmBot = require("./cmBot.cjs"); class setSafePassBot { constructor() { this.db = new DB(); this.bot = new cmBot(); } async getPasswordFromAcc(f_username) { const [result] = await this.db.query( "SELECT password FROM acc WHERE username = ?", [f_username] ); if (!result) { throw new Error("Account not found"); } return result.password; } async getUsernameFromWALink(wa_link) { try { const url = new URL(wa_link); if (url.protocol !== "https:" || !url.hostname.includes("whatsapp.com")) { throw new Error("Invalid WhatsApp link"); } } catch (error) { throw new Error("Invalid URL format"); } await this.bot.loadPageAndWaitForElement(this.page, wa_link, "#main_block"); const text = await this.page.$eval( "#main_block h3", (el) => el.textContent ); return text.replace(/^\*+/g, "").replace(/\s/g, "").split("/"); } async getUserData(wa_link) { const [t_username, f_username] = await this.getUsernameFromWALink(wa_link); const f_password = await this.getPasswordFromAcc(f_username); const t_password = "Sky533535"; return [f_username, f_password, t_username, t_password]; } async insertUserToDB(f_username, f_password, t_username, t_password) { await this.db.query( "INSERT INTO user (f_username, f_password, t_username, t_password) VALUES (?, ?, ?, ?)", [f_username, f_password, t_username, t_password] ); } async updateAccDB(f_username) { await this.db.query("UPDATE acc SET status = ? WHERE username = ?", [ "done", f_username, ]); } async isFUsernameExist(f_username) { const [result] = await this.db.query( "SELECT f_username FROM user WHERE f_username = ?", [f_username] ); return !!result; } async setSafePass(wa_link) { try { this.page = await this.bot.createNewPage(); const [f_username, f_password, t_username, t_password] = await this.getUserData(wa_link); if (await this.isFUsernameExist(f_username)) { throw new Error("account already exists"); } await this.bot.login(this.page, f_username, f_password); await this.bot.setSafePass(this.page); await this.insertUserToDB(f_username, f_password, t_username, t_password); await this.updateAccDB(f_username); this.bot.close(this.page); return [f_username, t_username]; } catch (error) { console.error(error); this.bot.close(this.page); throw error; } } } if (require.main === module) { (async () => { const bot = new setSafePassBot(); // console.log(await bot.getPasswordFromAcc("13c3599")); await bot.setSafePass("https://chat.whatsapp.com/Dh6WEaXzt4W6jVIwTZevNC"); })(); } module.exports = setSafePassBot;