Skip to content

Commit

Permalink
truncateDiscordMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ebiiim committed Dec 27, 2023
1 parent 0d6429a commit 8fabff6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Albion Onlineのギルド [Dog The Boston](https://twitter.com/DogTheBoston) 用

## リリースノート

- 2023-12-27 v1.8.1
- 修正: 応答がDiscordの文字数制限(2000字)を超えるときにエラーになる問題(省略するようにした)
- 2023-12-26 v1.8.0
- 機能: `ルートナビ` 確認用コマンド `/route-list` を追加
- 2023-12-24 v1.7.0
Expand Down
11 changes: 11 additions & 0 deletions pkg/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"unicode"
"unicode/utf8"

"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -145,3 +146,13 @@ func pickOne(choices []choice) string {
func Ptr[T any](v T) *T {
return &v
}

// truncateDiscordMessage truncates a string to <2000 characters.
func truncateDiscordMessage(s string, msg string) string {
const maxLen = 1950 // 2000 - 50 (for safety)
if utf8.RuneCountInString(s) <= maxLen {
return s
} else {
return s[:maxLen-1-utf8.RuneCountInString(msg)] + "\n" + msg
}
}
4 changes: 3 additions & 1 deletion pkg/handlers/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ func HandleReactionAddReactionRequired(s *discordgo.Session, r *discordgo.Messag
sb.WriteString(fmt.Sprintf("`%s` ", name))
}
}
resp := strings.TrimRight(sb.String(), " ")
resp = truncateDiscordMessage(resp, "...(省略)")

// Send the response.
msg, err := sendSilentMessage(s, r.ChannelID, &discordgo.MessageSend{
Content: strings.TrimRight(sb.String(), " "),
Content: resp,
Reference: parentMsg.Reference(),
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/handlers/slashcmd_roanav.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func (h *ROANavHandler) HandleCmdRouteList(s *discordgo.Session, i *discordgo.In
} else {
resp += smm
}
resp = truncateDiscordMessage(resp, "...長いので省略するわん!")

// Send response.
if mErr := respondEphemeralMessage(s, i, resp); mErr != nil {
Expand Down

0 comments on commit 8fabff6

Please sign in to comment.