42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package helper
|
|
|
|
import (
|
|
"gopkg.in/telebot.v3"
|
|
)
|
|
|
|
func throwErrorParamCountMismatch(_ telebot.Context) error {
|
|
return ErrorParamCountMismatch
|
|
}
|
|
|
|
// Base on the number of Args in context, it will execute the (n+1)-th function in the list
|
|
// - If the context have 0 argument, it will call funcs[0]
|
|
// - If the context have 1 argument, it will call funcs[1]
|
|
// Otherwise, it will call `other` function
|
|
func HandleZ0nArgs(other telebot.HandlerFunc, funcs ...telebot.HandlerFunc) telebot.HandlerFunc {
|
|
return func(context telebot.Context) error {
|
|
args := context.Args()
|
|
|
|
if len(args) > len(funcs)-1 {
|
|
return other(context)
|
|
}
|
|
return funcs[len(args)](context)
|
|
}
|
|
}
|
|
|
|
// Base on the number of Args in context, it will execute the (n+1)-th function in the list
|
|
// - If the context have 0 argument, it will call funcs[0]
|
|
// - If the context have 1 argument, it will call funcs[1]
|
|
// Otherwise, it will throw error
|
|
func Handle0nArgs(funcs ...telebot.HandlerFunc) telebot.HandlerFunc {
|
|
return HandleZ0nArgs(throwErrorParamCountMismatch, funcs...)
|
|
}
|
|
|
|
func HandleGetSet(getFunc telebot.HandlerFunc, setFunc telebot.HandlerFunc) telebot.HandlerFunc {
|
|
return func(context telebot.Context) error {
|
|
if len(context.Args()) == 0 {
|
|
return getFunc(context)
|
|
}
|
|
return setFunc(context)
|
|
}
|
|
}
|