Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/pattern-matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
RikaCelery committed Oct 12, 2024
2 parents 2e7bdfb + 5d23e7c commit 9403051
Show file tree
Hide file tree
Showing 25 changed files with 196 additions and 186 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go-vet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@master
with:
go-version: ^1.20
go-version: "1.20"

- name: Check out code into the Go module directory
uses: actions/checkout@master
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@master
with:
go-version: ^1.20
go-version: "1.20"

- name: Check out code into the Go module directory
uses: actions/checkout@master
Expand Down
17 changes: 7 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,38 @@ linters:
fast: false
enable:
- bodyclose
#- deadcode
#- depguard
- dogsled
- dupl
- errcheck
- exportloopref
- exhaustive
#- funlen
#- goconst
#- gocritic
- gocritic
#- gocyclo
- gofumpt
- gofmt
- goimports
- goprintffuncname
#- gosec
- gosimple
- govet
- ineffassign
- misspell
#- misspell
- nolintlint
- rowserrcheck
- staticcheck
#- structcheck
#- stylecheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
#- varcheck
- whitespace
- prealloc
- predeclared
- asciicheck
#- revive
- revive
- forbidigo
- makezero
#- interfacer


run:
Expand All @@ -57,6 +53,7 @@ run:
deadline: 5m
issues-exit-code: 1
tests: false
go: '1.20'

# output configuration options
output:
Expand Down
2 changes: 1 addition & 1 deletion all_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ctx *Ctx) CallAction(action string, params Params) APIResponse {
Action: action,
Params: params,
}
rsp, err := ctx.caller.CallApi(req)
rsp, err := ctx.caller.CallAPI(req)
if err != nil {
log.Errorln("[api] 调用", action, "时出现错误: ", err)
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (ctx *Ctx) GetMessage(messageID interface{}) Message {
}).Data
m := Message{
Elements: message.ParseMessage(helper.StringToBytes(rsp.Get("message").Raw)),
MessageId: message.NewMessageIDFromInteger(rsp.Get("message_id").Int()),
MessageID: message.NewMessageIDFromInteger(rsp.Get("message_id").Int()),
MessageType: rsp.Get("message_type").String(),
Sender: &User{},
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (ctx *Ctx) SetGroupName(groupID int64, groupName string) {

// SetThisGroupName 设置本群名
// https://github.com/botuniverse/onebot-11/blob/master/api/public.md#set_group_name-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D
func (ctx *Ctx) SetThisGroupName(groupID int64, groupName string) {
func (ctx *Ctx) SetThisGroupName(groupName string) {
ctx.SetGroupName(ctx.Event.GroupID, groupName)
}

Expand Down Expand Up @@ -682,20 +682,20 @@ func (ctx *Ctx) GetThisGroupFilesByFolder(folderID string) gjson.Result {
return ctx.GetGroupFilesByFolder(ctx.Event.GroupID, folderID)
}

// GetGroupFileUrl 获取群文件资源链接
// GetGroupFileURL 获取群文件资源链接
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%96%87%E4%BB%B6%E8%B5%84%E6%BA%90%E9%93%BE%E6%8E%A5
func (ctx *Ctx) GetGroupFileUrl(groupID, busid int64, fileID string) string {
func (ctx *Ctx) GetGroupFileURL(groupID, busid int64, fileID string) string {
return ctx.CallAction("get_group_file_url", Params{
"group_id": groupID,
"file_id": fileID,
"busid": busid,
}).Data.Get("url").Str
}

// GetThisGroupFileUrl 获取本群文件资源链接
// GetThisGroupFileURL 获取本群文件资源链接
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%96%87%E4%BB%B6%E8%B5%84%E6%BA%90%E9%93%BE%E6%8E%A5
func (ctx *Ctx) GetThisGroupFileUrl(busid int64, fileID string) string {
return ctx.GetGroupFileUrl(ctx.Event.GroupID, busid, fileID)
func (ctx *Ctx) GetThisGroupFileURL(busid int64, fileID string) string {
return ctx.GetGroupFileURL(ctx.Event.GroupID, busid, fileID)
}

// UploadGroupFile 上传群文件
Expand Down
24 changes: 12 additions & 12 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type Config struct {
// APICallers 所有的APICaller列表, 通过self-ID映射
var APICallers callerMap

// APICaller is the interface of CallApi
// APICaller is the interface of CallAPI
type APICaller interface {
CallApi(request APIRequest) (APIResponse, error)
CallAPI(request APIRequest) (APIResponse, error)
}

// Driver 与OneBot通信的驱动,使用driver.DefaultWebSocketDriver
Expand Down Expand Up @@ -125,18 +125,18 @@ func RunAndBlock(op *Config, preblock func()) {
}

var (
triggeredMessages = ttl.NewCache[int64, []message.MessageID](time.Minute * 5)
triggeredMessages = ttl.NewCache[int64, []message.ID](time.Minute * 5)
triggeredMessagesMu = sync.Mutex{}
)

type messageLogger struct {
msgid message.MessageID
msgid message.ID
caller APICaller
}

// CallApi 记录被触发的回复消息
func (m *messageLogger) CallApi(request APIRequest) (rsp APIResponse, err error) {
rsp, err = m.caller.CallApi(request)
// CallAPI 记录被触发的回复消息
func (m *messageLogger) CallAPI(request APIRequest) (rsp APIResponse, err error) {
rsp, err = m.caller.CallAPI(request)
if err != nil {
return
}
Expand All @@ -156,7 +156,7 @@ func (m *messageLogger) CallApi(request APIRequest) (rsp APIResponse, err error)
}

// GetTriggeredMessages 获取被 id 消息触发的回复消息 id
func GetTriggeredMessages(id message.MessageID) []message.MessageID {
func GetTriggeredMessages(id message.ID) []message.ID {
triggeredMessagesMu.Lock()
defer triggeredMessagesMu.Unlock()
return triggeredMessages.Get(id.ID())
Expand All @@ -167,7 +167,7 @@ func processEventAsync(response []byte, caller APICaller, maxwait time.Duration)
var event Event
_ = json.Unmarshal(response, &event)
event.RawEvent = gjson.Parse(helper.BytesToString(response))
var msgid message.MessageID
var msgid message.ID
messageID, err := strconv.ParseInt(helper.BytesToString(event.RawMessageID), 10, 64)
if err == nil {
event.MessageID = messageID
Expand Down Expand Up @@ -397,7 +397,7 @@ func preprocessMessageEvent(e *Event) {
msgs := message.ParseMessage(e.NativeMessage)

if len(msgs) > 0 {
filtered := make([]message.MessageSegment, 0, len(msgs))
filtered := make([]message.Segment, 0, len(msgs))
// trim space after at and remove empty text segment
for i := range msgs {
if i < len(msgs)-1 && msgs[i].Type == "at" && msgs[i+1].Type == "text" {
Expand Down Expand Up @@ -486,12 +486,12 @@ func RangeBot(iter func(id int64, ctx *Ctx) bool) {
// GetFirstSuperUser 在 qqs 中获得 SuperUsers 列表的首个 qq
//
// 找不到返回 -1
func (c *Config) GetFirstSuperUser(qqs ...int64) int64 {
func (op *Config) GetFirstSuperUser(qqs ...int64) int64 {
m := make(map[int64]struct{}, len(qqs)*4)
for _, qq := range qqs {
m[qq] = struct{}{}
}
for _, qq := range c.SuperUsers {
for _, qq := range op.SuperUsers {
if _, ok := m[qq]; ok {
return qq
}
Expand Down
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (ctx *Ctx) CheckSession() Rule {
}

// Send 快捷发送消息/合并转发
func (ctx *Ctx) Send(msg interface{}) message.MessageID {
func (ctx *Ctx) Send(msg interface{}) message.ID {
event := ctx.Event
m, ok := msg.(message.Message)
if !ok {
Expand All @@ -112,7 +112,7 @@ func (ctx *Ctx) Send(msg interface{}) message.MessageID {
}

// SendChain 快捷发送消息/合并转发-消息链
func (ctx *Ctx) SendChain(msg ...message.MessageSegment) message.MessageID {
func (ctx *Ctx) SendChain(msg ...message.Segment) message.ID {
return ctx.Send((message.Message)(msg))
}

Expand All @@ -126,8 +126,8 @@ func (ctx *Ctx) Echo(response []byte) {
}

// FutureEvent ...
func (ctx *Ctx) FutureEvent(Type string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(Type, rule...)
func (ctx *Ctx) FutureEvent(typ string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(typ, rule...)
}

// Get ..
Expand Down
2 changes: 1 addition & 1 deletion docs/content/start/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bookComments: true

### 安装Go

首先需要安装Go(支持版本1.18+), 你可以在
首先需要安装Go(支持版本1.20+), 你可以在
{{< button href="https://golang.org/dl" >}} Go官网{{< /button >}}
或者{{< button href="https://golang.google.cn/dl">}}Go中国镜像站{{< /button >}}
(国内用户推荐)找到对应的安装包
Expand Down
18 changes: 9 additions & 9 deletions driver/wsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ type WSClient struct {
conn *websocket.Conn
mu sync.Mutex // 写锁
seqMap seqSyncMap
Url string // ws连接地址
URL string // ws连接地址
AccessToken string
selfID int64
}

// NewWebSocketClient 默认Driver,使用正向WS通信
func NewWebSocketClient(url, accessToken string) *WSClient {
return &WSClient{
Url: url,
URL: url,
AccessToken: accessToken,
}
}

// Connect 连接ws服务端
func (ws *WSClient) Connect() {
log.Infof("[ws] 开始尝试连接到Websocket服务器: %v", ws.Url)
log.Infof("[ws] 开始尝试连接到Websocket服务器: %v", ws.URL)
header := http.Header{
"X-Client-Role": []string{"Universal"},
"User-Agent": []string{"ZeroBot/1.6.3"},
Expand All @@ -53,7 +53,7 @@ func (ws *WSClient) Connect() {
header["Authorization"] = []string{"Bearer " + ws.AccessToken}
}

network, address := resolveURI(ws.Url)
network, address := resolveURI(ws.URL)
dialer := websocket.Dialer{
NetDial: func(_, addr string) (net.Conn, error) {
if network == "unix" {
Expand All @@ -73,7 +73,7 @@ func (ws *WSClient) Connect() {
for {
conn, res, err := dialer.Dial(address, header)
if err != nil {
log.Warnf("[ws] 连接到Websocket服务器 %v 时出现错误: %v", ws.Url, err)
log.Warnf("[ws] 连接到Websocket服务器 %v 时出现错误: %v", ws.URL, err)
time.Sleep(2 * time.Second) // 等待两秒后重新连接
continue
}
Expand All @@ -84,13 +84,13 @@ func (ws *WSClient) Connect() {
}
err = ws.conn.ReadJSON(&rsp)
if err != nil {
log.Warnf("[ws] 与Websocket服务器 %v 握手时出现错误: %v", ws.Url, err)
log.Warnf("[ws] 与Websocket服务器 %v 握手时出现错误: %v", ws.URL, err)
time.Sleep(2 * time.Second) // 等待两秒后重新连接
continue
}
ws.selfID = rsp.SelfID
zero.APICallers.Store(ws.selfID, ws) // 添加Caller到 APICaller list...
log.Infof("[ws] 连接Websocket服务器: %s 成功, 账号: %d", ws.Url, rsp.SelfID)
log.Infof("[ws] 连接Websocket服务器: %s 成功, 账号: %d", ws.URL, rsp.SelfID)
break
}
}
Expand Down Expand Up @@ -137,8 +137,8 @@ func (ws *WSClient) nextSeq() uint64 {
return atomic.AddUint64(&ws.seq, 1)
}

// CallApi 发送ws请求
func (ws *WSClient) CallApi(req zero.APIRequest) (zero.APIResponse, error) {
// CallAPI 发送ws请求
func (ws *WSClient) CallAPI(req zero.APIRequest) (zero.APIResponse, error) {
ch := make(chan zero.APIResponse, 1)
req.Echo = ws.nextSeq()
ws.seqMap.Store(req.Echo, ch)
Expand Down
18 changes: 9 additions & 9 deletions driver/wsserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// WSServer ...
type WSServer struct {
Url string // ws连接地址
URL string // ws连接地址
AccessToken string
lstn net.Listener
caller chan *WSSCaller
Expand All @@ -34,7 +34,7 @@ type WSServer struct {
// UnmarshalJSON init WSServer with waitn=16
func (wss *WSServer) UnmarshalJSON(data []byte) error {
type jsoncfg struct {
Url string // ws连接地址
URL string // ws连接地址
AccessToken string
}
err := json.Unmarshal(data, (*jsoncfg)(unsafe.Pointer(wss)))
Expand All @@ -48,7 +48,7 @@ func (wss *WSServer) UnmarshalJSON(data []byte) error {
// NewWebSocketServer 使用反向WS通信
func NewWebSocketServer(waitn int, url, accessToken string) *WSServer {
return &WSServer{
Url: url,
URL: url,
AccessToken: accessToken,
caller: make(chan *WSSCaller, waitn),
}
Expand All @@ -64,14 +64,14 @@ type WSSCaller struct {
}

var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
CheckOrigin: func(_ *http.Request) bool {
return true
},
}

// Connect 监听ws服务
func (wss *WSServer) Connect() {
network, address := resolveURI(wss.Url)
network, address := resolveURI(wss.URL)
uri, err := url.Parse(address)
if err == nil && uri.Scheme != "" {
address = uri.Host
Expand Down Expand Up @@ -132,7 +132,7 @@ func (wss *WSServer) any(w http.ResponseWriter, r *http.Request) {
}
err = conn.ReadJSON(&rsp)
if err != nil {
log.Warnf("[wss] 与Websocket服务器 %v 握手时出现错误: %v", wss.Url, err)
log.Warnf("[wss] 与Websocket服务器 %v 握手时出现错误: %v", wss.URL, err)
return
}

Expand All @@ -141,7 +141,7 @@ func (wss *WSServer) any(w http.ResponseWriter, r *http.Request) {
selfID: rsp.SelfID,
}
zero.APICallers.Store(rsp.SelfID, c) // 添加Caller到 APICaller list...
log.Infof("[wss] 连接Websocket服务器: %s 成功, 账号: %d", wss.Url, rsp.SelfID)
log.Infof("[wss] 连接Websocket服务器: %s 成功, 账号: %d", wss.URL, rsp.SelfID)
wss.caller <- c
}

Expand Down Expand Up @@ -208,8 +208,8 @@ func (wssc *WSSCaller) nextSeq() uint64 {
return atomic.AddUint64(&wssc.seq, 1)
}

// CallApi 发送ws请求
func (wssc *WSSCaller) CallApi(req zero.APIRequest) (zero.APIResponse, error) {
// CallAPI 发送ws请求
func (wssc *WSSCaller) CallAPI(req zero.APIRequest) (zero.APIResponse, error) {
ch := make(chan zero.APIResponse, 1)
req.Echo = wssc.nextSeq()
wssc.seqMap.Store(req.Echo, ch)
Expand Down
Loading

0 comments on commit 9403051

Please sign in to comment.