mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
coolq: refactor send forward message
This commit is contained in:
parent
062eea62ab
commit
112441d76e
204
coolq/api.go
204
coolq/api.go
@ -789,116 +789,120 @@ func (bot *CQBot) CQSendGuildChannelMessage(guildID, channelID uint64, m gjson.R
|
|||||||
|
|
||||||
func (bot *CQBot) uploadForwardElement(m gjson.Result, groupID int64) *message.ForwardElement {
|
func (bot *CQBot) uploadForwardElement(m gjson.Result, groupID int64) *message.ForwardElement {
|
||||||
ts := time.Now().Add(-time.Minute * 5)
|
ts := time.Now().Add(-time.Minute * 5)
|
||||||
fm := message.NewForwardMessage()
|
|
||||||
source := message.Source{SourceType: message.SourceGroup, PrimaryID: groupID}
|
source := message.Source{SourceType: message.SourceGroup, PrimaryID: groupID}
|
||||||
|
builder := bot.Client.NewForwardMessageBuilder(groupID)
|
||||||
|
|
||||||
var w worker
|
var convertMessage func(m gjson.Result) *message.ForwardMessage
|
||||||
resolveElement := func(elems []message.IMessageElement) []message.IMessageElement {
|
convertMessage = func(m gjson.Result) *message.ForwardMessage {
|
||||||
for i, elem := range elems {
|
fm := message.NewForwardMessage()
|
||||||
p := &elems[i]
|
var w worker
|
||||||
switch o := elem.(type) {
|
resolveElement := func(elems []message.IMessageElement) []message.IMessageElement {
|
||||||
case *LocalVideoElement:
|
for i, elem := range elems {
|
||||||
w.do(func() {
|
p := &elems[i]
|
||||||
gm, err := bot.uploadLocalVideo(source, o)
|
switch o := elem.(type) {
|
||||||
if err != nil {
|
case *LocalVideoElement:
|
||||||
log.Warnf(uploadFailedTemplate, "群", groupID, "视频", err)
|
w.do(func() {
|
||||||
} else {
|
gm, err := bot.uploadLocalVideo(source, o)
|
||||||
*p = gm
|
if err != nil {
|
||||||
}
|
log.Warnf(uploadFailedTemplate, "群", groupID, "视频", err)
|
||||||
})
|
} else {
|
||||||
case *LocalImageElement:
|
*p = gm
|
||||||
w.do(func() {
|
|
||||||
gm, err := bot.uploadLocalImage(source, o)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf(uploadFailedTemplate, "群", groupID, "图片", err)
|
|
||||||
} else {
|
|
||||||
*p = gm
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return elems
|
|
||||||
}
|
|
||||||
|
|
||||||
convert := func(e gjson.Result) *message.ForwardNode {
|
|
||||||
if e.Get("type").Str != "node" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ts.Add(time.Second)
|
|
||||||
if e.Get("data.id").Exists() {
|
|
||||||
i := e.Get("data.id").Int()
|
|
||||||
m, _ := db.GetGroupMessageByGlobalID(int32(i))
|
|
||||||
if m != nil {
|
|
||||||
return &message.ForwardNode{
|
|
||||||
SenderId: m.Attribute.SenderUin,
|
|
||||||
SenderName: m.Attribute.SenderName,
|
|
||||||
Time: func() int32 {
|
|
||||||
msgTime := m.Attribute.Timestamp
|
|
||||||
if msgTime == 0 {
|
|
||||||
return int32(ts.Unix())
|
|
||||||
}
|
}
|
||||||
return int32(msgTime)
|
})
|
||||||
}(),
|
case *LocalImageElement:
|
||||||
Message: resolveElement(bot.ConvertContentMessage(m.Content, message.SourceGroup)),
|
w.do(func() {
|
||||||
|
gm, err := bot.uploadLocalImage(source, o)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf(uploadFailedTemplate, "群", groupID, "图片", err)
|
||||||
|
} else {
|
||||||
|
*p = gm
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
|
return elems
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
uin := e.Get("data.[user_id,uin].0").Int()
|
|
||||||
msgTime := e.Get("data.time").Int()
|
convert := func(e gjson.Result) *message.ForwardNode {
|
||||||
if msgTime == 0 {
|
if e.Get("type").Str != "node" {
|
||||||
msgTime = ts.Unix()
|
return nil
|
||||||
}
|
}
|
||||||
name := e.Get("data.name").Str
|
if e.Get("data.id").Exists() {
|
||||||
c := e.Get("data.content")
|
i := e.Get("data.id").Int()
|
||||||
if c.IsArray() {
|
m, _ := db.GetGroupMessageByGlobalID(int32(i))
|
||||||
nested := false
|
if m != nil {
|
||||||
c.ForEach(func(_, value gjson.Result) bool {
|
msgTime := m.Attribute.Timestamp
|
||||||
if value.Get("type").Str == "node" {
|
if msgTime == 0 {
|
||||||
nested = true
|
msgTime = ts.Unix()
|
||||||
return false
|
}
|
||||||
|
return &message.ForwardNode{
|
||||||
|
SenderId: m.Attribute.SenderUin,
|
||||||
|
SenderName: m.Attribute.SenderName,
|
||||||
|
Time: int32(msgTime),
|
||||||
|
Message: resolveElement(bot.ConvertContentMessage(m.Content, message.SourceGroup)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
|
||||||
})
|
return nil
|
||||||
if nested { // 处理嵌套
|
}
|
||||||
fe := bot.uploadForwardElement(c, groupID)
|
uin := e.Get("data.[user_id,uin].0").Int()
|
||||||
|
msgTime := e.Get("data.time").Int()
|
||||||
|
if msgTime == 0 {
|
||||||
|
msgTime = ts.Unix()
|
||||||
|
}
|
||||||
|
name := e.Get("data.name").Str
|
||||||
|
c := e.Get("data.content")
|
||||||
|
if c.IsArray() {
|
||||||
|
nested := false
|
||||||
|
c.ForEach(func(_, value gjson.Result) bool {
|
||||||
|
if value.Get("type").Str == "node" {
|
||||||
|
nested = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if nested { // 处理嵌套
|
||||||
|
nestedNode := builder.NestedNode()
|
||||||
|
builder.Link(nestedNode, convertMessage(c))
|
||||||
|
return &message.ForwardNode{
|
||||||
|
SenderId: uin,
|
||||||
|
SenderName: name,
|
||||||
|
Time: int32(msgTime),
|
||||||
|
Message: []message.IMessageElement{nestedNode},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content := bot.ConvertObjectMessage(c, message.SourceGroup)
|
||||||
|
if uin != 0 && name != "" && len(content) > 0 {
|
||||||
return &message.ForwardNode{
|
return &message.ForwardNode{
|
||||||
SenderId: uin,
|
SenderId: uin,
|
||||||
SenderName: name,
|
SenderName: name,
|
||||||
Time: int32(msgTime),
|
Time: int32(msgTime),
|
||||||
Message: []message.IMessageElement{fe},
|
Message: resolveElement(content),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
content := bot.ConvertObjectMessage(c, message.SourceGroup)
|
|
||||||
if uin != 0 && name != "" && len(content) > 0 {
|
|
||||||
return &message.ForwardNode{
|
|
||||||
SenderId: uin,
|
|
||||||
SenderName: name,
|
|
||||||
Time: int32(msgTime),
|
|
||||||
Message: resolveElement(content),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.IsArray() {
|
if m.IsArray() {
|
||||||
for _, item := range m.Array() {
|
for _, item := range m.Array() {
|
||||||
node := convert(item)
|
node := convert(item)
|
||||||
|
if node != nil {
|
||||||
|
fm.AddNode(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
node := convert(m)
|
||||||
if node != nil {
|
if node != nil {
|
||||||
fm.AddNode(node)
|
fm.AddNode(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
node := convert(m)
|
w.wait()
|
||||||
if node != nil {
|
return fm
|
||||||
fm.AddNode(node)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
w.wait()
|
return builder.Main(convertMessage(m))
|
||||||
return bot.Client.UploadGroupForwardMessage(groupID, fm)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CQSendGroupForwardMessage 扩展API-发送合并转发(群)
|
// CQSendGroupForwardMessage 扩展API-发送合并转发(群)
|
||||||
@ -912,17 +916,17 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
|||||||
}
|
}
|
||||||
|
|
||||||
fe := bot.uploadForwardElement(m, groupID)
|
fe := bot.uploadForwardElement(m, groupID)
|
||||||
if fe != nil {
|
if fe == nil {
|
||||||
ret := bot.Client.SendGroupForwardMessage(groupID, fe)
|
return Failed(100, "EMPTY_NODES", "未找到任何可发送的合并转发信息")
|
||||||
if ret == nil || ret.Id == -1 {
|
|
||||||
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
|
|
||||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
|
|
||||||
}
|
|
||||||
return OK(global.MSG{
|
|
||||||
"message_id": bot.InsertGroupMessage(ret),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return Failed(100, "EMPTY_NODES", "未找到任何可发送的合并转发信息")
|
ret := bot.Client.SendGroupForwardMessage(groupID, fe)
|
||||||
|
if ret == nil || ret.Id == -1 {
|
||||||
|
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
|
||||||
|
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
|
||||||
|
}
|
||||||
|
return OK(global.MSG{
|
||||||
|
"message_id": bot.InsertGroupMessage(ret),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CQSendPrivateMessage 发送私聊消息
|
// CQSendPrivateMessage 发送私聊消息
|
||||||
|
4
go.mod
4
go.mod
@ -4,8 +4,8 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Microsoft/go-winio v0.5.1
|
github.com/Microsoft/go-winio v0.5.1
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20220320070754-38990f6e1cf9
|
github.com/Mrs4s/MiraiGo v0.0.0-20220321133915-aa657c0f09d0
|
||||||
github.com/RomiChan/syncx v0.0.0-20220320065321-8d448f958257
|
github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
||||||
github.com/fumiama/go-hide-param v0.1.4
|
github.com/fumiama/go-hide-param v0.1.4
|
||||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
||||||
|
8
go.sum
8
go.sum
@ -1,11 +1,11 @@
|
|||||||
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
|
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
|
||||||
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
|
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20220320070754-38990f6e1cf9 h1:YJeHJRNVfpbuC7wSQD3WOcysC7AcILgQf5d8kFPL12Y=
|
github.com/Mrs4s/MiraiGo v0.0.0-20220321133915-aa657c0f09d0 h1:RsFAUylRh9T4zIx4iVe8KTbG/XtI/uHw2qhhQfoCetE=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20220320070754-38990f6e1cf9/go.mod h1:qseuVVwl5wxdhphaS29F6CN0gV2J0ojT880Ql5g2MBo=
|
github.com/Mrs4s/MiraiGo v0.0.0-20220321133915-aa657c0f09d0/go.mod h1:APw03y3A3Qp5In9gI/S9m7DBJkWsd6FQMV+fB636bmU=
|
||||||
github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896 h1:UFAqSbH6VqW5mEzQV2HVB7+p3k9JfTbidWJ/9F15yz0=
|
github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896 h1:UFAqSbH6VqW5mEzQV2HVB7+p3k9JfTbidWJ/9F15yz0=
|
||||||
github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE=
|
github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE=
|
||||||
github.com/RomiChan/syncx v0.0.0-20220320065321-8d448f958257 h1:fdMod+DEoiICoTtS1Wij/wl1d57FPvKVmretLi2sGD8=
|
github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c h1:zHWyqx7A71A/+mlzthPVcVrNGuTPyTpCW3meUPtaULU=
|
||||||
github.com/RomiChan/syncx v0.0.0-20220320065321-8d448f958257/go.mod h1:KqZzu7slNKROh3TSYEH/IUMG6f4M+1qubZ5e52QypsE=
|
github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c/go.mod h1:KqZzu7slNKROh3TSYEH/IUMG6f4M+1qubZ5e52QypsE=
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
|
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=
|
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user