2020-04-18 19:00:31 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
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) (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)
|
2020-04-25 21:21:57 +00:00
|
|
|
|
|
|
|
return err == nil, nil
|
2020-04-18 19:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return command.ExecuteCommand(cm.Bot, update)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *CommandManager) RegisterCommand(command Command) {
|
|
|
|
if cm.Commands == nil {
|
|
|
|
cm.Commands = make([]Command, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.Commands = append(cm.Commands, command)
|
|
|
|
}
|