group_helper/cli/manager.go

73 lines
1.4 KiB
Go
Raw Normal View History

package cli
import (
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strings"
)
type CommandManager struct {
Commands []Command
Bot *tgbotapi.BotAPI
}
func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update, group *models.Group) (bool, error) {
if update.Message == nil {
return true, nil
}
if update.Message.Text == "" {
return true, nil
}
cmdStr := update.Message.Text
if !strings.HasPrefix(cmdStr, "/") {
return true, nil
}
var command Command
for _, cmd := range cm.Commands {
if strings.HasPrefix(cmdStr, cmd.GetCommand()) {
command = cmd
break
}
}
if command == nil {
return true, nil
}
if !command.AllowChatType(update.Message.Chat) {
return true, nil
}
if !command.AllowEveryMember() {
userconfig := tgbotapi.ChatConfigWithUser{
ChatID: update.Message.Chat.ID,
UserID: update.Message.From.ID,
}
member, _ := cm.Bot.GetChatMember(userconfig)
if !command.AllowMember(&member) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not allowed to use this command.")
msg.ReplyToMessageID = update.Message.MessageID
_, err := cm.Bot.Send(msg)
return err == nil, nil
}
}
return command.ExecuteCommand(cm.Bot, update, group)
}
func (cm *CommandManager) RegisterCommand(command Command) {
if cm.Commands == nil {
cm.Commands = make([]Command, 0)
}
cm.Commands = append(cm.Commands, command)
}