forked from Sloboscc/Slobos-AFK-Aternos-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaveRejoin.js
More file actions
118 lines (95 loc) · 3.4 KB
/
leaveRejoin.js
File metadata and controls
118 lines (95 loc) · 3.4 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
function randomMs(minMs, maxMs) {
return Math.floor(Math.random() * (maxMs - minMs + 1)) + minMs
}
function setupLeaveRejoin(bot, createBot) {
// Timers
let leaveTimer = null
let jumpTimer = null
let jumpOffTimer = null
let reconnectTimer = null
// State
let stopped = false
let reconnectAttempts = 0
let lastLogAt = 0
function logThrottled(msg, minGapMs = 2000) {
const now = Date.now()
if (now - lastLogAt >= minGapMs) {
lastLogAt = now
console.log(msg)
}
}
function cleanup() {
stopped = true
if (leaveTimer) clearTimeout(leaveTimer)
if (jumpTimer) clearTimeout(jumpTimer)
if (jumpOffTimer) clearTimeout(jumpOffTimer)
if (reconnectTimer) clearTimeout(reconnectTimer)
leaveTimer = jumpTimer = jumpOffTimer = reconnectTimer = null
}
function scheduleNextJump() {
if (stopped || !bot.entity) return
bot.setControlState('jump', true)
jumpOffTimer = setTimeout(() => {
bot.setControlState('jump', false)
}, 300)
// random jump 20s -> 5m
const nextJump = randomMs(20000, 5 * 60 * 1000)
jumpTimer = setTimeout(scheduleNextJump, nextJump)
}
function scheduleReconnect(reason = 'end') {
if (stopped) return
// FAST RECONNECT: 2s -> 10s (User requested faster)
let delay = randomMs(2000, 10000)
// Slight backoff for repeated failures, but keep it snappy
reconnectAttempts++
if (reconnectAttempts > 3) {
delay += 5000 // Add 5s if it's failing a lot
}
// Cap at 30s max
delay = Math.min(delay, 15000)
logThrottled(`[AFK] Rejoin scheduled in ${Math.round(delay / 1000)}s (reason: ${reason}, attempt: ${reconnectAttempts})`)
reconnectTimer = setTimeout(() => {
if (stopped) return
try {
if (typeof createBot === 'function') createBot()
} catch (e) {
console.log('[AFK] createBot error:', e?.message || e)
scheduleReconnect('createBot-error')
}
}, delay)
}
bot.once('spawn', () => {
// reset attempt counter on successful connect
reconnectAttempts = 0
// clear any old timers
cleanup()
stopped = false
// Stay connected: 2 minutes -> 15 minutes (More realistic AFK behavior)
// Stay connected 1-5 minutes before a scheduled leave/rejoin cycle.
const stayTime = randomMs(60000, 300000)
logThrottled(`[AFK] Will leave in ${Math.round(stayTime / 1000)} seconds`)
scheduleNextJump()
leaveTimer = setTimeout(() => {
if (stopped) return
logThrottled('[AFK] Leaving server (timer)')
cleanup()
try {
bot.quit()
} catch (e) {
// ignore if already closed
}
}, stayTime)
})
// When the connection ends for ANY reason, just clean up our timers.
// Reconnection is handled by index.js — no duplicate reconnect here.
bot.on('end', () => {
cleanup()
})
bot.on('kicked', () => {
cleanup()
})
bot.on('error', () => {
cleanup()
})
}
module.exports = setupLeaveRejoin