-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (112 loc) · 3.07 KB
/
main.go
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
package main
import (
"log"
"strconv"
"strings"
"sync"
"time"
g "xabbo.b7c.io/goearth"
"xabbo.b7c.io/goearth/shockwave/in"
"xabbo.b7c.io/goearth/shockwave/out"
)
// Initialize the extension with metadata
var ext = g.NewExt(g.ExtInfo{
Title: "AFK-tracker",
Description: "Anti AFK. An extension that tracks how long you have been AFK.",
Author: "danii / v19",
Version: "0.5",
})
var (
afkTimer *time.Timer
lastActionTime time.Time
afkDuration = 5 * time.Minute // Set to 5 minutes
setupMutex sync.Mutex
afkActive bool
sendingAfkMessage bool // Flag to indicate if the script is sending an AFK message
)
// Entry point of the application
func main() {
ext.Initialized(onInitialized)
ext.Connected(onConnected)
ext.Disconnected(onDisconnected)
ext.Intercept(out.CHAT, out.WHISPER, out.DICE_OFF, out.THROW_DICE, out.MOVE).With(resetAfkTimer)
ext.Intercept(in.CHAT).With(ignoreAfkMessages)
ext.Run()
}
func onInitialized(e g.InitArgs) {
lastActionTime = time.Now()
startAfkTimer()
log.Println("Extension initialized")
}
func onConnected(e g.ConnectArgs) {
log.Printf("Game connected (%s)\n", e.Host)
}
func onDisconnected() {
log.Println("Game disconnected")
if afkTimer != nil {
afkTimer.Stop()
}
}
func resetAfkTimer(e *g.Intercept) {
setupMutex.Lock()
defer setupMutex.Unlock()
if sendingAfkMessage {
return
}
lastActionTime = time.Now()
if !afkActive {
startAfkTimer()
}
}
func startAfkTimer() {
if afkTimer != nil {
afkTimer.Stop()
}
afkTimer = time.AfterFunc(afkDuration, func() {
setupMutex.Lock()
defer setupMutex.Unlock()
afkActive = true
sendAfkMessages()
afkActive = false
startAfkTimer() // Continue the timer for the next interval
})
}
func sendAfkMessages() {
sendingAfkMessage = true
time.Sleep(2 * time.Second) // Pause before sending the message
elapsed := time.Since(lastActionTime)
message := formatAfkMessage(elapsed)
ext.Send(out.SHOUT, message)
time.Sleep(2 * time.Second) // Pause after sending the message
sendingAfkMessage = false
}
func formatAfkMessage(elapsed time.Duration) string {
totalMinutes := int(elapsed.Minutes())
days := totalMinutes / (60 * 24)
hours := (totalMinutes % (60 * 24)) / 60
minutes := totalMinutes % 60
var messageParts []string
if days > 0 {
messageParts = append(messageParts, strconv.Itoa(days)+" day"+pluralize(days))
}
if hours > 0 {
messageParts = append(messageParts, strconv.Itoa(hours)+" hour"+pluralize(hours))
}
if minutes > 0 || (days == 0 && hours == 0) {
messageParts = append(messageParts, strconv.Itoa(minutes)+" minute"+pluralize(minutes))
}
return "I have been AFK for " + strings.Join(messageParts, " ")
}
func pluralize(value int) string {
if value == 1 {
return ""
}
return "s"
}
func ignoreAfkMessages(e *g.Intercept) {
msg := e.Packet.ReadString()
if strings.Contains(msg, "I'm AFK") || strings.Contains(msg, "I have been AFK for") {
e.Block()
log.Println("Blocked AFK message")
}
}