-
Notifications
You must be signed in to change notification settings - Fork 171
/
custom.js
134 lines (120 loc) · 3.84 KB
/
custom.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
128
129
130
131
132
133
134
const logger = require('./utils/log');
const cron = require('node-cron');
module.exports = async ({ api }) => {
const minInterval = 5;
let lastMessageTime = 0;
let messagedThreads = new Set();
const config = {
autoRestart: {
status: false,
time: 40,
note: 'To avoid problems, enable periodic bot restarts',
},
acceptPending: {
status: false,
time: 30,
note: 'Approve waiting messages after a certain time',
},
};
function autoRestart(config) {
if (config.status) {
cron.schedule(`*/${config.time} * * * *`, () => {
logger.log('Start rebooting the system!', 'Auto Restart');
process.exit(1);
});
}
}
function acceptPending(config) {
if (config.status) {
cron.schedule(`*/${config.time} * * * *`, async () => {
const list = [
...(await api.getThreadList(1, null, ['PENDING'])),
...(await api.getThreadList(1, null, ['OTHER'])),
];
if (list[0]) {
api.sendMessage('You have been approved for the queue. (This is an automated message)', list[0].threadID);
}
});
}
}
autoRestart(config.autoRestart);
acceptPending(config.acceptPending);
// AUTOGREET EVERY 10 MINUTES
cron.schedule('*/10 * * * *', () => {
const currentTime = Date.now();
if (currentTime - lastMessageTime < minInterval) {
console.log("Skipping message due to rate limit");
return;
}
api.getThreadList(25, null, ['INBOX'], async (err, data) => {
if (err) return console.error("Error [Thread List Cron]: " + err);
let i = 0;
let j = 0;
async function message(thread) {
try {
api.sendMessage({
body: `⟩ Thank you for using BotPack!\n\n⟩ Fork Here: https://replit.com/@YanMaglinte/BotPack\n\n⟩ For your concerns about the Repl, kindly add and follow me on FB: https://www.facebook.com/yandeva.me?mibextid=ZbWKwL`
}, thread.threadID, (err) => {
if (err) return;
messagedThreads.add(thread.threadID);
});
} catch (error) {
console.error("Error sending a message:", error);
}
}
while (j < 20 && i < data.length) {
if (data[i].isGroup && data[i].name != data[i].threadID && !messagedThreads.has(data[i].threadID)) {
await message(data[i]);
j++;
const CuD = data[i].threadID;
setTimeout(() => {
messagedThreads.delete(CuD);
}, 1000);
}
i++;
}
});
}, {
scheduled: false, // Set this to false to turn it off
timezone: "Asia/Manila"
});
// AUTOGREET EVERY 30 MINUTES
cron.schedule('*/30 * * * *', () => {
const currentTime = Date.now();
if (currentTime - lastMessageTime < minInterval) {
console.log("Skipping message due to rate limit");
return;
}
api.getThreadList(25, null, ['INBOX'], async (err, data) => {
if (err) return console.error("Error [Thread List Cron]: " + err);
let i = 0;
let j = 0;
async function message(thread) {
try {
api.sendMessage({
body: `Hey There! How are you? ヾ(^-^)ノ`
}, thread.threadID, (err) => {
if (err) return;
messagedThreads.add(thread.threadID);
});
} catch (error) {
console.error("Error sending a message:", error);
}
}
while (j < 20 && i < data.length) {
if (data[i].isGroup && data[i].name != data[i].threadID && !messagedThreads.has(data[i].threadID)) {
await message(data[i]);
j++;
const CuD = data[i].threadID;
setTimeout(() => {
messagedThreads.delete(CuD);
}, 1000);
}
i++;
}
});
}, {
scheduled: false, // Set this to false to turn it off
timezone: "Asia/Manila"
});
};