Improve command parser

This commit is contained in:
c0re100 2025-05-01 20:32:42 +08:00
parent b943b2fe5e
commit eb767ed26e
No known key found for this signature in database
GPG key ID: 7C3B3004FE745AAF

View file

@ -15,33 +15,29 @@ func UuidV4Generator() ExtraGenerator {
} }
func IsCommand(text string) bool { func IsCommand(text string) bool {
if text != "" { if i := strings.Index(text, "/"); i == 0 {
if text[0] == '/' { return true
return true
}
} }
return false return false
} }
func CheckCommand(text string, entities []*TextEntity) string { func CheckCommand(text string, entities []*TextEntity) string {
if IsCommand(text) { if IsCommand(text) {
var cmd string
// e.g. ["/hello 123", "/hell o 123"] // e.g. ["/hello 123", "/hell o 123"]
// Result: "/hello", "/hell" // Result: "/hello", "/hell"
if i := strings.Index(text, " "); i != -1 { if i := strings.Index(text, " "); i != -1 {
// Fallback: remove @bot cmd = text[:i]
if i2 := strings.Index(text, "@"); i2 != -1 {
return text[:i2]
}
return text[:i]
} }
// e.g.: ["/hello@world_bot", "/hello@", "/hello@123"] // e.g.: ["/hello@world_bot", "/hello@", "/hello@123"]
// Result: "/hello" // Result: "/hello"
if i := strings.Index(text, "@"); i != -1 { if i := strings.Index(text, "@"); i != -1 {
return text[:i] cmd = text[:i]
} }
return text return cmd
} }
return "" return ""
} }