nechotron/command.go

62 lines
1.3 KiB
Go

package nechotron
import (
"fmt"
"log"
"strings"
"github.com/NicoNex/echotron/v3"
)
type Command struct {
Body string
IsAdminOnly bool
Description string
}
func NewCommand(body string, desc string, isAdminOnly bool) *Command {
return &Command{
Body: body,
IsAdminOnly: isAdminOnly,
Description: desc,
}
}
func (c *Command) String() string {
return "/" + c.Body
}
func (c *Command) Param(text string) string {
strs := strings.SplitN(text, " ", 2)
if len(strs) > 1 {
return strs[1]
}
return ""
}
func MakeCommandList(format string, commands ...*Command) string {
result := ""
for _, command := range commands {
result += fmt.Sprintf(format, command.String(), EscapeMd2(command.Description))
}
return result
}
func botCommands(commands ...*Command) []echotron.BotCommand {
cmds := []echotron.BotCommand{}
for _, c := range commands {
cmds = append(cmds, echotron.BotCommand{Command: c.Body, Description: c.Description})
}
return cmds
}
func SetMyCommands(api echotron.API, langCode string, scope echotron.BotCommandScope, commands ...*Command) error {
_, err := api.SetMyCommands(&echotron.CommandOptions{LanguageCode: "", Scope: scope}, botCommands(commands...)...)
if err != nil {
log.Println(err)
return err
}
return nil
}
// func HandleCommand(command *Command, handler cmdFunc) (bool, error)