Skip to content

Commit 6820d23

Browse files
Fix: Delete join service message for auto-banned users (#22)
When a user joins, the bot might receive a `ChatMemberUpdated` event before the `NewChatMembers` service message. If the auto-ban is triggered by the `ChatMemberUpdated` event, the service message ID is not yet known, leaving the "X joined the group" message visible. This change introduces a structured `JoinProcessedCache` that tracks the `JoinMsgID` and `IsBanned` status. If a join message ID arrives for an already banned user (within 10 seconds of the initial join detection), it is now retroactively deleted. Changes: - Replaced `JoinProcessedCache map[int64]time.Time` with `map[int64]*JoinProcessedEntry`. - Added `JoinProcessedEntry` struct in `telegram/telegram.go`. - Updated `processJoin` in `telegram/autoban.go` to handle late message IDs and mark banned users. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: birabittoh <26506860+birabittoh@users.noreply.github.com>
1 parent 43d716a commit 6820d23

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

telegram/autoban.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,24 @@ func processJoin(escarbot *EscarBot, chatID int64, user tgbotapi.User, joinMsgID
5959
escarbot.JoinCacheMutex.Lock()
6060
// Cleanup old entries (older than 1 minute) periodically
6161
// We do it here for simplicity
62-
for id, t := range escarbot.JoinProcessedCache {
63-
if time.Since(t) > 1*time.Minute {
62+
for id, entry := range escarbot.JoinProcessedCache {
63+
if time.Since(entry.Time) > 1*time.Minute {
6464
delete(escarbot.JoinProcessedCache, id)
6565
}
6666
}
6767

6868
lastProcessed, exists := escarbot.JoinProcessedCache[user.ID]
6969
// If processed in the last 10 seconds, it's a duplicate
70-
if exists && time.Since(lastProcessed) < 10*time.Second {
70+
if exists && time.Since(lastProcessed.Time) < 10*time.Second {
71+
// If we now have a JoinMsgID but didn't before
72+
if joinMsgID != 0 && lastProcessed.JoinMsgID == 0 {
73+
lastProcessed.JoinMsgID = joinMsgID
74+
if lastProcessed.IsBanned {
75+
// User was already banned, delete this new join message
76+
deleteMessages(escarbot, chatID, joinMsgID)
77+
log.Printf("Deleted late join message %d for banned user %d", joinMsgID, user.ID)
78+
}
79+
}
7180
escarbot.JoinCacheMutex.Unlock()
7281

7382
// Even if it's a duplicate, we might want to update the joinMsgID for captcha
@@ -81,7 +90,13 @@ func processJoin(escarbot *EscarBot, chatID int64, user tgbotapi.User, joinMsgID
8190
escarbot.CaptchaMutex.Unlock()
8291
return
8392
}
84-
escarbot.JoinProcessedCache[user.ID] = time.Now()
93+
94+
entry := &JoinProcessedEntry{
95+
Time: time.Now(),
96+
JoinMsgID: joinMsgID,
97+
IsBanned: false,
98+
}
99+
escarbot.JoinProcessedCache[user.ID] = entry
85100
escarbot.JoinCacheMutex.Unlock()
86101

87102
escarbot.StateMutex.RLock()
@@ -97,6 +112,11 @@ func processJoin(escarbot *EscarBot, chatID int64, user tgbotapi.User, joinMsgID
97112
// Ban user and cleanup join message
98113
banAndCleanup(escarbot, chatID, user, joinMsgID)
99114

115+
// Mark as banned in join cache to handle potential late service messages
116+
escarbot.JoinCacheMutex.Lock()
117+
entry.IsBanned = true
118+
escarbot.JoinCacheMutex.Unlock()
119+
100120
return
101121
}
102122

telegram/telegram.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ type EscarBot struct {
4242
EnabledReplacers map[string]bool
4343
PendingCaptchas map[int64]*PendingCaptcha
4444
CaptchaMutex sync.RWMutex
45-
JoinProcessedCache map[int64]time.Time
45+
JoinProcessedCache map[int64]*JoinProcessedEntry
4646
JoinCacheMutex sync.Mutex
4747
}
4848

49+
// JoinProcessedEntry represents a join event that was already processed
50+
type JoinProcessedEntry struct {
51+
Time time.Time
52+
JoinMsgID int
53+
IsBanned bool
54+
}
55+
4956
// MessageHistory represents a previous version of a message
5057
type MessageHistory struct {
5158
Text string `json:"text"`
@@ -231,7 +238,7 @@ func NewBot(botToken string, channelId string, groupId string, adminId, logChann
231238
CaptchaText: os.Getenv("CAPTCHA_TEXT"),
232239
EnabledReplacers: enabledReplacers,
233240
PendingCaptchas: make(map[int64]*PendingCaptcha),
234-
JoinProcessedCache: make(map[int64]time.Time),
241+
JoinProcessedCache: make(map[int64]*JoinProcessedEntry),
235242
}
236243

237244
availableReactionsMap[groupIdInt] = getAvailableReactions(escarbot, groupIdInt)

0 commit comments

Comments
 (0)