-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (109 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require("fs");
const qrcode = require("qrcode-terminal");
const { Client, LocalAuth } = require("whatsapp-web.js");
// Define session file path
const SESSION_FILE_PATH = './session.json';
// Read session from file
function readSession() {
try {
if (fs.existsSync(SESSION_FILE_PATH)) {
const data = fs.readFileSync(SESSION_FILE_PATH, 'utf-8');
return JSON.parse(data);
}
} catch (error) {
console.error(`Error reading session file: ${error.message}`);
}
return null;
}
// Write session to file
function writeSession(session) {
try {
fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session, null, 2));
console.log('Session saved successfully.');
} catch (error) {
console.error(`Error writing session file: ${error.message}`);
}
}
// Delete session file
function deleteSession() {
try {
if (fs.existsSync(SESSION_FILE_PATH)) {
fs.unlinkSync(SESSION_FILE_PATH);
console.log('Session file deleted.');
}
} catch (error) {
console.error(`Error deleting session file: ${error.message}`);
}
}
// Setting up the client with session
const sessionData = readSession();
const client = new Client({
puppeteer: {
args: ["--no-sandbox"],
},
session: sessionData
});
client.on("qr", (qr) => {
console.log('QR Code received, scan please.');
qrcode.generate(qr, { small: true });
});
client.on("authenticated", (session) => {
console.log('Authenticated successfully.');
writeSession(session);
});
client.on("auth_failure", (message) => {
console.error('Authentication failed:', message);
deleteSession();
});
client.on("ready", () => {
console.log("Client is ready!");
});
client.on("message", (message) => {
if (message.body === "!help") {
message.reply(
"commands:\n`!dice` roll a dice!\n`!bal` to get your balance",
);
}
if (message.body === "!dice") {
const result = getRandomInt(0, 1) ? "heads" : "tails";
message.reply(`you rolled a dice and you got: '${result}'`);
// Example of using writeJSON
if (result) {
const userData = readJSON("data.json") || {};
const userPhoneNumber = message.from;
userData[userPhoneNumber] = userData[userPhoneNumber] || { balance: 0 };
userData[userPhoneNumber].balance++; // Increment balance by 1
writeJSON("data.json", userData);
}
}
if (message.body === "!bal") {
const userData = readJSON("data.json") || {};
const userPhoneNumber = message.from;
const userBalance = userData[userPhoneNumber]?.balance || 0;
message.reply(`your balance is: ${userBalance}`);
}
});
client.initialize();
// Getting some needed functions
function getRandomInt(min, max) {
return Math.round(Math.random() * max) + min;
}
// Read JSON function
JSON.read = function (filePath) {
try {
const data = fs.readFileSync(filePath);
return JSON.parse(data);
} catch (error) {
console.error(`Error reading JSON file ${filePath}: ${error.message}`);
return null;
}
}
// Write JSON function
JSON.write = function (filePath, data) {
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
console.log(`JSON file ${filePath} updated successfully.`);
} catch (error) {
console.error(`Error writing JSON file ${filePath}: ${error.message}`);
}
}