From eb767ed26e973d845b3f56090ab4b05b9762fbf1 Mon Sep 17 00:00:00 2001 From: c0re100 Date: Thu, 1 May 2025 20:32:42 +0800 Subject: [PATCH] Improve command parser --- client/extra.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/client/extra.go b/client/extra.go index 827c857..c199cbf 100644 --- a/client/extra.go +++ b/client/extra.go @@ -15,33 +15,29 @@ func UuidV4Generator() ExtraGenerator { } func IsCommand(text string) bool { - if text != "" { - if text[0] == '/' { - return true - } + if i := strings.Index(text, "/"); i == 0 { + return true } return false } func CheckCommand(text string, entities []*TextEntity) string { if IsCommand(text) { + var cmd string + // e.g. ["/hello 123", "/hell o 123"] // Result: "/hello", "/hell" if i := strings.Index(text, " "); i != -1 { - // Fallback: remove @bot - if i2 := strings.Index(text, "@"); i2 != -1 { - return text[:i2] - } - return text[:i] + cmd = text[:i] } // e.g.: ["/hello@world_bot", "/hello@", "/hello@123"] // Result: "/hello" if i := strings.Index(text, "@"); i != -1 { - return text[:i] + cmd = text[:i] } - return text + return cmd } return "" }