Skip to content

Commit

Permalink
use DecodeLastRuneInString
Browse files Browse the repository at this point in the history
  • Loading branch information
ebiiim committed Dec 27, 2023
1 parent 8fabff6 commit 91214b1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,15 @@ func Ptr[T any](v T) *T {

// truncateDiscordMessage truncates a string to <2000 characters.
func truncateDiscordMessage(s string, msg string) string {
const maxLen = 1950 // 2000 - 50 (for safety)
const maxLen = 1980 // 2000 - 20 (for safety)
if utf8.RuneCountInString(s) <= maxLen {
return s
} else {
return s[:maxLen-1-utf8.RuneCountInString(msg)] + "\n" + msg
// cut chars
for utf8.RuneCountInString(s) > maxLen-1-utf8.RuneCountInString(msg) {
_, size := utf8.DecodeLastRuneInString(s)
s = s[:len(s)-size]
}
return s + "\n" + msg
}
}

0 comments on commit 91214b1

Please sign in to comment.