Skip to content

Commit

Permalink
temp push
Browse files Browse the repository at this point in the history
  • Loading branch information
xditya committed Aug 30, 2022
1 parent 692c0d7 commit abf628f
Show file tree
Hide file tree
Showing 5 changed files with 554 additions and 13 deletions.
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("dotenv").config();

const CMD_HANDLER = process.env.CMD_HANDLER;
const DETA_PROJ_KEY = process.env.DETA_PROJ_KEY;

module.exports ={ CMD_HANDLER };
module.exports ={ CMD_HANDLER, DETA_PROJ_KEY };
59 changes: 59 additions & 0 deletions database/sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fs = require("fs");

const { Deta } = require("deta");
const { zip, COMPRESSION_LEVEL } = require("zip-a-folder");
const { extract } = require("extract-zip");

const { DETA_PROJ_KEY } = require("../config.js")

const deta = Deta(DETA_PROJ_KEY);
const whatsDB = deta.Drive("WhatsUB")

const checkSessionAndLoad = async () => {
if (!fs.existsSync("./WhatsUB")) {
try {
console.log("No session file found. Trying to load from database...")
const get_s = await whatsDB.get("whatsubsession");
if (get_s) {
const buffer = await get_s.arrayBuffer();
fs.writeFileSync("./WhatsUB.zip", Buffer.from(buffer));
await extract("./WhatsUB.zip", { dir: __dirname + "./WhatsUB" });
fs.unlinkSync("./WhatsUB.zip");
console.log("Session restored from database.")
return true;
}
else {
console.log("Session does not exist in database.")
return false;
}
} catch (err) {
console.log("Error loading session from db: " + err);
}
}
else {
console.log("Local session already exists!");
const get_s = await whatsDB.get("whatsubsession");
if (get_s == null) { await storeSession() }
return true;
}
}


const storeSession = async () => {
if(fs.existsSync("./WhatsUB")) {
try
{
console.log("Saving session file to database for future use...")
await zip("./WhatsUB", "./WhatsUB.zip", {compression: COMPRESSION_LEVEL.high});
await whatsDB.put("whatsubsession", {path: "./WhatsUB.zip"});
fs.unlink("./WhatsUB.zip");
console.log("Saved session to database.")
}
catch (err)
{
console.log("Error saving session to db: " + err);
}
}
}

module.exports = { storeSession,checkSessionAndLoad };
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ const fs = require("fs");
const qrcode = require("qrcode-terminal");
const { Client, LocalAuth } = require('whatsapp-web.js');
const config = require("./config.js");
const { checkSessionAndLoad } = require("./database/sessions.js");


// check and load old session
checkSessionAndLoad().then(value => { get_session = value });
new Promise((resolve) => {
setTimeout(resolve, 5000);
});

const client = new Client({
authStrategy: new LocalAuth({
Expand Down Expand Up @@ -30,7 +38,11 @@ client.on('qr', (qr) => {
console.log(qrcode.generate(qr, { small: true }))
});

client.on('ready', () => {
client.on('ready', async () => {
// console.log(get_session);
// if (await get_session == false) {
// await storeSession();
// }
console.log("\n\t\tWhatsUB has started!\n\t©️ @xditya < https://xditya.me >")
});

Expand All @@ -50,14 +62,5 @@ client.on("message_create", async (msg) => {
}
});


/*
#TODO
Save session (WhatsUB/*) to a database, maybe MongoDB
Find a better name?
Implement commands
*/
console.log("Initializing Client...")
client.initialize();
Loading

0 comments on commit abf628f

Please sign in to comment.