From 63a0752c614807b0d9170f534f9f65addd1b69df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 4 Oct 2022 16:34:25 +0800 Subject: [PATCH] fix command rule --- event.go | 1 + rules.go | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/event.go b/event.go index 38ff064..2276728 100644 --- a/event.go +++ b/event.go @@ -76,6 +76,7 @@ func match(ctx *Ctx, matchers []*Matcher) { if ctx.Message != nil && ctx.Message.Text != "" { // 确保无空 ctx.IsToMe = func(ctx *Ctx) bool { if ctx.Message.Chat.IsPrivate() { + log.Debugln("[event] private event") return true } name := ctx.Caller.Self.String() diff --git a/rules.go b/rules.go index ad8d582..03e45ff 100644 --- a/rules.go +++ b/rules.go @@ -56,7 +56,7 @@ func SuffixRule(suffixes ...string) Rule { func CommandRule(commands ...string) Rule { return func(ctx *Ctx) bool { msg, ok := ctx.Value.(*tgba.Message) - if !ok || msg.Text == "" || !ctx.IsToMe { // 确保无空 + if !ok || msg.Text == "" { // 确保无空 return false } msg.Text = strings.TrimSpace(msg.Text) @@ -72,10 +72,13 @@ func CommandRule(commands ...string) Rule { case strings.HasPrefix(msg.Text, "/"): a := strings.Index(msg.Text, "@") b := strings.Index(msg.Text, " ") - switch { - case b <= 1 || b == len(msg.Text) || a >= b: + if a > 0 && !strings.HasPrefix(msg.Text[a:], "@"+ctx.Caller.Self.String()) { return false - case a < 0: + } + switch { + case b <= 1: + return false + case a < 0 || a >= b: cmdMessage = msg.Text[1:b] args = msg.Text[b+1:] default: