mirror of
https://github.com/prosolis/gogobee.git
synced 2026-07-15 08:32:41 +00:00
- Add forex plugin (Frankfurter v2 API) with rate lookups, analysis, DM-based alerts, and daily cron poll. Backfills 1 year of history on startup for moving averages and buy signal scoring. - Fix bot hang caused by SQLite lock contention in reminder polling: rows cursor was held open while writing to the same DB. Collect results first, close cursor, then process. Same fix in milkcarton. - Add sync retry loop so the bot reconnects after network drops instead of silently exiting. StopSync() for clean Ctrl+C shutdown. - Add panic recovery to all dispatch, syncer, and cron paths. - Make all HTTP-calling plugin commands async (goroutines) so a slow or dead external API cannot block the message dispatch pipeline. Affects: lookup, stocks, forex, anime, movies, concerts, gaming, retro, wotd, urls, howami. - Extract DisplayName to Base, add db.Exec helper, convert silent error discards across the codebase. - Fix UNO mercy-kill bug (eliminated bot continues playing), adventure DM nag spam, stats column mismatch, per-call regex/replacer allocs. - Update README: forex commands, Finance section, 47 plugins. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
141 lines
3.8 KiB
Go
141 lines
3.8 KiB
Go
package plugin
|
|
|
|
import (
|
|
"gogobee/internal/db"
|
|
)
|
|
|
|
// ── Rate Storage ────────────────────────────────────────────────────────────
|
|
|
|
func fxSaveRate(currency, date string, rate float64) {
|
|
db.Exec("forex: save rate",
|
|
`INSERT OR IGNORE INTO forex_rates (currency, date, rate) VALUES (?, ?, ?)`,
|
|
currency, date, rate)
|
|
}
|
|
|
|
type fxRateRecord struct {
|
|
Date string
|
|
Rate float64
|
|
}
|
|
|
|
// fxGetRates returns rates for a currency in the given date range, ordered by date.
|
|
// Uses ORDER BY date DESC LIMIT n when limit > 0 (for "last N trading days").
|
|
func fxGetRatesByLimit(currency string, limit int) ([]fxRateRecord, error) {
|
|
d := db.Get()
|
|
rows, err := d.Query(
|
|
`SELECT date, rate FROM forex_rates WHERE currency = ? ORDER BY date DESC LIMIT ?`,
|
|
currency, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var records []fxRateRecord
|
|
for rows.Next() {
|
|
var r fxRateRecord
|
|
if err := rows.Scan(&r.Date, &r.Rate); err != nil {
|
|
continue
|
|
}
|
|
records = append(records, r)
|
|
}
|
|
|
|
// Reverse to chronological order (oldest first)
|
|
for i, j := 0, len(records)-1; i < j; i, j = i+1, j-1 {
|
|
records[i], records[j] = records[j], records[i]
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
// fxLatestRate returns the most recent stored rate for a currency.
|
|
func fxLatestRate(currency string) (float64, bool) {
|
|
d := db.Get()
|
|
var rate float64
|
|
err := d.QueryRow(
|
|
`SELECT rate FROM forex_rates WHERE currency = ? ORDER BY date DESC LIMIT 1`,
|
|
currency).Scan(&rate)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
return rate, true
|
|
}
|
|
|
|
// ── Alert Storage ───────────────────────────────────────────────────────────
|
|
|
|
type fxAlertRecord struct {
|
|
UserID string
|
|
Currency string
|
|
Threshold float64
|
|
FiredAt int64
|
|
}
|
|
|
|
func fxSaveAlert(userID, currency string, threshold float64) error {
|
|
d := db.Get()
|
|
_, err := d.Exec(
|
|
`INSERT OR REPLACE INTO forex_alerts (user_id, currency, threshold, fired_at)
|
|
VALUES (?, ?, ?, 0)`,
|
|
userID, currency, threshold)
|
|
return err
|
|
}
|
|
|
|
func fxAlertsForUser(userID string) ([]fxAlertRecord, error) {
|
|
d := db.Get()
|
|
rows, err := d.Query(
|
|
`SELECT user_id, currency, threshold, fired_at
|
|
FROM forex_alerts WHERE user_id = ? ORDER BY currency, threshold`,
|
|
userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var alerts []fxAlertRecord
|
|
for rows.Next() {
|
|
var a fxAlertRecord
|
|
if err := rows.Scan(&a.UserID, &a.Currency, &a.Threshold, &a.FiredAt); err != nil {
|
|
continue
|
|
}
|
|
alerts = append(alerts, a)
|
|
}
|
|
return alerts, nil
|
|
}
|
|
|
|
func fxAllAlerts() ([]fxAlertRecord, error) {
|
|
d := db.Get()
|
|
rows, err := d.Query(
|
|
`SELECT user_id, currency, threshold, fired_at FROM forex_alerts`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var alerts []fxAlertRecord
|
|
for rows.Next() {
|
|
var a fxAlertRecord
|
|
if err := rows.Scan(&a.UserID, &a.Currency, &a.Threshold, &a.FiredAt); err != nil {
|
|
continue
|
|
}
|
|
alerts = append(alerts, a)
|
|
}
|
|
return alerts, nil
|
|
}
|
|
|
|
func fxDeleteAlert(userID, currency string, threshold float64) error {
|
|
d := db.Get()
|
|
_, err := d.Exec(
|
|
`DELETE FROM forex_alerts WHERE user_id = ? AND currency = ? AND threshold = ?`,
|
|
userID, currency, threshold)
|
|
return err
|
|
}
|
|
|
|
func fxMarkAlertFired(a fxAlertRecord) {
|
|
db.Exec("forex: mark alert fired",
|
|
`UPDATE forex_alerts SET fired_at = unixepoch() WHERE user_id = ? AND currency = ? AND threshold = ?`,
|
|
a.UserID, a.Currency, a.Threshold)
|
|
}
|
|
|
|
// fxResetExpiredAlerts clears fired_at for alerts that fired more than 24 hours ago,
|
|
// allowing them to re-fire if the threshold is still met.
|
|
func fxResetExpiredAlerts() {
|
|
db.Exec("forex: reset expired alerts",
|
|
`UPDATE forex_alerts SET fired_at = 0 WHERE fired_at > 0 AND fired_at < unixepoch() - 86400`)
|
|
}
|