2023-01-22 23:54:54 +03:00
|
|
|
package nechotron
|
|
|
|
|
2023-01-24 00:37:08 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-01-24 03:08:34 +03:00
|
|
|
"log"
|
2023-01-24 00:37:08 +03:00
|
|
|
"strings"
|
2023-01-24 03:08:34 +03:00
|
|
|
|
|
|
|
"github.com/NicoNex/echotron/v3"
|
2023-01-24 00:37:08 +03:00
|
|
|
)
|
2023-01-22 23:54:54 +03:00
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Body string
|
|
|
|
IsAdminOnly bool
|
2023-01-24 00:37:08 +03:00
|
|
|
Description string
|
2023-01-22 23:54:54 +03:00
|
|
|
}
|
|
|
|
|
2023-01-24 00:37:08 +03:00
|
|
|
func NewCommand(body string, desc string, isAdminOnly bool) *Command {
|
2023-01-22 23:54:54 +03:00
|
|
|
return &Command{
|
|
|
|
Body: body,
|
|
|
|
IsAdminOnly: isAdminOnly,
|
2023-01-24 00:37:08 +03:00
|
|
|
Description: desc,
|
2023-01-22 23:54:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Command) String() string {
|
|
|
|
return "/" + c.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Param(text string) string {
|
2023-01-24 03:10:57 +03:00
|
|
|
strs := strings.SplitN(text, " ", 2)
|
|
|
|
if len(strs) > 1 {
|
|
|
|
return strs[1]
|
|
|
|
}
|
|
|
|
return ""
|
2023-01-22 23:54:54 +03:00
|
|
|
}
|
|
|
|
|
2023-01-24 03:08:34 +03:00
|
|
|
func MakeCommandList(format string, commands ...*Command) string {
|
2023-01-24 00:37:08 +03:00
|
|
|
result := ""
|
|
|
|
for _, command := range commands {
|
2023-01-24 03:08:34 +03:00
|
|
|
result += fmt.Sprintf(format, command.String(), EscapeMd2(command.Description))
|
2023-01-24 00:37:08 +03:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-01-24 03:08:34 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:54:54 +03:00
|
|
|
// func HandleCommand(command *Command, handler cmdFunc) (bool, error)
|