-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
126 lines (123 loc) · 4.97 KB
/
main.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
//init stuff
const { Client, ChatTypes } = require('whatsapp-web.js');
const client = new Client({ puppeteer: { headless: false }, clientId: 'example' });
const fsPromises = require('fs').promises;
var replace = require("replace");
client.initialize();
client.on('qr', (qr) => {
console.log('QR RECEIVED', qr);
});
client.on('authenticated', () => {
console.log('AUTHENTICATED');
});
client.on('auth_failure', msg => {
console.error('AUTHENTICATION FAILURE', msg);
});
client.on('ready', () => {
console.log('READY');
});
client.on('disconnected', (reason) => {
console.log('Client was logged out', reason);
});
///
//slightly edited but pretty much a copy paste function from https://www.geeksforgeeks.org/node-js-filehandle-readfile-method/
async function readtxtfrom(filename) {
let filehandle = null;
try {
filehandle =
await fsPromises.open(filename, 'r+');
var txtdata =
await filehandle.readFile("utf8");
await filehandle.close()//closes the file. idk why this wasn't included in there example
return txtdata;//returns the read data
} catch (e) {
console.log("Error", e);
}
}
//another variation of that but it returns an array (each line(phone number) is an element)
async function readtxtlines(filename) {
let filehandle = null;
try {
filehandle =
await fsPromises.open(filename, 'r+');
var txtdata =
await filehandle.readFile("utf8");
await filehandle.close()
lines = txtdata.split(/\r\n|\n|\r/);
return lines;
} catch (e) {
console.log("Error", e);
}
}
//another variation that writes new lines to a txt file (currently only used for whitelisting)
async function writetxtline(filename,towrite) {
let filehandle = null;
try {
filehandle =
await fsPromises.open(filename, 'a');
await filehandle.appendFile(towrite)
await filehandle.appendFile("\n")
await filehandle.close()
} catch (e) {
console.log("Error", e);
}
}
//
async function removetxtline(filename,toremove){
await replace({regex: toremove + "\n" , replacement: "", paths: ['./',filename],recursive: false,silent: true})
};
client.on('message_create', (msg) => {
msg.getChat().then(function(inGroup) {
if (inGroup.isGroup){//used to stop the bot from sending response messages to group chats
console.log('group msg -> ignoring');
}else if(msg.type == "revoked"){
console.log('someone deleted a message -> ignoring');
}else if (msg.fromMe){
if (msg.body =="!whitelist"){
readtxtlines("whitelist.txt").then(function(lines){
if (lines.includes(msg.to)){
console.log("number",msg.to,"is already on whitelist")
msg.reply("already on the whitelist")
}else{
writetxtline("whitelist.txt",msg.to)
console.log("added",msg.to,"to the whitelist")
msg.reply("added to the whitelist")
}
})
}else if(msg.body == "!rmwhitelist"){
readtxtlines("whitelist.txt").then(function(lines){
if (lines.includes(msg.to)){
removetxtline("whitelist.txt", msg.to)
console.log("removed ",msg.to,"from the whitelist")
msg.reply("removed from the whitelist")
}else{
console.log("number",msg.to,"is not on the whitelist")
msg.reply("not on the whitelist")
}
})
}
}else if (!inGroup.isGroup){
var date = new Date();
//you can use the check below to set a time in which the bot stops sending response messages
//anything above == 23 will make the bot always responed with the message
//currently set to between 12AM and 1AM aka. 0(or 24, idk man i don't use 24 hour format) to 1
if(date.getHours() == 0 ){
console.log("private msg inside active hours -> ignoring")
}else if(msg.from == "status@broadcast") { //this is used so that the bot doesn't try to respond to status updates
console.log("status update -> ignoring")
}else{
readtxtlines("whitelist.txt").then(function(lines){
if (lines.includes(msg.from)){
console.log("private msg from a whitlisted number -> ignoring")
}else{
console.log("private msg outside active hours -> sending automsg to", msg.from, "time =",Date())
readtxtfrom("automsg.txt").then(function (txtdata){
client.sendMessage(msg.from, txtdata);
})
}
})
}
}
},
function(error) {console.log(error)});
},);