Skip to content

Commit 3f1e349

Browse files
authored
update: 更新 LagrangeGo 到最新 (#38)
* update: 更新LagrangeGo 到最新 * fix: 修复 图片保存和读取 相关功能
1 parent ae92e97 commit 3f1e349

File tree

18 files changed

+186
-192
lines changed

18 files changed

+186
-192
lines changed

cmd/gocq/login.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/LagrangeDev/LagrangeGo/client"
1515
"github.com/LagrangeDev/LagrangeGo/client/auth"
1616
"github.com/LagrangeDev/LagrangeGo/client/packets/wtlogin/qrcodestate"
17-
"github.com/LagrangeDev/LagrangeGo/utils"
17+
"github.com/LagrangeDev/LagrangeGo/utils/io"
1818
"github.com/mattn/go-colorable"
1919
"github.com/pkg/errors"
2020
log "github.com/sirupsen/logrus"
@@ -81,7 +81,7 @@ func printQRCode(imgData []byte) {
8181
}
8282

8383
bound := img.Bounds().Max.X
84-
buf := make([]byte, 0, (bound+1)*(bound/2+utils.Ternary(bound%2 == 0, 0, 1)))
84+
buf := make([]byte, 0, (bound+1)*(bound/2+io.Ternary(bound%2 == 0, 0, 1)))
8585

8686
padding := 0
8787
lastColor := img.At(padding, padding).(color.Gray).Y
@@ -94,7 +94,7 @@ func printQRCode(imgData []byte) {
9494
for y := padding; y < bound-padding; y += 2 {
9595
for x := padding; x < bound-padding; x++ {
9696
isUpWhite := img.At(x, y).(color.Gray).Y == 255
97-
isDownWhite := utils.Ternary(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)
97+
isDownWhite := io.Ternary(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)
9898

9999
switch {
100100
case !isUpWhite && !isDownWhite:
@@ -277,7 +277,7 @@ func getTicket(u string) (string, string) {
277277
log.Warnf("2. 手动抓取提交")
278278
log.Warn("请输入(1 - 2):")
279279
text := readLine()
280-
id := utils.NewUUID()
280+
id := io.NewUUID()
281281
auto := !strings.Contains(text, "2")
282282
// TODO 自动获取验证码
283283
if auto {

cmd/gocq/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/LagrangeDev/LagrangeGo/client"
1818
"github.com/LagrangeDev/LagrangeGo/client/auth"
1919
"github.com/LagrangeDev/LagrangeGo/client/packets/pb/action"
20-
"github.com/LagrangeDev/LagrangeGo/utils"
2120
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
21+
"github.com/LagrangeDev/LagrangeGo/utils/io"
2222
para "github.com/fumiama/go-hide-param"
2323
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
2424
log "github.com/sirupsen/logrus"
@@ -377,7 +377,7 @@ func LoginInteract() {
377377
if uint(base.Account.Status) >= 3000 {
378378
base.Account.Status = 10
379379
}
380-
_ = cli.SetOnlineStatus(utils.Ternary(base.Account.Status >= 1000, action.SetStatus{
380+
_ = cli.SetOnlineStatus(io.Ternary(base.Account.Status >= 1000, action.SetStatus{
381381
Status: 10,
382382
ExtStatus: uint32(base.Account.Status),
383383
}, action.SetStatus{Status: uint32(base.Account.Status)}))
@@ -488,19 +488,19 @@ type protocolLogger struct{}
488488

489489
const fromProtocol = "Protocol -> "
490490

491-
func (p protocolLogger) Info(format string, arg ...any) {
491+
func (p protocolLogger) Infof(format string, arg ...any) {
492492
log.Infof(fromProtocol+format, arg...)
493493
}
494494

495-
func (p protocolLogger) Warning(format string, arg ...any) {
495+
func (p protocolLogger) Warningf(format string, arg ...any) {
496496
log.Warnf(fromProtocol+format, arg...)
497497
}
498498

499-
func (p protocolLogger) Debug(format string, arg ...any) {
499+
func (p protocolLogger) Debugf(format string, arg ...any) {
500500
log.Debugf(fromProtocol+format, arg...)
501501
}
502502

503-
func (p protocolLogger) Error(format string, arg ...any) {
503+
func (p protocolLogger) Errorf(format string, arg ...any) {
504504
log.Errorf(fromProtocol+format, arg...)
505505
}
506506

cmd/gocq/sign.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/LagrangeDev/LagrangeGo/client/auth"
1919
"github.com/LagrangeDev/LagrangeGo/client/sign"
20-
"github.com/LagrangeDev/LagrangeGo/utils"
20+
"github.com/LagrangeDev/LagrangeGo/utils/io"
2121
log "github.com/sirupsen/logrus"
2222

2323
"github.com/Mrs4s/go-cqhttp/internal/base"
@@ -113,7 +113,7 @@ func (c *signer) AddRequestHeader(header map[string]string) {
113113
func (c *signer) AddSignServer(signServers ...string) {
114114
c.lock.Lock()
115115
defer c.lock.Unlock()
116-
c.instances = append(c.instances, utils.Map[string, *remote](signServers, func(s string) *remote {
116+
c.instances = append(c.instances, io.Map(signServers, func(s string) *remote {
117117
return &remote{server: s}
118118
})...)
119119
}
@@ -122,7 +122,7 @@ func (c *signer) AddSignServer(signServers ...string) {
122122
func (c *signer) GetSignServer() []string {
123123
c.lock.RLock()
124124
defer c.lock.RUnlock()
125-
return utils.Map(c.instances, func(sign *remote) string {
125+
return io.Map(c.instances, func(sign *remote) string {
126126
return sign.server
127127
})
128128
}

coolq/api.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"github.com/LagrangeDev/LagrangeGo/client/entity"
2020
"github.com/LagrangeDev/LagrangeGo/client/sign"
2121
"github.com/LagrangeDev/LagrangeGo/message"
22-
"github.com/LagrangeDev/LagrangeGo/utils"
2322
"github.com/LagrangeDev/LagrangeGo/utils/binary"
23+
"github.com/LagrangeDev/LagrangeGo/utils/io"
2424
log "github.com/sirupsen/logrus"
2525
"github.com/tidwall/gjson"
2626

@@ -272,7 +272,7 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
272272
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
273273
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
274274
}
275-
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
275+
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, io.Ternary(folder == "", "/", folder)); err != nil {
276276
log.Warnf("上传群 %v 文件 %v 失败: %v", groupID, file, err)
277277
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
278278
}
@@ -837,9 +837,9 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
837837
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
838838
}
839839
if approve {
840-
_ = bot.Client.SetGroupRequest(req.IsFiltered, true, req.Sequence, uint32(req.EventType), req.GroupUin, "")
840+
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateAllow, req.Sequence, uint32(req.EventType), req.GroupUin, "")
841841
} else {
842-
_ = bot.Client.SetGroupRequest(req.IsFiltered, false, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
842+
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateDeny, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
843843
}
844844
return OK(nil)
845845
}
@@ -852,9 +852,9 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
852852
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
853853
}
854854
if approve {
855-
_ = bot.Client.SetGroupRequest(req.IsFiltered, true, req.Sequence, uint32(req.EventType), req.GroupUin, "")
855+
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateAllow, req.Sequence, uint32(req.EventType), req.GroupUin, "")
856856
} else {
857-
_ = bot.Client.SetGroupRequest(req.IsFiltered, false, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
857+
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateDeny, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
858858
}
859859
return OK(nil)
860860
}
@@ -1057,7 +1057,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) global
10571057
},
10581058
})
10591059

1060-
err := json.Unmarshal(utils.S2B(reply.Raw), &replySegments)
1060+
err := json.Unmarshal(io.S2B(reply.Raw), &replySegments)
10611061
if err != nil {
10621062
log.WithError(err).Warnf("处理 at_sender 过程中发生错误")
10631063
return Failed(-1, "处理 at_sender 过程中发生错误", err.Error())
@@ -1071,7 +1071,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) global
10711071
return Failed(-1, "处理 at_sender 过程中发生错误", err.Error())
10721072
}
10731073

1074-
reply = gjson.Parse(utils.B2S(modified))
1074+
reply = gjson.Parse(io.B2S(modified))
10751075
} else if at && reply.Type == gjson.String {
10761076
reply = gjson.Parse(fmt.Sprintf(
10771077
"\"[CQ:at,qq=%d]%s\"",
@@ -1141,11 +1141,12 @@ func (bot *CQBot) CQGetImage(file string) global.MSG {
11411141

11421142
if err == nil {
11431143
r := binary.NewReader(b)
1144-
r.ReadBytes(16)
1144+
r.SkipBytes(16)
11451145
msg := global.MSG{
11461146
"size": r.ReadI32(),
11471147
"filename": r.ReadStringWithLength("u32", true),
11481148
"url": r.ReadStringWithLength("u32", true),
1149+
"fileuuid": r.ReadStringWithLength("u32", true),
11491150
}
11501151
local := path.Join(global.CachePath, file+path.Ext(msg["filename"].(string)))
11511152
if !global.FileExists(local) {

coolq/bot.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
event2 "github.com/LagrangeDev/LagrangeGo/client/event"
1818
"github.com/LagrangeDev/LagrangeGo/client/sign"
1919
"github.com/LagrangeDev/LagrangeGo/message"
20-
"github.com/LagrangeDev/LagrangeGo/utils"
2120
"github.com/LagrangeDev/LagrangeGo/utils/binary"
21+
"github.com/LagrangeDev/LagrangeGo/utils/io"
2222
"github.com/RomiChan/syncx"
2323
"github.com/pkg/errors"
2424
log "github.com/sirupsen/logrus"
@@ -67,7 +67,7 @@ func (e *Event) JSONBytes() []byte {
6767
// by lazy marshalling.
6868
func (e *Event) JSONString() string {
6969
e.once.Do(e.marshal)
70-
return utils.B2S(e.buffer.Bytes())
70+
return io.B2S(e.buffer.Bytes())
7171
}
7272

7373
// NewQQBot 初始化一个QQBot实例
@@ -426,12 +426,12 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage, source message.Sou
426426
Timestamp: int64(m.Time),
427427
},
428428
GroupCode: int64(m.GroupUin),
429-
AnonymousID: func() string {
430-
if m.Sender.IsAnonymous() {
431-
return m.Sender.AnonymousInfo.AnonymousID
432-
}
433-
return ""
434-
}(),
429+
// AnonymousID: func() string {
430+
// if m.Sender.IsAnonymous() {
431+
// return m.Sender.AnonymousInfo.AnonymousID
432+
// }
433+
// return ""
434+
// }(),
435435
Content: ToMessageContent(m.Elements, source),
436436
}
437437
if replyElem != nil {
@@ -557,8 +557,5 @@ func formatMemberName(mem *entity.GroupMember) string {
557557

558558
// encodeMessageID 临时先这样, 暂时用不上
559559
func encodeMessageID(target int64, seq int32) string {
560-
return hex.EncodeToString(binary.NewWriterF(func(w *binary.Builder) {
561-
w.WriteU64(uint64(target))
562-
w.WriteU32(uint32(seq))
563-
}))
560+
return hex.EncodeToString(binary.NewBuilder().WriteU64(uint64(target)).WriteU32(uint32(seq)).ToBytes())
564561
}

coolq/converter.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,44 +73,44 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) *event {
7373
},
7474
"user_id": m.Sender.Uin,
7575
}
76-
if m.Sender.IsAnonymous() {
77-
gm["anonymous"] = global.MSG{
78-
"flag": m.Sender.AnonymousInfo.AnonymousID + "|" + m.Sender.AnonymousInfo.AnonymousNick,
79-
"id": m.Sender.Uin,
80-
"name": m.Sender.AnonymousInfo.AnonymousNick,
76+
// if m.Sender.IsAnonymous() {
77+
// gm["anonymous"] = global.MSG{
78+
// "flag": m.Sender.AnonymousInfo.AnonymousID + "|" + m.Sender.AnonymousInfo.AnonymousNick,
79+
// "id": m.Sender.Uin,
80+
// "name": m.Sender.AnonymousInfo.AnonymousNick,
81+
// }
82+
// gm["sender"].(global.MSG)["nickname"] = "匿名消息"
83+
// typ = "message/group/anonymous"
84+
// } else {
85+
mem := bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
86+
if mem == nil {
87+
log.Warnf("获取 %v 成员信息失败,尝试刷新成员列表", m.Sender.Uin)
88+
err := bot.Client.RefreshGroupMembersCache(m.GroupUin)
89+
if err != nil {
90+
log.Warnf("刷新群 %v 成员列表失败: %v", m.GroupUin, err)
91+
return nil
8192
}
82-
gm["sender"].(global.MSG)["nickname"] = "匿名消息"
83-
typ = "message/group/anonymous"
84-
} else {
85-
mem := bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
93+
mem = bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
8694
if mem == nil {
87-
log.Warnf("获取 %v 成员信息失败,尝试刷新成员列表", m.Sender.Uin)
88-
err := bot.Client.RefreshGroupMembersCache(m.GroupUin)
89-
if err != nil {
90-
log.Warnf("刷新群 %v 成员列表失败: %v", m.GroupUin, err)
91-
return nil
92-
}
93-
mem = bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
94-
if mem == nil {
95-
return nil
96-
}
95+
return nil
9796
}
98-
ms := gm["sender"].(global.MSG)
99-
role := "member"
100-
switch mem.Permission { // nolint:exhaustive
101-
case entity.Owner:
102-
role = "owner"
103-
case entity.Admin:
104-
role = "admin"
105-
case entity.Member:
106-
role = "member"
107-
}
108-
ms["role"] = role
109-
ms["nickname"] = m.Sender.Nickname
110-
ms["card"] = m.Sender.CardName
111-
// TODO 获取专属头衔
112-
ms["title"] = ""
11397
}
98+
ms := gm["sender"].(global.MSG)
99+
role := "member"
100+
switch mem.Permission { // nolint:exhaustive
101+
case entity.Owner:
102+
role = "owner"
103+
case entity.Admin:
104+
role = "admin"
105+
case entity.Member:
106+
role = "member"
107+
}
108+
ms["role"] = role
109+
ms["nickname"] = m.Sender.Nickname
110+
ms["card"] = m.Sender.CardName
111+
// TODO 获取专属头衔
112+
ms["title"] = ""
113+
// }
114114
ev := bot.event(typ, gm)
115115
ev.Time = int64(m.Time)
116116
return ev

coolq/cqcode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717

1818
"github.com/LagrangeDev/LagrangeGo/client/packets/pb/service/oidb"
1919
"github.com/LagrangeDev/LagrangeGo/message"
20-
"github.com/LagrangeDev/LagrangeGo/utils"
2120
"github.com/LagrangeDev/LagrangeGo/utils/binary"
2221
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
22+
lgrio "github.com/LagrangeDev/LagrangeGo/utils/io"
2323
b14 "github.com/fumiama/go-base16384"
2424
"github.com/segmentio/asm/base64"
2525
log "github.com/sirupsen/logrus"
@@ -940,7 +940,7 @@ func (bot *CQBot) makeImageOrVideoElem(elem msg.Element, video bool, sourceType
940940
return &msg.LocalImage{Stream: bytes.NewReader(b), URL: f}, nil
941941
}
942942
if !video && strings.HasPrefix(f, "base16384") {
943-
b, err := b14.UTF82UTF16BE(utils.S2B(strings.TrimPrefix(f, "base16384://")))
943+
b, err := b14.UTF82UTF16BE(lgrio.S2B(strings.TrimPrefix(f, "base16384://")))
944944
if err != nil {
945945
return nil, err
946946
}

coolq/event.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,14 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, source message.Source)
533533
// i.URL = u
534534
// }
535535
//}
536-
data := binary.NewWriterF(func(w *binary.Builder) {
537-
_, _ = w.Write(i.Md5)
538-
w.WritePacketString(i.FileUUID, "u32", true)
539-
w.WritePacketString(i.ImageID, "u32", true)
540-
})
536+
// 根据 https://github.com/Mrs4s/go-cqhttp/blob/a5923f179b360331786a6509eb33481e775a7bd1/coolq/event.go#L663-L669 修复
537+
data := binary.NewBuilder().
538+
WriteBytes(i.Md5).
539+
WriteU32(i.Size).
540+
WritePacketString(i.ImageID, "u32", true).
541+
WritePacketString(i.URL, "u32", true).
542+
WritePacketString(i.FileUUID, "u32", true).
543+
ToBytes()
541544
cache.Image.Insert(i.Md5, data)
542545

543546
case *message.VoiceElement:
@@ -552,14 +555,14 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, source message.Source)
552555
}
553556
}
554557
case *message.ShortVideoElement:
555-
data := binary.NewWriterF(func(w *binary.Builder) {
556-
w.WriteBool(source.SourceType == message.SourceGroup)
557-
w.WriteBytes(i.Md5)
558-
w.WriteBytes(i.Sha1)
559-
w.WritePacketString(i.Name, "u32", true)
560-
w.WritePacketString(i.UUID, "u32", true)
561-
w.WriteU32(uint32(source.PrimaryID))
562-
})
558+
data := binary.NewBuilder().
559+
WriteBool(source.SourceType == message.SourceGroup).
560+
WriteBytes(i.Md5).
561+
WriteBytes(i.Sha1).
562+
WritePacketString(i.Name, "u32", true).
563+
WritePacketString(i.UUID, "u32", true).
564+
WriteU32(uint32(source.PrimaryID)).
565+
ToBytes()
563566
filename := hex.EncodeToString(i.Md5) + ".video"
564567
cache.Video.Insert(i.Md5, data)
565568
if source.SourceType == message.SourceGroup {

0 commit comments

Comments
 (0)