Files
cyclingbot/bot/handlers_reports.go
Blake Ridgway ba1770b493 first commit
2026-04-11 14:06:59 -05:00

44 lines
1.3 KiB
Go

package bot
import (
"context"
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) handleWeeklyReport(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
from := weekStart()
b.sendReport(ctx, s, i, fmt.Sprintf("📅 Week of %s", from.Format("2 Jan")), from, time.Now().UTC())
}
func (b *Bot) handleMonthlyReport(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
from := monthStart()
b.sendReport(ctx, s, i, fmt.Sprintf("📅 %s", from.Format("January 2006")), from, time.Now().UTC())
}
func (b *Bot) sendReport(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, title string, from, to time.Time) {
entries, err := b.db.GetStatsInRange(ctx, i.GuildID, from, to, 20)
if err != nil {
respondEphemeral(s, i, "Error fetching report.")
return
}
if len(entries) == 0 {
respondEphemeral(s, i, "No rides logged in this period yet.")
return
}
var total float64
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## %s Report\n\n", title))
for idx, e := range entries {
sb.WriteString(fmt.Sprintf("**%d.** %s — **%.1f km** (%d ride%s)\n",
idx+1, e.Username, e.TotalKM, e.LogCount, plural(e.LogCount)))
total += e.TotalKM
}
sb.WriteString(fmt.Sprintf("\n**Period total:** %.1f km", total))
respond(s, i, sb.String())
}