Skip to content

Commit

Permalink
support @username
Browse files Browse the repository at this point in the history
  • Loading branch information
RikaCelery committed Nov 7, 2024
1 parent 666c6e5 commit 7600cb8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
18 changes: 12 additions & 6 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package zero

import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/wdvxdr1123/ZeroBot/message"
"regexp"
"strconv"
"strings"

"github.com/sirupsen/logrus"

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

const (
Expand All @@ -25,7 +27,7 @@ func (p *Pattern) AsRule() Rule {

// copy messages
msgs := make([]message.Segment, 0, len(ctx.Event.Message))
atRegexp := regexp.MustCompile(`@([\d\S]+)`)
atRegexp := regexp.MustCompile(`@([\d\S]*)`)
for i := 0; i < len(ctx.Event.Message); i++ {
if i > 0 && ctx.Event.Message[i-1].Type == "reply" && ctx.Event.Message[i].Type == "at" {
// [reply][at]
Expand All @@ -51,8 +53,13 @@ func (p *Pattern) AsRule() Rule {
uid, err := strconv.ParseInt(ats[i][1], 10, 64)
if err != nil {
// assume is user name
uid = 0

list := ctx.GetThisGroupMemberList().Array()
for _, member := range list {
if member.Get("card").Str == ats[i][1] || member.Get("nickname").Str == ats[i][1] {
uid = member.Get("user_id").Int()
break
}
}
}
tmp = append(tmp, message.At(uid))
}
Expand Down Expand Up @@ -188,7 +195,6 @@ func NewTextParser(regex string) Parser {
return func(msg *message.Segment) PatternParsed {
s := msg.Data["text"]
s = strings.Trim(s, " \n\r\t")
println("testing", s, regex, re.MatchString(s))
matchString := re.MatchString(s)
if matchString {
return PatternParsed{
Expand Down
30 changes: 19 additions & 11 deletions pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,33 @@ func TestPattern_FuzzyAt(t *testing.T) {
pattern *Pattern
expected bool
}{
{[]message.Segment{message.Text("haha @114514")}, NewPattern(PatternOption{
//{[]message.Segment{message.Text("haha @114514")}, NewPattern(PatternOption{
// cleanRedundantAt: true,
// fuzzyAt: true,
//}).Text("haha").At(), true},
//{[]message.Segment{message.Text("haha 114514")}, NewPattern(PatternOption{
// cleanRedundantAt: true,
// fuzzyAt: true,
//}).Text("haha").At(), false},
//{[]message.Segment{message.Text("haha @你好")}, NewPattern(PatternOption{
// cleanRedundantAt: true,
// fuzzyAt: true,
//}).Text("haha").At(), true},
{[]message.Segment{message.Text("haha @")}, NewPattern(PatternOption{
cleanRedundantAt: true,
fuzzyAt: true,
}).Text("haha").At(), true},
{[]message.Segment{message.Text("haha 114514")}, NewPattern(PatternOption{
{[]message.Segment{message.Text("haha @ 你说的对")}, NewPattern(PatternOption{
cleanRedundantAt: true,
fuzzyAt: true,
}).Text("haha").At(), false},
{[]message.Segment{message.Text("haha @你好")}, NewPattern(PatternOption{
}).Text("haha").At().Text("你说的对"), true},
{[]message.Segment{message.Text("haha @114514 你说的对")}, NewPattern(PatternOption{
cleanRedundantAt: true,
fuzzyAt: true,
}).Text("haha").At(), true},
{[]message.Segment{message.Text("haha 你好")}, NewPattern(PatternOption{
cleanRedundantAt: true,
fuzzyAt: true,
}).Text("haha").At(), false},
}).Text("haha").At().Text("你说的对"), true},
}
for i, v := range textTests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
for _, v := range textTests {
t.Run(v.msg.String(), func(t *testing.T) {
ctx := fakeCtx(v.msg)
rule := v.pattern.AsRule()
out := rule(ctx)
Expand Down

0 comments on commit 7600cb8

Please sign in to comment.