Files
velox-bot/internal/tickets/transcript.go
T
FernandoJVideiraandClaude Opus 5.5 d294b3463b Add support tickets that work with private threads
Members open a ticket from a panel in the ticket channel: one button per
category (a dropdown past five), then a short form. Each ticket is a private
thread named after the category, number and opener, with a first message
that mentions the category's staff role so its members join, and Claim and
Close buttons.

- Claim: staff (the category's role, or Manage Threads / Manage Server)
  take a ticket; the first message shows who has it.
- Close: the opener or staff close it, with an optional reason. The thread
  is locked and archived, and the log channel gets a summary and a .txt
  transcript of the whole thread.
- /ticket add|remove|claim|close inside a ticket; /tickets setup,
  /tickets category add|remove|list and /tickets panel for admins.
- Per-member limit on open tickets (default 1), per-server ticket numbers
  handed out atomically, a deleted thread closes its ticket.
- The dashboard asks the bot to post or refresh the panel through
  ticket_commands (LISTEN/NOTIFY, like music); the listener reconnects if
  the database connection drops.
- Setup warns about missing bot permissions in the ticket and log channels.

The flows live in internal/tickets behind small Discord and store
interfaces, with unit tests on fakes; the repo and the listener have
integration tests (-tags integration, TICKETS_TEST_DB).

Needs the ticket tables (velox-dashboard-api migration 000016_tickets).
.gitignore gains the /tldr benchmark rules so private chat exports and
benchmark results on disk can't be committed from this branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-20 19:31:52 +01:00

89 lines
2.3 KiB
Go

package tickets
import (
"fmt"
"strings"
"velox-bot/internal/db/repos/ticketrepo"
"github.com/bwmarrin/discordgo"
)
// maxTranscriptMessages bounds how far back a transcript reads: 50 pages of
// 100, far beyond any normal ticket.
const maxTranscriptMessages = 5000
// Transcript renders the whole thread as plain text, oldest first, and
// reports how many messages it holds. Attachments and embeds are listed so
// the log keeps their links and titles.
func Transcript(d Discord, t *ticketrepo.Ticket) ([]byte, int, error) {
var msgs []*discordgo.Message
before := ""
var fetchErr error
for len(msgs) < maxTranscriptMessages {
page, err := d.ChannelMessages(id(t.ThreadID), 100, before, "", "")
if err != nil {
fetchErr = err
break
}
msgs = append(msgs, page...)
if len(page) < 100 {
break
}
before = page[len(page)-1].ID
}
var b strings.Builder
fmt.Fprintf(&b, "Ticket #%04d · %s\n", t.Number, t.CategoryName)
fmt.Fprintf(&b, "Subject: %s\n", t.Subject)
fmt.Fprintf(&b, "Opened by user %d on %s\n", t.OpenerID, t.CreatedAt.UTC().Format("2006-01-02 15:04 MST"))
if fetchErr != nil {
fmt.Fprintf(&b, "Note: only part of the thread could be read (%v)\n", fetchErr)
}
b.WriteString(strings.Repeat("-", 60) + "\n\n")
// Discord returns newest first.
for i := len(msgs) - 1; i >= 0; i-- {
writeMessage(&b, msgs[i])
}
return []byte(b.String()), len(msgs), fetchErr
}
func writeMessage(b *strings.Builder, m *discordgo.Message) {
name := "unknown"
if m.Author != nil {
name = m.Author.Username
if m.Author.GlobalName != "" {
name = m.Author.GlobalName + " (" + m.Author.Username + ")"
}
if m.Author.Bot {
name += " [bot]"
}
}
fmt.Fprintf(b, "[%s] %s\n", m.Timestamp.UTC().Format("2006-01-02 15:04"), name)
if content := strings.TrimSpace(m.Content); content != "" {
for _, line := range strings.Split(content, "\n") {
b.WriteString(" " + line + "\n")
}
}
for _, e := range m.Embeds {
if e == nil {
continue
}
title := e.Title
if title == "" {
title = clip(e.Description, 120)
}
if title != "" {
b.WriteString(" [embed] " + strings.ReplaceAll(title, "\n", " ") + "\n")
}
}
for _, a := range m.Attachments {
if a != nil {
b.WriteString(" [file] " + a.Filename + " " + a.URL + "\n")
}
}
b.WriteString("\n")
}