Add rate limiting, API response caching, and fix progress bar panic

- Rate limit !weather (5/day), !concerts (10/day), fix !translate default to 20/day
- Add generic api_cache table with CacheGet/CacheSet helpers
- Cache !weather (1h), !wiki, !define, !tv (24h), !upcoming movies, !anime season/upcoming (24h)
- Fix ProgressBar panic on negative Repeat count when XP exceeds level threshold
- All rate limits configurable via RATELIMIT_* env vars

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
prosolis
2026-03-08 19:07:48 -07:00
parent ddb196aaad
commit 2c7c15b282
8 changed files with 145 additions and 21 deletions

View File

@@ -290,6 +290,12 @@ func (p *MoviesPlugin) handleTV(ctx MessageContext) error {
return p.SendReply(ctx.RoomID, ctx.EventID, "TV lookups are not configured (missing API key).")
}
// Check 24h cache
cacheKey := "tv:" + strings.ToLower(query)
if cached := db.CacheGet(cacheKey, 86400); cached != "" {
return p.SendReply(ctx.RoomID, ctx.EventID, cached)
}
encoded := url.QueryEscape(query)
apiURL := fmt.Sprintf("https://api.themoviedb.org/3/search/tv?api_key=%s&query=%s&page=1",
p.apiKey, encoded)
@@ -311,7 +317,9 @@ func (p *MoviesPlugin) handleTV(ctx MessageContext) error {
return p.SendReply(ctx.RoomID, ctx.EventID, "Failed to fetch TV show details.")
}
return p.SendReply(ctx.RoomID, ctx.EventID, p.formatTV(detail))
msg := p.formatTV(detail)
db.CacheSet(cacheKey, msg)
return p.SendReply(ctx.RoomID, ctx.EventID, msg)
}
func (p *MoviesPlugin) fetchTVDetail(tvID int) (*tmdbTVDetail, error) {
@@ -474,6 +482,12 @@ func (p *MoviesPlugin) handleUpcoming(ctx MessageContext) error {
return p.SendReply(ctx.RoomID, ctx.EventID, "Movie lookups are not configured (missing API key).")
}
// Check 6h cache
cacheKey := "upcoming_movies"
if cached := db.CacheGet(cacheKey, 86400); cached != "" {
return p.SendReply(ctx.RoomID, ctx.EventID, cached)
}
apiURL := fmt.Sprintf("https://api.themoviedb.org/3/movie/upcoming?api_key=%s&region=US&page=1", p.apiKey)
var resp tmdbUpcomingResponse
@@ -511,7 +525,9 @@ func (p *MoviesPlugin) handleUpcoming(ctx MessageContext) error {
sb.WriteString(fmt.Sprintf("%d. %s (%s)%s\n", i+1, title, dateStr, ratingStr))
}
return p.SendReply(ctx.RoomID, ctx.EventID, sb.String())
msg := sb.String()
db.CacheSet(cacheKey, msg)
return p.SendReply(ctx.RoomID, ctx.EventID, msg)
}
func (p *MoviesPlugin) tmdbGet(apiURL string, target interface{}) error {