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

77 lines
2.2 KiB
Go

package bot
import (
"context"
"fmt"
"math"
"strings"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) handleAddKM(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !b.isAdmin(i) {
respondEphemeral(s, i, "You need the **Manage Server** permission to use this command.")
return
}
opts := i.ApplicationCommandData().Options
targetUser := opts[0].UserValue(s)
km := opts[1].FloatValue()
targetMember, _ := s.GuildMember(i.GuildID, targetUser.ID)
name := displayName(targetMember, targetUser)
if err := b.db.AdjustKM(ctx, i.GuildID, targetUser.ID, name, km); err != nil {
respondEphemeral(s, i, "Error updating KM.")
return
}
verb := "added"
if km < 0 {
verb = "removed"
km = math.Abs(km)
}
respondEphemeral(s, i, fmt.Sprintf("✅ %s %.1f km for **%s**.", titleCase(verb), km, name))
}
func (b *Bot) handleRemoveLog(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !b.isAdmin(i) {
respondEphemeral(s, i, "You need the **Manage Server** permission to use this command.")
return
}
messageID := i.ApplicationCommandData().Options[0].StringValue()
km, err := b.db.RemoveLog(ctx, messageID)
if err != nil {
respondEphemeral(s, i, "Error removing log.")
return
}
if km == 0 {
respondEphemeral(s, i, "No log found for that message ID.")
return
}
respondEphemeral(s, i, fmt.Sprintf("✅ Removed a **%.1f km** entry.", km))
}
func (b *Bot) handleAudit(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
if !b.isAdmin(i) {
respondEphemeral(s, i, "You need the **Manage Server** permission to use this command.")
return
}
target := i.ApplicationCommandData().Options[0].UserValue(s)
targetMember, _ := s.GuildMember(i.GuildID, target.ID)
name := displayName(targetMember, target)
logs, err := b.db.GetUserLogs(ctx, i.GuildID, target.ID, 20)
if err != nil || len(logs) == 0 {
respondEphemeral(s, i, fmt.Sprintf("No logs found for **%s**.", name))
return
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## 🔍 Audit: %s (last %d)\n\n", name, len(logs)))
for _, l := range logs {
sb.WriteString(fmt.Sprintf("`%s` — **%.1f km** (msg: `%s`)\n",
l.LoggedAt.Format("02 Jan 15:04"), l.KM, l.MessageID))
}
respondEphemeral(s, i, sb.String())
}