58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package message
|
|
|
|
import (
|
|
"fmt"
|
|
"trbot/utils"
|
|
"trbot/utils/handler_params"
|
|
"trbot/utils/type/contain"
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
)
|
|
|
|
type FullCommand struct {
|
|
FullCommand string
|
|
ForChatType []models.ChatType // default for private, group, supergroup
|
|
|
|
MessageHandler func(*handler_params.Message) error
|
|
}
|
|
|
|
type FullCommandHandlers []FullCommand
|
|
|
|
func (hs *FullCommandHandlers) Add(handler FullCommand) bool{
|
|
if handler.FullCommand == "" {
|
|
return false
|
|
}
|
|
if handler.ForChatType == nil {
|
|
handler.ForChatType = []models.ChatType{
|
|
models.ChatTypePrivate,
|
|
models.ChatTypeGroup,
|
|
models.ChatTypeSupergroup,
|
|
}
|
|
}
|
|
*hs = append(*hs, handler)
|
|
return true
|
|
}
|
|
|
|
func (hs *FullCommandHandlers) Remove(fullCommand string) {
|
|
for i, h := range *hs {
|
|
if h.FullCommand == fullCommand {
|
|
*hs = append((*hs)[:i], (*hs)[i + 1:]...)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (hs *FullCommandHandlers) RunMessage(params *handler_params.Message) (bool, error) {
|
|
for _, plugin := range *hs {
|
|
if plugin.MessageHandler == nil { continue }
|
|
if contain.AnyType(params.Message.Chat.Type, plugin.ForChatType...) && utils.CommandMaybeWithSuffixUsername(params.Fields, plugin.FullCommand) {
|
|
err := plugin.MessageHandler(params)
|
|
if err != nil {
|
|
return true, fmt.Errorf("error in full command handler: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|