Skip to content

Commit

Permalink
chore: make lint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
RikaCelery committed Oct 12, 2024
1 parent 9403051 commit 7526ee8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion extension/rate/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions extension/single/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime"

"github.com/RomiChan/syncx"

zero "github.com/wdvxdr1123/ZeroBot"
)

Expand Down
49 changes: 25 additions & 24 deletions pattern.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package zero

import (
"github.com/wdvxdr1123/ZeroBot/message"
"regexp"
"strings"

"github.com/wdvxdr1123/ZeroBot/message"
)

type Pattern []PatternSegment
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
},
}
Expand All @@ -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,
}
},
}
Expand All @@ -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"],
Expand All @@ -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"],
Expand All @@ -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
Expand All @@ -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++
}
Expand Down
6 changes: 3 additions & 3 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
KEY_PATTERN = "pattern_matched"
KeyPattern = "pattern_matched"
)

// Type check the ctx.Event's type
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7526ee8

Please sign in to comment.