Skip to content

Commit

Permalink
feat: 请求消息体增加至50000字符
Browse files Browse the repository at this point in the history
  • Loading branch information
xjd committed Feb 5, 2024
1 parent fb33792 commit 9770fe2
Show file tree
Hide file tree
Showing 5 changed files with 563 additions and 525 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,21 @@ func RandomElement[T any](slice []T) (T, error) {
index := rand.Intn(len(slice))
return slice[index], nil
}

func ReverseSegment(s string, segLen int) []string {
var result []string
runeSlice := []rune(s) // 将字符串转换为rune切片,以正确处理多字节字符

// 从字符串开头开始切片
for i := 0; i < len(runeSlice); i += segLen {
// 检查是否达到或超过字符串末尾
if i+segLen > len(runeSlice) {
// 如果超过,直接从当前位置到字符串末尾的所有字符都添加到结果切片中
result = append(result, string(runeSlice[i:len(runeSlice)]))
} else {
// 否则,从当前位置到i+segLen的子切片添加到结果切片中
result = append(result, string(runeSlice[i:i+segLen]))
}
}
return result
}
34 changes: 27 additions & 7 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,39 @@ func SendMessage(c *gin.Context, channelID, cozeBotId, message string) (*discord
return nil, fmt.Errorf("discord session not initialized")
}

content := fmt.Sprintf("<@%s> %s", cozeBotId, message)
var sentMsg *discordgo.Message

if runeCount := len([]rune(content)); runeCount > 2000 {
content := fmt.Sprintf("%s <@%s>", message, cozeBotId)

if runeCount := len([]rune(content)); runeCount > 50000 {
common.LogError(ctx, fmt.Sprintf("prompt已超过限制,请分段发送 [%v] %s", runeCount, content))
return nil, fmt.Errorf("prompt已超过限制,请分段发送 [%v]", runeCount)
}

// 添加@机器人逻辑
sentMsg, err := Session.ChannelMessageSend(channelID, content)
if err != nil {
common.LogError(ctx, fmt.Sprintf("error sending message: %s", err))
return nil, fmt.Errorf("error sending message")
for i, msg := range common.ReverseSegment(content, 2000) {
sentMsg, err := Session.ChannelMessageSend(channelID, msg)
if err != nil {
common.LogError(ctx, fmt.Sprintf("error sending message: %s", err))
return nil, fmt.Errorf("error sending message")
}
if i == len(common.ReverseSegment(content, 2000))-1 {
return sentMsg, nil
}
}

//content := fmt.Sprintf("<@%s> %s", cozeBotId, message)
//
//if runeCount := len([]rune(content)); runeCount > 2000 {
// common.LogError(ctx, fmt.Sprintf("prompt已超过限制,请分段发送 [%v] %s", runeCount, content))
// return nil, fmt.Errorf("prompt已超过限制,请分段发送 [%v]", runeCount)
//}
//
//// 添加@机器人逻辑
//sentMsg, err := Session.ChannelMessageSend(channelID, content)
//if err != nil {
// common.LogError(ctx, fmt.Sprintf("error sending message: %s", err))
// return nil, fmt.Errorf("error sending message")
//}
return sentMsg, nil
}

Expand Down
Loading

0 comments on commit 9770fe2

Please sign in to comment.