feat: add truncateText and truncateURL helpers for Discord field limits

Discord silently rejects presence updates when text fields exceed 128
characters or URL fields exceed 256 characters. Fixes #16
This commit is contained in:
deluan
2026-03-04 12:14:58 -05:00
parent c22b950be3
commit c5af3c1d63
2 changed files with 75 additions and 0 deletions
+24
View File
@@ -55,6 +55,30 @@ const (
const heartbeatInterval = 41 // Heartbeat interval in seconds
// Discord API field length limits
const (
maxTextLength = 128 // Max characters for text fields (details, state, name, large_text)
maxURLLength = 256 // Max characters for URL fields (details_url, state_url, etc.)
)
// truncateText truncates s to maxTextLength runes, appending "…" if truncated.
func truncateText(s string) string {
runes := []rune(s)
if len(runes) <= maxTextLength {
return s
}
return string(runes[:maxTextLength-1]) + "…"
}
// truncateURL returns s unchanged if within maxURLLength, otherwise returns ""
// (a truncated URL would be broken, so we omit it entirely).
func truncateURL(s string) string {
if len(s) <= maxURLLength {
return s
}
return ""
}
// activity represents a Discord activity sent via Gateway opcode 3.
type activity struct {
Name string `json:"name"`