From ace55c3f84102de9d5e193b2c211dd570132a57b 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: Fri, 10 Jun 2022 20:51:04 +0800 Subject: [PATCH] fix CommandRule --- rules.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/rules.go b/rules.go index 4de7fa5..4389dc6 100644 --- a/rules.go +++ b/rules.go @@ -55,12 +55,38 @@ 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 == "" || !msg.IsCommand() { // 确保无空 + if !ok || msg.Text == "" { // 确保无空 return false } - ctx.State["command"] = msg.CommandWithAt() - ctx.State["args"] = msg.CommandArguments() - return true + if msg.IsCommand() { + ctx.State["command"] = msg.Command() + ctx.State["args"] = msg.CommandArguments() + return true + } + if strings.HasPrefix(msg.Text, "/") { + a := strings.Index(msg.Text, "@") + b := strings.Index(msg.Text, " ") + if b <= 1 { + ctx.State["command"] = msg.Text[1:] + ctx.State["args"] = "" + return true + } + if b == len(msg.Text) { + return false + } + if a < 0 { + ctx.State["command"] = msg.Text[1:b] + ctx.State["args"] = msg.Text[b+1:] + return true + } + if a >= b { + return false + } + ctx.State["command"] = msg.Text[1:a] + ctx.State["args"] = msg.Text[b+1:] + return true + } + return false } } @@ -153,6 +179,9 @@ func OnlyToMe(ctx *Ctx) bool { if !ok || msg.Text == "" { // 确保无空 return false } + if msg.Chat.IsPrivate() { + return true + } name := ctx.Caller.Self.String() if strings.HasPrefix(msg.Text, name) { return true