From 91214b1ed04e05edc1855fa849edb342a0f282f5 Mon Sep 17 00:00:00 2001 From: Shunsuke Ise Date: Wed, 27 Dec 2023 17:40:10 +0900 Subject: [PATCH] use DecodeLastRuneInString --- pkg/handlers/common.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/handlers/common.go b/pkg/handlers/common.go index 7b1d36e..c1cfd20 100644 --- a/pkg/handlers/common.go +++ b/pkg/handlers/common.go @@ -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 } }