From 7526ee8e9d9fa98d595ed7631137f4192efa26ad Mon Sep 17 00:00:00 2001 From: RikaCelery Date: Sun, 13 Oct 2024 00:30:47 +0800 Subject: [PATCH] chore: make lint happy --- extension/rate/rate.go | 2 +- extension/single/single.go | 1 + pattern.go | 49 +++++++++++++++++++------------------- rules.go | 6 ++--- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/extension/rate/rate.go b/extension/rate/rate.go index f176e54..24720f3 100644 --- a/extension/rate/rate.go +++ b/extension/rate/rate.go @@ -26,7 +26,7 @@ func NewManager[K comparable](interval time.Duration, burst int) *LimiterManager } // Delete 删除对应限速器 -func (l *LimiterManager[K]) Delete(key K) { +func (l *LimiterManager[K]) Delete(key K) { l.limiters.Delete(key) } diff --git a/extension/single/single.go b/extension/single/single.go index af52909..e4304fb 100644 --- a/extension/single/single.go +++ b/extension/single/single.go @@ -4,6 +4,7 @@ import ( "runtime" "github.com/RomiChan/syncx" + zero "github.com/wdvxdr1123/ZeroBot" ) diff --git a/pattern.go b/pattern.go index 7f300d3..a0ee04b 100644 --- a/pattern.go +++ b/pattern.go @@ -1,9 +1,10 @@ package zero import ( - "github.com/wdvxdr1123/ZeroBot/message" "regexp" "strings" + + "github.com/wdvxdr1123/ZeroBot/message" ) type Pattern []PatternSegment @@ -16,7 +17,7 @@ func NewPattern() *Pattern { type PatternSegment struct { Type string Optional bool - Parse func(msg *message.MessageSegment) *PatternParsed + Parse func(msg *message.Segment) *PatternParsed } // SetOptional set previous segment is optional, is v is empty, Optional will be true @@ -37,7 +38,7 @@ func (p *Pattern) SetOptional(v ...bool) *Pattern { type PatternParsed struct { Valid bool Value any - Msg *message.MessageSegment + Msg *message.Segment } func (p PatternParsed) GetText() []string { @@ -70,7 +71,7 @@ func (p *Pattern) Text(regex string) *Pattern { re := regexp.MustCompile(regex) pattern := PatternSegment{ Type: "text", - Parse: func(msg *message.MessageSegment) *PatternParsed { + Parse: func(msg *message.Segment) *PatternParsed { s := msg.Data["text"] s = strings.Trim(s, " \n\r\t") matchString := re.MatchString(s) @@ -80,12 +81,12 @@ func (p *Pattern) Text(regex string) *Pattern { Value: re.FindStringSubmatch(s), Msg: msg, } - } else { - return &PatternParsed{ - Valid: false, - Value: nil, - Msg: nil, - } + } + + return &PatternParsed{ + Valid: false, + Value: nil, + Msg: nil, } }, } @@ -100,19 +101,19 @@ func (p *Pattern) At(id ...string) *Pattern { } pattern := PatternSegment{ Type: "at", - Parse: func(msg *message.MessageSegment) *PatternParsed { + Parse: func(msg *message.Segment) *PatternParsed { if len(id) == 0 || len(id) == 1 && id[0] == msg.Data["qq"] { return &PatternParsed{ Valid: true, Value: msg.Data["qq"], Msg: msg, } - } else { - return &PatternParsed{ - Valid: false, - Value: nil, - Msg: nil, - } + } + + return &PatternParsed{ + Valid: false, + Value: nil, + Msg: nil, } }, } @@ -124,7 +125,7 @@ func (p *Pattern) At(id ...string) *Pattern { func (p *Pattern) Image() *Pattern { pattern := PatternSegment{ Type: "image", - Parse: func(msg *message.MessageSegment) *PatternParsed { + Parse: func(msg *message.Segment) *PatternParsed { return &PatternParsed{ Valid: true, Value: msg.Data["file"], @@ -140,7 +141,7 @@ func (p *Pattern) Image() *Pattern { func (p *Pattern) Reply() *Pattern { pattern := PatternSegment{ Type: "reply", - Parse: func(msg *message.MessageSegment) *PatternParsed { + Parse: func(msg *message.Segment) *PatternParsed { return &PatternParsed{ Valid: true, Value: msg.Data["id"], @@ -159,12 +160,12 @@ func containsOptional(pattern Pattern) bool { } return false } -func patternMatch(ctx *Ctx, pattern Pattern, msgs []message.MessageSegment) bool { +func patternMatch(ctx *Ctx, pattern Pattern, msgs []message.Segment) bool { if !containsOptional(pattern) && len(pattern) != len(msgs) { return false } - if _, ok := ctx.State[KEY_PATTERN]; !ok { - ctx.State[KEY_PATTERN] = make([]*PatternParsed, 0, 1) + if _, ok := ctx.State[KeyPattern]; !ok { + ctx.State[KeyPattern] = make([]*PatternParsed, 0, 1) } i := 0 j := 0 @@ -181,13 +182,13 @@ func patternMatch(ctx *Ctx, pattern Pattern, msgs []message.MessageSegment) bool } if j >= len(msgs) || pattern[i].Type != (msgs[j].Type) || !parsed.Valid { if pattern[i].Optional { - ctx.State[KEY_PATTERN] = append(ctx.State[KEY_PATTERN].([]*PatternParsed), parsed) + ctx.State[KeyPattern] = append(ctx.State[KeyPattern].([]*PatternParsed), parsed) i++ continue } return false } - ctx.State[KEY_PATTERN] = append(ctx.State[KEY_PATTERN].([]*PatternParsed), parsed) + ctx.State[KeyPattern] = append(ctx.State[KeyPattern].([]*PatternParsed), parsed) i++ j++ } diff --git a/rules.go b/rules.go index 0f3f226..5c52d7d 100644 --- a/rules.go +++ b/rules.go @@ -12,7 +12,7 @@ import ( ) const ( - KEY_PATTERN = "pattern_matched" + KeyPattern = "pattern_matched" ) // Type check the ctx.Event's type @@ -133,13 +133,13 @@ func PatternRule(pattern *Pattern) Rule { return false } // copy messages - msgs := make([]message.MessageSegment, 0, len(ctx.Event.Message)) + msgs := make([]message.Segment, 0, len(ctx.Event.Message)) msgs = append(msgs, ctx.Event.Message[0]) for i := 1; i < len(ctx.Event.Message); i++ { if ctx.Event.Message[i-1].Type == "reply" && ctx.Event.Message[i].Type == "at" { // [reply][at] reply := ctx.GetMessage(ctx.Event.Message[i-1].Data["id"]) - if reply.MessageId.ID() == 0 || reply.Sender == nil || reply.Sender.ID == 0 { + if reply.MessageID.ID() == 0 || reply.Sender == nil || reply.Sender.ID == 0 { // failed to get history message msgs = append(msgs, ctx.Event.Message[i]) continue