mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-21 20:20:17 +01:00
Improve command parser
This commit is contained in:
parent
b943b2fe5e
commit
eb767ed26e
1 changed files with 7 additions and 11 deletions
|
|
@ -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 ""
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue