mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
feat(coolq): upload images/videos in forward message concurrently
This commit is contained in:
parent
80e35fc800
commit
b8fe459c75
76
coolq/api.go
76
coolq/api.go
@ -12,6 +12,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/segmentio/asm/base64"
|
"github.com/segmentio/asm/base64"
|
||||||
@ -787,36 +788,26 @@ func (bot *CQBot) CQSendGuildChannelMessage(guildID, channelID uint64, m gjson.R
|
|||||||
return OK(global.MSG{"message_id": mid})
|
return OK(global.MSG{"message_id": mid})
|
||||||
}
|
}
|
||||||
|
|
||||||
// CQSendGroupForwardMessage 扩展API-发送合并转发(群)
|
func (bot *CQBot) uploadForwardElement(m gjson.Result, groupID int64) *message.ForwardElement {
|
||||||
//
|
|
||||||
// https://docs.go-cqhttp.org/api/#%E5%8F%91%E9%80%81%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91-%E7%BE%A4
|
|
||||||
// @route(send_group_forward_msg)
|
|
||||||
// @rename(m->messages)
|
|
||||||
func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) global.MSG {
|
|
||||||
if m.Type != gjson.JSON {
|
|
||||||
return Failed(100)
|
|
||||||
}
|
|
||||||
fm := message.NewForwardMessage()
|
|
||||||
ts := time.Now().Add(-time.Minute * 5)
|
ts := time.Now().Add(-time.Minute * 5)
|
||||||
hasCustom := false
|
fm := message.NewForwardMessage()
|
||||||
m.ForEach(func(_, item gjson.Result) bool {
|
|
||||||
if item.Get("data.uin").Exists() || item.Get("data.user_id").Exists() {
|
|
||||||
hasCustom = true
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
|
var lazyUpload []func()
|
||||||
|
var wg sync.WaitGroup
|
||||||
resolveElement := func(elems []message.IMessageElement) []message.IMessageElement {
|
resolveElement := func(elems []message.IMessageElement) []message.IMessageElement {
|
||||||
for i, elem := range elems {
|
for i, elem := range elems {
|
||||||
switch elem.(type) {
|
switch elem.(type) {
|
||||||
case *LocalImageElement, *LocalVideoElement:
|
case *LocalImageElement, *LocalVideoElement:
|
||||||
gm, err := bot.uploadMedia(elem, groupID, true)
|
wg.Add(1)
|
||||||
if err != nil {
|
lazyUpload = append(lazyUpload, func() {
|
||||||
log.Warnf("警告: 群 %d %s上传失败: %v", groupID, elem.Type().String(), err)
|
defer wg.Done()
|
||||||
continue
|
gm, err := bot.uploadMedia(elem, groupID, true)
|
||||||
}
|
if err != nil {
|
||||||
elems[i] = gm
|
log.Warnf("警告: 群 %d %s上传失败: %v", groupID, elem.Type().String(), err)
|
||||||
|
} else {
|
||||||
|
elems[i] = gm
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return elems
|
return elems
|
||||||
@ -837,7 +828,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
|||||||
SenderName: m.Attribute.SenderName,
|
SenderName: m.Attribute.SenderName,
|
||||||
Time: func() int32 {
|
Time: func() int32 {
|
||||||
msgTime := m.Attribute.Timestamp
|
msgTime := m.Attribute.Timestamp
|
||||||
if hasCustom && msgTime == 0 {
|
if msgTime == 0 {
|
||||||
return int32(ts.Unix())
|
return int32(ts.Unix())
|
||||||
}
|
}
|
||||||
return int32(msgTime)
|
return int32(msgTime)
|
||||||
@ -865,19 +856,12 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if nested { // 处理嵌套
|
if nested { // 处理嵌套
|
||||||
nest := message.NewForwardMessage()
|
fe := bot.uploadForwardElement(c, groupID)
|
||||||
for _, item := range c.Array() {
|
|
||||||
node := convert(item)
|
|
||||||
if node != nil {
|
|
||||||
nest.AddNode(node)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elem := bot.Client.UploadGroupForwardMessage(groupID, nest)
|
|
||||||
return &message.ForwardNode{
|
return &message.ForwardNode{
|
||||||
SenderId: uin,
|
SenderId: uin,
|
||||||
SenderName: name,
|
SenderName: name,
|
||||||
Time: int32(msgTime),
|
Time: int32(msgTime),
|
||||||
Message: []message.IMessageElement{elem},
|
Message: []message.IMessageElement{fe},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -893,6 +877,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
|||||||
log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
|
log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.IsArray() {
|
if m.IsArray() {
|
||||||
for _, item := range m.Array() {
|
for _, item := range m.Array() {
|
||||||
node := convert(item)
|
node := convert(item)
|
||||||
@ -906,8 +891,27 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
|
|||||||
fm.AddNode(node)
|
fm.AddNode(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if fm.Length() > 0 {
|
|
||||||
fe := bot.Client.UploadGroupForwardMessage(groupID, fm)
|
for _, upload := range lazyUpload {
|
||||||
|
go upload()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return bot.Client.UploadGroupForwardMessage(groupID, fm)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CQSendGroupForwardMessage 扩展API-发送合并转发(群)
|
||||||
|
//
|
||||||
|
// https://docs.go-cqhttp.org/api/#%E5%8F%91%E9%80%81%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91-%E7%BE%A4
|
||||||
|
// @route(send_group_forward_msg)
|
||||||
|
// @rename(m->messages)
|
||||||
|
func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) global.MSG {
|
||||||
|
if m.Type != gjson.JSON {
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
fe := bot.uploadForwardElement(m, groupID)
|
||||||
|
if fe != nil {
|
||||||
ret := bot.Client.SendGroupForwardMessage(groupID, fe)
|
ret := bot.Client.SendGroupForwardMessage(groupID, fe)
|
||||||
if ret == nil || ret.Id == -1 {
|
if ret == nil || ret.Id == -1 {
|
||||||
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
|
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user