mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-15 20:16:11 +00:00
feat(commands): improve discovery and normalize parameters
This commit is contained in:
1 parent
3316eec74c
commit
f5ba9d4032
31 files changed
+479
-56
No files matched your search
@@ -3,10 +3,14 @@ package modules
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var commandNameRe = regexp.MustCompile(`^[a-z0-9_]{1,32}$`)
|
||||
|
||||
const telegramCommandDescriptionMaxRunes = 256
|
||||
|
||||
func validateCommand(c Command) error {
|
||||
if !commandNameRe.MatchString(c.Name) {
|
||||
return fmt.Errorf("command name %q must match %s", c.Name, commandNameRe)
|
||||
@@ -16,9 +20,27 @@ func validateCommand(c Command) error {
|
||||
default:
|
||||
return fmt.Errorf("command %q: unknown visibility %d", c.Name, c.Visibility)
|
||||
}
|
||||
if c.Description == "" {
|
||||
if strings.TrimSpace(c.Description) == "" {
|
||||
return fmt.Errorf("command %q: description is required", c.Name)
|
||||
}
|
||||
if strings.ContainsAny(c.Description, "\r\n") {
|
||||
return fmt.Errorf("command %q: description must be single-line", c.Name)
|
||||
}
|
||||
if strings.ContainsAny(c.Parameters, "\r\n") {
|
||||
return fmt.Errorf("command %q: parameters must be single-line", c.Name)
|
||||
}
|
||||
if strings.ContainsAny(c.Example, "\r\n") {
|
||||
return fmt.Errorf("command %q: example must be single-line", c.Name)
|
||||
}
|
||||
if example := strings.TrimSpace(c.Example); example != "" {
|
||||
prefix := "/" + c.Name
|
||||
if example != prefix && !strings.HasPrefix(example, prefix+" ") {
|
||||
return fmt.Errorf("command %q: example must invoke %s", c.Name, prefix)
|
||||
}
|
||||
}
|
||||
if c.Visibility == VisibilityPublic && utf8.RuneCountInString(c.TelegramMenuDescription()) > telegramCommandDescriptionMaxRunes {
|
||||
return fmt.Errorf("command %q: Telegram menu description exceeds %d characters", c.Name, telegramCommandDescriptionMaxRunes)
|
||||
}
|
||||
if c.Handler == nil {
|
||||
return fmt.Errorf("command %q: handler is nil", c.Name)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user