Skip to content

Commit

Permalink
Merge pull request #5 from FloatTech/dev
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
fumiama authored Sep 17, 2022
2 parents c0b19e8 + 6f355e7 commit 595ae3b
Show file tree
Hide file tree
Showing 35 changed files with 417 additions and 1,250 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Go environment
uses: actions/[email protected]
with:
go-version: 1.18
go-version: 1.19
- name: Cache downloaded module
uses: actions/cache@v2
with:
Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/golint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ jobs:
- name: Setup Go environment
uses: actions/[email protected]
with:
go-version: 1.18
go-version: 1.19

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest

- name: Static Check
uses: dominikh/[email protected]
with:
install-go: false
version: "2022.1"

- name: Tests
run: |
go test $(go list ./...)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.18'
go-version: '1.19'

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
20 changes: 8 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@ linters:
disable-all: true
fast: false
enable:
#- bodyclose
#- deadcode
#- depguard
#- dogsled
- bodyclose
- durationcheck
- gofmt
- goimports
- errcheck
- exportloopref
- exhaustive
- bidichk
- gocritic
#- gosimple
- gosimple
- govet
- ineffassign
#- nolintlint
#- rowserrcheck
#- staticcheck
- structcheck
#- stylecheck
- staticcheck
- stylecheck
- unconvert
#- unparam
#- unused
- varcheck
- usestdlibvars
- unparam
- unused
- whitespace
- prealloc
- predeclared
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18-alpine AS builder
FROM golang:1.19-alpine AS builder

RUN go env -w GO111MODULE=auto \
&& go env -w CGO_ENABLED=0 \
Expand Down
108 changes: 54 additions & 54 deletions README.md

Large diffs are not rendered by default.

57 changes: 54 additions & 3 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package gocq
import (
"bufio"
"bytes"
"fmt"
"image"
"image/png"
"os"
"strings"
"time"

"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/utils"
"github.com/mattn/go-colorable"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"

"github.com/Mrs4s/go-cqhttp/global"
)
Expand Down Expand Up @@ -142,7 +145,19 @@ func loginResponseProcessor(res *client.LoginResponse) error {
var text string
switch res.Error {
case client.SliderNeededError:
log.Warnf("登录需要滑条验证码, 请使用手机QQ扫描二维码以继续登录.")
log.Warnf("登录需要滑条验证码, 请选择验证方式: ")
log.Warnf("1. 使用浏览器抓取滑条并登录")
log.Warnf("2. 使用手机QQ扫码验证 (需要手Q和gocq在同一网络下).")
log.Warn("请输入(1 - 2) (将在10秒后自动选择1):")
text = readLineTimeout(time.Second*10, "1")
if strings.Contains(text, "1") {
ticket := getTicket(res.VerifyUrl)
if ticket == "" {
os.Exit(0)
}
res, err = cli.SubmitTicket(ticket)
continue
}
cli.Disconnect()
cli.Release()
cli = client.NewClientEmpty()
Expand Down Expand Up @@ -192,8 +207,7 @@ func loginResponseProcessor(res *client.LoginResponse) error {
msg := res.ErrorMessage
if strings.Contains(msg, "版本") {
msg = "密码错误或账号被冻结"
}
if strings.Contains(msg, "冻结") {
} else if strings.Contains(msg, "冻结") {
log.Fatalf("账号被冻结")
}
log.Warnf("登录失败: %v", msg)
Expand All @@ -203,3 +217,40 @@ func loginResponseProcessor(res *client.LoginResponse) error {
}
}
}

func getTicket(u string) (str string) {
id := utils.RandomString(8)
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)))
manual := make(chan string, 1)
go func() {
manual <- readLine()
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for count := 120; count > 0; count-- {
select {
case <-ticker.C:
str = fetchCaptcha(id)
if str != "" {
return
}
case str = <-manual:
return
}
}
log.Warnf("验证超时")
return ""
}

func fetchCaptcha(id string) string {
data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id)
if err != nil {
log.Warnf("获取 Ticket 时出现错误: %v", err)
return ""
}
g := gjson.ParseBytes(data)
if g.Get("ticket").Exists() {
return g.Get("ticket").String()
}
return ""
}
82 changes: 70 additions & 12 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,30 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
return OK(nil)
}

// CQUploadPrivateFile 扩展API-上传私聊文件
//
// @route(upload_private_file)
func (bot *CQBot) CQUploadPrivateFile(userID int64, file, name string) global.MSG {
target := message.Source{
SourceType: message.SourcePrivate,
PrimaryID: userID,
}
fileBody, err := os.Open(file)
if err != nil {
log.Warnf("上传私聊文件 %v 失败: %+v", file, err)
return Failed(100, "OPEN_FILE_ERROR", "打开文件失败")
}
localFile := &client.LocalFile{
FileName: name,
Body: fileBody,
}
if err := bot.Client.UploadFile(target, localFile); err != nil {
log.Warnf("上传私聊 %v 文件 %v 失败: %+v", userID, file, err)
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
}
return OK(nil)
}

// CQGroupFileCreateFolder 拓展API-创建群文件文件夹
//
// @route(create_group_file_folder)
Expand Down Expand Up @@ -741,9 +765,9 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape b
}
}
fixAt(elem)
mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
mid, err := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
if err != nil {
return Failed(100, "SEND_MSG_API_ERROR", err.Error())
}
log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(m.String()), mid)
return OK(global.MSG{"message_id": mid})
Expand Down Expand Up @@ -808,7 +832,12 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
groupID := target
source := message.Source{SourceType: sourceType, PrimaryID: target}
if sourceType == message.SourcePrivate {
groupID = 0
// ios 设备的合并转发来源群号不能为 0
if len(bot.Client.GroupList) == 0 {
groupID = 1
} else {
groupID = bot.Client.GroupList[0].Uin
}
}
builder := bot.Client.NewForwardMessageBuilder(groupID)

Expand Down Expand Up @@ -849,17 +878,21 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
}
if e.Get("data.id").Exists() {
i := e.Get("data.id").Int()
m, _ := db.GetGroupMessageByGlobalID(int32(i))
m, _ := db.GetMessageByGlobalID(int32(i))
if m != nil {
msgTime := m.Attribute.Timestamp
mSource := message.SourcePrivate
if m.GetType() == "group" {
mSource = message.SourceGroup
}
msgTime := m.GetAttribute().Timestamp
if msgTime == 0 {
msgTime = ts.Unix()
}
return &message.ForwardNode{
SenderId: m.Attribute.SenderUin,
SenderName: m.Attribute.SenderName,
SenderId: m.GetAttribute().SenderUin,
SenderName: m.GetAttribute().SenderName,
Time: int32(msgTime),
Message: resolveElement(bot.ConvertContentMessage(m.Content, message.SourceGroup)),
Message: resolveElement(bot.ConvertContentMessage(m.GetContent(), mSource)),
}
}
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
Expand Down Expand Up @@ -892,7 +925,7 @@ func (bot *CQBot) uploadForwardElement(m gjson.Result, target int64, sourceType
}
}
}
content := bot.ConvertObjectMessage(c, message.SourceGroup)
content := bot.ConvertObjectMessage(c, sourceType)
if uin != 0 && name != "" && len(content) > 0 {
return &message.ForwardNode{
SenderId: uin,
Expand Down Expand Up @@ -944,8 +977,11 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) globa
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
}
mid := bot.InsertGroupMessage(ret)
log.Infof("发送群 %v(%v) 的合并转发消息: %v (%v)", groupID, groupID, limitedString(m.String()), mid)
return OK(global.MSG{
"message_id": bot.InsertGroupMessage(ret),
"message_id": mid,
"forward_id": fe.ResId,
})
}

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

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

// CQDelGroupMemo 扩展API-删除群公告
// @route(_del_group_notice)
// @rename(fid->notice_id)
func (bot *CQBot) CQDelGroupMemo(groupID int64, fid string) global.MSG {
if g := bot.Client.FindGroup(groupID); g != nil {
if g.SelfPermission() == client.Member {
return Failed(100, "PERMISSION_DENIED", "权限不足")
}
err := bot.Client.DelGroupNotice(groupID, fid)
if err != nil {
return Failed(100, "DELETE_NOTICE_ERROR", err.Error())
}
return OK(nil)
}
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
}

// CQSetGroupKick 群组踢人
//
// https://git.io/Jtz1V
Expand Down Expand Up @@ -1364,6 +1421,7 @@ func (bot *CQBot) CQGetStrangerInfo(userID int64) global.MSG {
"age": info.Age,
"level": info.Level,
"login_days": info.LoginDays,
"vip_level": info.VipLevel,
})
}

Expand Down
Loading

0 comments on commit 595ae3b

Please sign in to comment.