-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Expand file tree
/
Copy pathautostatus.js
More file actions
261 lines (232 loc) · 9.46 KB
/
autostatus.js
File metadata and controls
261 lines (232 loc) · 9.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const fs = require('fs');
const path = require('path');
const isOwnerOrSudo = require('../lib/isOwner');
const channelInfo = {
contextInfo: {
forwardingScore: 1,
isForwarded: true,
forwardedNewsletterMessageInfo: {
newsletterJid: '120363161513685998@newsletter',
newsletterName: 'KnightBot MD',
serverMessageId: -1
}
}
};
// Path to store auto status configuration
const configPath = path.join(__dirname, '../data/autoStatus.json');
// Initialize config file if it doesn't exist
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, JSON.stringify({
enabled: false,
reactOn: false
}));
}
async function autoStatusCommand(sock, chatId, msg, args) {
try {
const senderId = msg.key.participant || msg.key.remoteJid;
const isOwner = await isOwnerOrSudo(senderId, sock, chatId);
if (!msg.key.fromMe && !isOwner) {
await sock.sendMessage(chatId, {
text: '❌ This command can only be used by the owner!',
...channelInfo
});
return;
}
// Read current config
let config = JSON.parse(fs.readFileSync(configPath));
// If no arguments, show current status
if (!args || args.length === 0) {
const status = config.enabled ? 'enabled' : 'disabled';
const reactStatus = config.reactOn ? 'enabled' : 'disabled';
await sock.sendMessage(chatId, {
text: `🔄 *Auto Status Settings*\n\n📱 *Auto Status View:* ${status}\n💫 *Status Reactions:* ${reactStatus}\n\n*Commands:*\n.autostatus on - Enable auto status view\n.autostatus off - Disable auto status view\n.autostatus react on - Enable status reactions\n.autostatus react off - Disable status reactions`,
...channelInfo
});
return;
}
// Handle on/off commands
const command = args[0].toLowerCase();
if (command === 'on') {
config.enabled = true;
fs.writeFileSync(configPath, JSON.stringify(config));
await sock.sendMessage(chatId, {
text: '✅ Auto status view has been enabled!\nBot will now automatically view all contact statuses.',
...channelInfo
});
} else if (command === 'off') {
config.enabled = false;
fs.writeFileSync(configPath, JSON.stringify(config));
await sock.sendMessage(chatId, {
text: '❌ Auto status view has been disabled!\nBot will no longer automatically view statuses.',
...channelInfo
});
} else if (command === 'react') {
// Handle react subcommand
if (!args[1]) {
await sock.sendMessage(chatId, {
text: '❌ Please specify on/off for reactions!\nUse: .autostatus react on/off',
...channelInfo
});
return;
}
const reactCommand = args[1].toLowerCase();
if (reactCommand === 'on') {
config.reactOn = true;
fs.writeFileSync(configPath, JSON.stringify(config));
await sock.sendMessage(chatId, {
text: '💫 Status reactions have been enabled!\nBot will now react to status updates.',
...channelInfo
});
} else if (reactCommand === 'off') {
config.reactOn = false;
fs.writeFileSync(configPath, JSON.stringify(config));
await sock.sendMessage(chatId, {
text: '❌ Status reactions have been disabled!\nBot will no longer react to status updates.',
...channelInfo
});
} else {
await sock.sendMessage(chatId, {
text: '❌ Invalid reaction command! Use: .autostatus react on/off',
...channelInfo
});
}
} else {
await sock.sendMessage(chatId, {
text: '❌ Invalid command! Use:\n.autostatus on/off - Enable/disable auto status view\n.autostatus react on/off - Enable/disable status reactions',
...channelInfo
});
}
} catch (error) {
console.error('Error in autostatus command:', error);
await sock.sendMessage(chatId, {
text: '❌ Error occurred while managing auto status!\n' + error.message,
...channelInfo
});
}
}
// Function to check if auto status is enabled
function isAutoStatusEnabled() {
try {
const config = JSON.parse(fs.readFileSync(configPath));
return config.enabled;
} catch (error) {
console.error('Error checking auto status config:', error);
return false;
}
}
// Function to check if status reactions are enabled
function isStatusReactionEnabled() {
try {
const config = JSON.parse(fs.readFileSync(configPath));
return config.reactOn;
} catch (error) {
console.error('Error checking status reaction config:', error);
return false;
}
}
// Function to react to status using proper method
async function reactToStatus(sock, statusKey) {
try {
if (!isStatusReactionEnabled()) {
return;
}
// Use the proper relayMessage method for status reactions
await sock.relayMessage(
'status@broadcast',
{
reactionMessage: {
key: {
remoteJid: 'status@broadcast',
id: statusKey.id,
participant: statusKey.participant || statusKey.remoteJid,
fromMe: false
},
text: '💖'
}
},
{
messageId: statusKey.id,
statusJidList: [statusKey.remoteJid, statusKey.participant || statusKey.remoteJid]
}
);
// Removed success log - only keep errors
} catch (error) {
console.error('❌ Error reacting to status:', error.message);
}
}
// Function to handle status updates
async function handleStatusUpdate(sock, status) {
try {
if (!isAutoStatusEnabled()) {
return;
}
// Add delay to prevent rate limiting
await new Promise(resolve => setTimeout(resolve, 1000));
// Handle status from messages.upsert
if (status.messages && status.messages.length > 0) {
const msg = status.messages[0];
if (msg.key && msg.key.remoteJid === 'status@broadcast') {
try {
await sock.readMessages([msg.key]);
const sender = msg.key.participant || msg.key.remoteJid;
// React to status if enabled
await reactToStatus(sock, msg.key);
// Removed success log - only keep errors
} catch (err) {
if (err.message?.includes('rate-overlimit')) {
console.log('⚠️ Rate limit hit, waiting before retrying...');
await new Promise(resolve => setTimeout(resolve, 2000));
await sock.readMessages([msg.key]);
} else {
throw err;
}
}
return;
}
}
// Handle direct status updates
if (status.key && status.key.remoteJid === 'status@broadcast') {
try {
await sock.readMessages([status.key]);
const sender = status.key.participant || status.key.remoteJid;
// React to status if enabled
await reactToStatus(sock, status.key);
// Removed success log - only keep errors
} catch (err) {
if (err.message?.includes('rate-overlimit')) {
console.log('⚠️ Rate limit hit, waiting before retrying...');
await new Promise(resolve => setTimeout(resolve, 2000));
await sock.readMessages([status.key]);
} else {
throw err;
}
}
return;
}
// Handle status in reactions
if (status.reaction && status.reaction.key.remoteJid === 'status@broadcast') {
try {
await sock.readMessages([status.reaction.key]);
const sender = status.reaction.key.participant || status.reaction.key.remoteJid;
// React to status if enabled
await reactToStatus(sock, status.reaction.key);
// Removed success log - only keep errors
} catch (err) {
if (err.message?.includes('rate-overlimit')) {
console.log('⚠️ Rate limit hit, waiting before retrying...');
await new Promise(resolve => setTimeout(resolve, 2000));
await sock.readMessages([status.reaction.key]);
} else {
throw err;
}
}
return;
}
} catch (error) {
console.error('❌ Error in auto status view:', error.message);
}
}
module.exports = {
autoStatusCommand,
handleStatusUpdate
};