Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(QQ): 针对NCqq的图像格式的兼容性改进 #1066

Merged
merged 6 commits into from
Oct 15, 2024
28 changes: 25 additions & 3 deletions dice/platform_adapter_gocq.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ func FormatDiceIDQQChGroup(guildID, channelID string) string {
return fmt.Sprintf("QQ-CH-Group:%s-%s", guildID, channelID)
}

func isLink(text string) bool {
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
// 正则表达式匹配三种情况:file URI、http(s) URL 和 base64 URI
regex := `^(file:\/\/\/[^\s]+|(http|https):\/\/[^\s]+|base64:\/\/[a-zA-Z0-9+/=]+)$`
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
match, _ := regexp.MatchString(regex, text)
return match
}

func tryParseOneBot11ArrayMessage(log *log.Helper, message string, writeTo *MessageQQ) error {
msgQQType2 := new(MessageQQArray)
err := json.Unmarshal([]byte(message), msgQQType2)
Expand All @@ -289,7 +296,15 @@ func tryParseOneBot11ArrayMessage(log *log.Helper, message string, writeTo *Mess
case "text":
cqMessage.WriteString(i.Data["text"].(string))
case "image":
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", i.Data["file"]))
// 兼容NC的URL,这里的转换目前只有收到的时候才会用,所以如果收到的file没有http/https前缀,那目前可以判断一定是nc的情况,这样的话,我们读取URL。
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
fileurl := fmt.Sprintf("[CQ:image,file=%v]", i.Data["file"])
if isLink(fileurl) {
// 是正常的图片
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", fileurl))
} else {
// 非正常图片,取URL
cqMessage.WriteString(fmt.Sprintf("[CQ:image,file=%v]", i.Data["url"]))
}
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
case "face":
// 兼容四叶草,移除 .(string)。自动获取的信息表示此类型为 float64,这是go解析的问题
cqMessage.WriteString(fmt.Sprintf("[CQ:face,id=%v]", i.Data["id"]))
Expand Down Expand Up @@ -329,8 +344,15 @@ func OneBot11CqMessageToArrayMessage(longText string) []interface{} {
// 将 CQ 拼入数组
switch cq.Type {
case "image":
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["file"]}}
arr = append(arr, i)
// 同理,兼容NC的URL,这种情况一般是用户需要发送
PaienNate marked this conversation as resolved.
Show resolved Hide resolved
if isLink(cq.Args["file"]) {
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["file"]}}
arr = append(arr, i)
} else {
i := OneBotV11ArrMsgItem[OneBotV11MsgItemImageType]{Type: "image", Data: OneBotV11MsgItemImageType{File: cq.Args["url"]}}
arr = append(arr, i)
}

case "record":
i := OneBotV11ArrMsgItem[OneBotV11MsgItemRecordType]{Type: "record", Data: OneBotV11MsgItemRecordType{File: cq.Args["file"]}}
arr = append(arr, i)
Expand Down
Loading