-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcheck-groups.js
More file actions
73 lines (62 loc) · 1.87 KB
/
check-groups.js
File metadata and controls
73 lines (62 loc) · 1.87 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
require('dotenv').config({ path: './.env' })
const {
db
} = require('./database')
const Telegram = require('telegraf/telegram')
const telegram = new Telegram(process.env.BOT_TOKEN)
const checkGroup = async (group) => {
const result = await telegram.sendChatAction(group.group_id, 'typing').catch(error => {
return error.response
})
if (result) {
const active = result.ok !== false
// Targeted update — a full group.save() would race with concurrent
// $inc writes on quoteCounter from the live bot.
await db.Group.updateOne(
{ _id: group._id },
{ $set: { 'available.active': active, 'available.check': true } }
)
}
return { group_id: group.group_id, result }
}
let checkRunning = false
const MAX_ITERATIONS = 1000 // Prevent infinite loops
let iterationCount = 0
const checkGroups = async () => {
if (checkRunning) return
checkRunning = true
try {
const groups = await db.Group.find({ 'available.check': { $ne: true } }).limit(100)
if (groups.length === 0) {
console.log('All groups checked, exiting...')
checkRunning = false
process.exit(0)
return
}
iterationCount++
if (iterationCount >= MAX_ITERATIONS) {
console.log('Max iterations reached, exiting...')
checkRunning = false
process.exit(0)
return
}
const checkArray = groups.map(checkGroup)
console.log(await Promise.all(checkArray))
checkRunning = false
// Add delay to prevent overwhelming the system
// Use early return to prevent infinite recursion
if (iterationCount < MAX_ITERATIONS) {
setTimeout(() => {
checkGroups()
}, 1000)
} else {
console.log('Max iterations reached in timeout, exiting...')
process.exit(0)
}
} catch (error) {
console.error('Error in checkGroups:', error)
checkRunning = false
process.exit(1)
}
}
checkGroups()