-
Notifications
You must be signed in to change notification settings - Fork 1
add: update when counter with 10 minutes cooldown globally #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| package cmd | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/MetrolistGroup/metrobot/db" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| const releaseCooldown = 10 * time.Minute | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| var releaseDatePattern = regexp.MustCompile(`(?i)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `(when\b[^.]{0,80}\b(?:update|release|version|app)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\beta\b[^.]{0,80}\b(?:update|release|version)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bstill\s+(?:no|waiting)\b[^.]{0,80}\b(?:update|release|version|news)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\b(?:waiting|longing|hoping)\s+for\b[^.]{0,80}\b(?:update|release|version)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bany\s+(?:update|release|version|news|eta)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bwhere\s+(?:is|are)\b[^.]{0,80}\b(?:update|release|version|app)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\brelease\s+date\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bis\s+there\s+a\s+(?:new\s+)?(?:update|release|version)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bwhat'?s\s+the\s+(?:eta|status|release\s+date)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\b(?:update|release|version)\s+when\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bwhen\s+will\s+you\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bhow\s+(?:long|much)\s+(?:until|before|till)\b[^.]{0,80}\b(?:update|release|version)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bis\s+it\s+(?:out|released|available)\b[^.]{0,20}\b(?:yet|already)\b)` + | ||||||||||||||||||||||||||||||||||||||||||||||||||
| `|(\bdid\s+it\s+(?:release|come\s+out)\b)`) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| type ReleaseCounterHandler struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| DB *db.DB | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mu sync.Mutex | ||||||||||||||||||||||||||||||||||||||||||||||||||
| lastTriggeredAt time.Time | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (h *ReleaseCounterHandler) Increment(isTelegram bool) (string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| h.mu.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if time.Since(h.lastTriggeredAt) < releaseCooldown { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| h.mu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| h.mu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Race condition allows cooldown bypass. The mutex is released between the cooldown check (line 38) and the DB increment (line 44). Two concurrent message handlers can both pass the cooldown check before either sets 🔒 Proposed fix: hold the lock for the entire operation func (h *ReleaseCounterHandler) Increment(isTelegram bool) (string, error) {
h.mu.Lock()
+ defer h.mu.Unlock()
+
if time.Since(h.lastTriggeredAt) < releaseCooldown {
- h.mu.Unlock()
return "", nil
}
- h.mu.Unlock()
count, err := h.DB.IncrementReleaseCounter()
if err != nil {
return "", fmt.Errorf("incrementing release counter: %w", err)
}
- h.mu.Lock()
h.lastTriggeredAt = time.Now()
- h.mu.Unlock()
return formatCounter(count, isTelegram), nil
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| count, err := h.DB.IncrementReleaseCounter() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", fmt.Errorf("incrementing release counter: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| h.mu.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| h.lastTriggeredAt = time.Now() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| h.mu.Unlock() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return formatCounter(count, isTelegram), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (h *ReleaseCounterHandler) Get(isTelegram bool) (string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| count, err := h.DB.GetReleaseCounter() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return "", fmt.Errorf("getting release counter: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return formatCounter(count, isTelegram), nil | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func formatCounter(count int, isTelegram bool) string { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msg := fmt.Sprintf("Release date question counter: %d", count) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if isTelegram { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return msg | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return msg | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func MatchReleaseQuestion(content string) bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return releaseDatePattern.MatchString(content) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/MetrolistGroup/metrobot/cmd" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/MetrolistGroup/metrobot/util" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/bwmarrin/discordgo" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "go.uber.org/zap" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -87,6 +88,8 @@ func (b *Bot) onInteractionCreate(s *discordgo.Session, i *discordgo.Interaction | |||||||||||||||||||||||||||||||||||||||||||||||||
| b.handlePurge(s, i, opts, callerID) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case "refreshstarboard": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.handleRefreshStarboard(s, i, callerID) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case "counter": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.handleCounter(s, i) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -110,6 +113,19 @@ func (b *Bot) onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) | |||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if cmd.MatchReleaseQuestion(content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| text, err := b.ReleaseCounter.Increment(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.Logger.Error("release counter error", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if text == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sendReply(s, m.ChannelID, m.ID, text, false, b.Logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+116
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Release question check can intercept moderation commands. The 🐛 Proposed fix: skip `!`-prefixed messages- if cmd.MatchReleaseQuestion(content) {
+ if !strings.HasPrefix(content, "!") && cmd.MatchReleaseQuestion(content) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| matches := chatModPattern.FindStringSubmatch(content) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if matches == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -731,6 +747,16 @@ func (b *Bot) handleRefreshStarboard(s *discordgo.Session, i *discordgo.Interact | |||||||||||||||||||||||||||||||||||||||||||||||||
| editDeferredResponse(s, i, "✅ Starboard refreshed successfully.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (b *Bot) handleCounter(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| text, err := b.ReleaseCounter.Get(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.Logger.Error("counter error", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| respondEphemeral(s, i, "Error fetching counter.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| respondPublic(s, i, text) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // --- Helpers --- | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func optionMap(opts []*discordgo.ApplicationCommandInteractionDataOption) map[string]*discordgo.ApplicationCommandInteractionDataOption { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/MetrolistGroup/metrobot/cmd" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/MetrolistGroup/metrobot/util" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "go.uber.org/zap" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,6 +41,19 @@ func (b *Bot) handleMessage(msg *tgbotapi.Message) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if cmd.MatchReleaseQuestion(content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| text, err := b.ReleaseCounter.Increment(true) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.Logger.Error("release counter error", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if text == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sendPublicReply(b.API, msg.Chat.ID, msg.MessageID, text, "", false, b.Logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Release question check can intercept moderation commands (same as Discord). The 🐛 Proposed fix: skip `!`-prefixed messages- if cmd.MatchReleaseQuestion(content) {
+ if !strings.HasPrefix(content, "!") && cmd.MatchReleaseQuestion(content) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| matches := chatModPattern.FindStringSubmatch(content) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if matches == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // No action triggered, so don't log this message | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -168,6 +182,8 @@ func (b *Bot) handleCommand(msg *tgbotapi.Message, callerID string) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| b.tgHandleRemoveAdmin(msg, args, callerID) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case "ping": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.tgHandlePing(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| case "counter": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.tgHandleCounter(msg) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -738,6 +754,16 @@ func (b *Bot) tgHandlePing(msg *tgbotapi.Message) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| sendPublicReply(b.API, msg.Chat.ID, msg.MessageID, text, "", false, b.Logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func (b *Bot) tgHandleCounter(msg *tgbotapi.Message) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| text, err := b.ReleaseCounter.Get(true) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b.Logger.Error("counter error", zap.Error(err)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sendPublicReply(b.API, msg.Chat.ID, msg.MessageID, "Error fetching counter.", "", false, b.Logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sendPublicReply(b.API, msg.Chat.ID, msg.MessageID, text, "", false, b.Logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| func extractTelegramUserID(msg *tgbotapi.Message, mention string) string { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if msg.ReplyToMessage != nil && msg.ReplyToMessage.From != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return strconv.FormatInt(msg.ReplyToMessage.From.ID, 10) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Regex alternative
\bwhen\s+will\s+you\bis too broad.This pattern matches any "when will you" phrase regardless of context — e.g., "when will you be back?" or "when will you respond?" — producing false positives that inflate the counter. All other alternatives require a release-related keyword; this one does not.
🐛 Proposed fix: require a release-related keyword
📝 Committable suggestion
🤖 Prompt for AI Agents