REPOSITORY / ScuroNeko/Laniakea
Pull Requests
v1.0.0 #9
+1
-1
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CmdRegexp matches command names allowed for Telegram command registration.
|
// CmdRegexp matches command names allowed for Telegram command registration.
|
||||||
var CmdRegexp = regexp.MustCompile("^[a-zA-Z0-9]+$")
|
var CmdRegexp = regexp.MustCompile("^[_a-z0-9]+$")
|
||||||
|
|
||||||
// ErrTooManyCommands is returned when the total number of registered commands
|
// ErrTooManyCommands is returned when the total number of registered commands
|
||||||
// exceeds Telegram's limit of 100 bot commands per bot.
|
// exceeds Telegram's limit of 100 bot commands per bot.
|
||||||
|
|||||||
+4
-4
@@ -127,10 +127,10 @@ func (c *Command[T]) SkipCommandAutoGen() *Command[T] {
|
|||||||
// Returns ErrCmdArgCountMismatch if too few arguments are provided.
|
// Returns ErrCmdArgCountMismatch if too few arguments are provided.
|
||||||
// Returns ErrCmdArgRegexpMismatch if any argument fails regex validation.
|
// Returns ErrCmdArgRegexpMismatch if any argument fails regex validation.
|
||||||
func (c *Command[T]) validateArgs(args []string) error {
|
func (c *Command[T]) validateArgs(args []string) error {
|
||||||
// Count required args
|
for i := range c.args.Len() {
|
||||||
requiredCount := c.args.Filter(func(a CommandArg) bool { return a.required }).Len()
|
if i >= len(args) && c.args.Get(i).required {
|
||||||
if len(args) < requiredCount {
|
return ErrCmdArgCountMismatch
|
||||||
return ErrCmdArgCountMismatch
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate each argument against its regex
|
// Validate each argument against its regex
|
||||||
|
|||||||
@@ -22,3 +22,19 @@ func TestValidateArgsRequiresFullMatch(t *testing.T) {
|
|||||||
t.Fatalf("expected ErrCmdArgRegexpMismatch for partial bool match, got %v", err)
|
t.Fatalf("expected ErrCmdArgRegexpMismatch for partial bool match, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateArgsEnforcesRequiredArgIndex(t *testing.T) {
|
||||||
|
cmd := NewCommand[NoDB](
|
||||||
|
func(ctx *MsgContext, db *NoDB) {},
|
||||||
|
"mixed",
|
||||||
|
*NewCommandArg("optional"),
|
||||||
|
*NewCommandArg("required").SetRequired(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err := cmd.validateArgs([]string{"only-optional"}); !errors.Is(err, ErrCmdArgCountMismatch) {
|
||||||
|
t.Fatalf("expected ErrCmdArgCountMismatch when required second arg is missing, got %v", err)
|
||||||
|
}
|
||||||
|
if err := cmd.validateArgs([]string{"optional", "required"}); err != nil {
|
||||||
|
t.Fatalf("expected both args to validate, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ func (api *API) RemoveMyProfilePhotoWithContext(ctx context.Context) (bool, erro
|
|||||||
// SetChatMenuButtonP holds parameters for the setChatMenuButton method.
|
// SetChatMenuButtonP holds parameters for the setChatMenuButton method.
|
||||||
// See https://core.telegram.org/bots/api#setchatmenubutton
|
// See https://core.telegram.org/bots/api#setchatmenubutton
|
||||||
type SetChatMenuButtonP struct {
|
type SetChatMenuButtonP struct {
|
||||||
ChatID int64 `json:"chat_id"`
|
ChatID int64 `json:"chat_id,omitempty"`
|
||||||
MenuButton MenuButtonType `json:"menu_button"`
|
MenuButton MenuButtonType `json:"menu_button"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user