init code

This commit is contained in:
thuanle
2024-10-24 09:53:23 +07:00
parent a7559b3f9d
commit 92a63c7885
43 changed files with 1115 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
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)
}
}