74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
|
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)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|