2020-04-18 19:00:31 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-05-09 18:54:42 +00:00
|
|
|
"git.dragon-labs.de/alphyron/group_helper/models"
|
2020-04-18 19:00:31 +00:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandManager struct {
|
|
|
|
Commands []Command
|
|
|
|
Bot *tgbotapi.BotAPI
|
|
|
|
}
|
|
|
|
|
2020-05-09 18:54:42 +00:00
|
|
|
func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update, group *models.Group) (bool, error) {
|
2020-04-18 19:00:31 +00:00
|
|
|
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)
|
2020-04-25 21:21:57 +00:00
|
|
|
|
|
|
|
return err == nil, nil
|
2020-04-18 19:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 18:54:42 +00:00
|
|
|
return command.ExecuteCommand(cm.Bot, update, group)
|
2020-04-18 19:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *CommandManager) RegisterCommand(command Command) {
|
|
|
|
if cm.Commands == nil {
|
|
|
|
cm.Commands = make([]Command, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.Commands = append(cm.Commands, command)
|
|
|
|
}
|