Skip to content

Commit 595ae3b

Browse files
authored
Merge pull request #5 from FloatTech/dev
sync
2 parents c0b19e8 + 6f355e7 commit 595ae3b

35 files changed

+417
-1250
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup Go environment
2929
uses: actions/setup-go@v2.1.3
3030
with:
31-
go-version: 1.18
31+
go-version: 1.19
3232
- name: Cache downloaded module
3333
uses: actions/cache@v2
3434
with:

.github/workflows/golint.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ jobs:
1212
- name: Setup Go environment
1313
uses: actions/setup-go@v2.1.3
1414
with:
15-
go-version: 1.18
15+
go-version: 1.19
1616

1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v2
1919
with:
2020
version: latest
2121

22-
- name: Static Check
23-
uses: dominikh/staticcheck-action@v1.2.0
24-
with:
25-
install-go: false
26-
version: "2022.1"
27-
2822
- name: Tests
2923
run: |
3024
go test $(go list ./...)

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Set up Go
1818
uses: actions/setup-go@v2
1919
with:
20-
go-version: '1.18'
20+
go-version: '1.19'
2121

2222
- name: Run GoReleaser
2323
uses: goreleaser/goreleaser-action@v2

.golangci.yml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,25 @@ linters:
2121
disable-all: true
2222
fast: false
2323
enable:
24-
#- bodyclose
25-
#- deadcode
26-
#- depguard
27-
#- dogsled
24+
- bodyclose
25+
- durationcheck
2826
- gofmt
2927
- goimports
3028
- errcheck
3129
- exportloopref
3230
- exhaustive
3331
- bidichk
3432
- gocritic
35-
#- gosimple
33+
- gosimple
3634
- govet
3735
- ineffassign
3836
#- nolintlint
39-
#- rowserrcheck
40-
#- staticcheck
41-
- structcheck
42-
#- stylecheck
37+
- staticcheck
38+
- stylecheck
4339
- unconvert
44-
#- unparam
45-
#- unused
46-
- varcheck
40+
- usestdlibvars
41+
- unparam
42+
- unused
4743
- whitespace
4844
- prealloc
4945
- predeclared

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.18-alpine AS builder
1+
FROM golang:1.19-alpine AS builder
22

33
RUN go env -w GO111MODULE=auto \
44
&& go env -w CGO_ENABLED=0 \

README.md

Lines changed: 54 additions & 54 deletions
Large diffs are not rendered by default.

cmd/gocq/login.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ package gocq
33
import (
44
"bufio"
55
"bytes"
6+
"fmt"
67
"image"
78
"image/png"
89
"os"
910
"strings"
1011
"time"
1112

1213
"github.com/Mrs4s/MiraiGo/client"
14+
"github.com/Mrs4s/MiraiGo/utils"
1315
"github.com/mattn/go-colorable"
1416
"github.com/pkg/errors"
1517
log "github.com/sirupsen/logrus"
18+
"github.com/tidwall/gjson"
1619

1720
"github.com/Mrs4s/go-cqhttp/global"
1821
)
@@ -142,7 +145,19 @@ func loginResponseProcessor(res *client.LoginResponse) error {
142145
var text string
143146
switch res.Error {
144147
case client.SliderNeededError:
145-
log.Warnf("登录需要滑条验证码, 请使用手机QQ扫描二维码以继续登录.")
148+
log.Warnf("登录需要滑条验证码, 请选择验证方式: ")
149+
log.Warnf("1. 使用浏览器抓取滑条并登录")
150+
log.Warnf("2. 使用手机QQ扫码验证 (需要手Q和gocq在同一网络下).")
151+
log.Warn("请输入(1 - 2) (将在10秒后自动选择1):")
152+
text = readLineTimeout(time.Second*10, "1")
153+
if strings.Contains(text, "1") {
154+
ticket := getTicket(res.VerifyUrl)
155+
if ticket == "" {
156+
os.Exit(0)
157+
}
158+
res, err = cli.SubmitTicket(ticket)
159+
continue
160+
}
146161
cli.Disconnect()
147162
cli.Release()
148163
cli = client.NewClientEmpty()
@@ -192,8 +207,7 @@ func loginResponseProcessor(res *client.LoginResponse) error {
192207
msg := res.ErrorMessage
193208
if strings.Contains(msg, "版本") {
194209
msg = "密码错误或账号被冻结"
195-
}
196-
if strings.Contains(msg, "冻结") {
210+
} else if strings.Contains(msg, "冻结") {
197211
log.Fatalf("账号被冻结")
198212
}
199213
log.Warnf("登录失败: %v", msg)
@@ -203,3 +217,40 @@ func loginResponseProcessor(res *client.LoginResponse) error {
203217
}
204218
}
205219
}
220+
221+
func getTicket(u string) (str string) {
222+
id := utils.RandomString(8)
223+
log.Warnf("请前往该地址验证 -> %v <- 或输入手动抓取的 ticket:(Enter 提交)", strings.ReplaceAll(u, "https://ssl.captcha.qq.com/template/wireless_mqq_captcha.html?", fmt.Sprintf("https://captcha.go-cqhttp.org/captcha?id=%v&", id)))
224+
manual := make(chan string, 1)
225+
go func() {
226+
manual <- readLine()
227+
}()
228+
ticker := time.NewTicker(time.Second)
229+
defer ticker.Stop()
230+
for count := 120; count > 0; count-- {
231+
select {
232+
case <-ticker.C:
233+
str = fetchCaptcha(id)
234+
if str != "" {
235+
return
236+
}
237+
case str = <-manual:
238+
return
239+
}
240+
}
241+
log.Warnf("验证超时")
242+
return ""
243+
}
244+
245+
func fetchCaptcha(id string) string {
246+
data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id)
247+
if err != nil {
248+
log.Warnf("获取 Ticket 时出现错误: %v", err)
249+
return ""
250+
}
251+
g := gjson.ParseBytes(data)
252+
if g.Get("ticket").Exists() {
253+
return g.Get("ticket").String()
254+
}
255+
return ""
256+
}

coolq/api.go

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,30 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
601601
return OK(nil)
602602
}
603603

604+
// CQUploadPrivateFile 扩展API-上传私聊文件
605+
//
606+
// @route(upload_private_file)
607+
func (bot *CQBot) CQUploadPrivateFile(userID int64, file, name string) global.MSG {
608+
target := message.Source{
609+
SourceType: message.SourcePrivate,
610+
PrimaryID: userID,
611+
}
612+
fileBody, err := os.Open(file)
613+
if err != nil {
614+
log.Warnf("上传私聊文件 %v 失败: %+v", file, err)
615+
return Failed(100, "OPEN_FILE_ERROR", "打开文件失败")
616+
}
617+
localFile := &client.LocalFile{
618+
FileName: name,
619+
Body: fileBody,
620+
}
621+
if err := bot.Client.UploadFile(target, localFile); err != nil {
622+
log.Warnf("上传私聊 %v 文件 %v 失败: %+v", userID, file, err)
623+
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
624+
}
625+
return OK(nil)
626+
}
627+
604628
// CQGroupFileCreateFolder 拓展API-创建群文件文件夹
605629
//
606630
// @route(create_group_file_folder)
@@ -741,9 +765,9 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape b
741765
}
742766
}
743767
fixAt(elem)
744-
mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
745-
if mid == -1 {
746-
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
768+
mid, err := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
769+
if err != nil {
770+
return Failed(100, "SEND_MSG_API_ERROR", err.Error())
747771
}
748772
log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(m.String()), mid)
749773
return OK(global.MSG{"message_id": mid})
@@ -808,7 +832,12 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
808832
groupID := target
809833
source := message.Source{SourceType: sourceType, PrimaryID: target}
810834
if sourceType == message.SourcePrivate {
811-
groupID = 0
835+
// ios 设备的合并转发来源群号不能为 0
836+
if len(bot.Client.GroupList) == 0 {
837+
groupID = 1
838+
} else {
839+
groupID = bot.Client.GroupList[0].Uin
840+
}
812841
}
813842
builder := bot.Client.NewForwardMessageBuilder(groupID)
814843

@@ -849,17 +878,21 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
849878
}
850879
if e.Get("data.id").Exists() {
851880
i := e.Get("data.id").Int()
852-
m, _ := db.GetGroupMessageByGlobalID(int32(i))
881+
m, _ := db.GetMessageByGlobalID(int32(i))
853882
if m != nil {
854-
msgTime := m.Attribute.Timestamp
883+
mSource := message.SourcePrivate
884+
if m.GetType() == "group" {
885+
mSource = message.SourceGroup
886+
}
887+
msgTime := m.GetAttribute().Timestamp
855888
if msgTime == 0 {
856889
msgTime = ts.Unix()
857890
}
858891
return &message.ForwardNode{
859-
SenderId: m.Attribute.SenderUin,
860-
SenderName: m.Attribute.SenderName,
892+
SenderId: m.GetAttribute().SenderUin,
893+
SenderName: m.GetAttribute().SenderName,
861894
Time: int32(msgTime),
862-
Message: resolveElement(bot.ConvertContentMessage(m.Content, message.SourceGroup)),
895+
Message: resolveElement(bot.ConvertContentMessage(m.GetContent(), mSource)),
863896
}
864897
}
865898
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
@@ -892,7 +925,7 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
892925
}
893926
}
894927
}
895-
content := bot.ConvertObjectMessage(c, message.SourceGroup)
928+
content := bot.ConvertObjectMessage(c, sourceType)
896929
if uin != 0 && name != "" && len(content) > 0 {
897930
return &message.ForwardNode{
898931
SenderId: uin,
@@ -944,8 +977,11 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
944977
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
945978
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
946979
}
980+
mid := bot.InsertGroupMessage(ret)
981+
log.Infof("发送群 %v(%v) 的合并转发消息: %v (%v)", groupID, groupID, limitedString(m.String()), mid)
947982
return OK(global.MSG{
948-
"message_id": bot.InsertGroupMessage(ret),
983+
"message_id": mid,
984+
"forward_id": fe.ResId,
949985
})
950986
}
951987

@@ -967,7 +1003,11 @@ func (bot *CQBot) CQSendPrivateForwardMessage(userID int64, m gjson.Result) glob
9671003
log.Warnf("合并转发(好友)消息发送失败: 账号可能被风控.")
9681004
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
9691005
}
970-
return OK(global.MSG{"message_id": mid})
1006+
log.Infof("发送好友 %v(%v) 的合并转发消息: %v (%v)", userID, userID, limitedString(m.String()), mid)
1007+
return OK(global.MSG{
1008+
"message_id": mid,
1009+
"forward_id": fe.ResId,
1010+
})
9711011
}
9721012

9731013
// CQSendPrivateMessage 发送私聊消息
@@ -1081,6 +1121,23 @@ func (bot *CQBot) CQSetGroupMemo(groupID int64, msg, img string) global.MSG {
10811121
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
10821122
}
10831123

1124+
// CQDelGroupMemo 扩展API-删除群公告
1125+
// @route(_del_group_notice)
1126+
// @rename(fid->notice_id)
1127+
func (bot *CQBot) CQDelGroupMemo(groupID int64, fid string) global.MSG {
1128+
if g := bot.Client.FindGroup(groupID); g != nil {
1129+
if g.SelfPermission() == client.Member {
1130+
return Failed(100, "PERMISSION_DENIED", "权限不足")
1131+
}
1132+
err := bot.Client.DelGroupNotice(groupID, fid)
1133+
if err != nil {
1134+
return Failed(100, "DELETE_NOTICE_ERROR", err.Error())
1135+
}
1136+
return OK(nil)
1137+
}
1138+
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
1139+
}
1140+
10841141
// CQSetGroupKick 群组踢人
10851142
//
10861143
// https://git.io/Jtz1V
@@ -1364,6 +1421,7 @@ func (bot *CQBot) CQGetStrangerInfo(userID int64) global.MSG {
13641421
"age": info.Age,
13651422
"level": info.Level,
13661423
"login_days": info.LoginDays,
1424+
"vip_level": info.VipLevel,
13671425
})
13681426
}
13691427

0 commit comments

Comments
 (0)