From e396d6a885df9b7e1c0ac3f4ac8162ac7090eba1 Mon Sep 17 00:00:00 2001 From: nefrace Date: Tue, 24 Jan 2023 02:25:08 +0300 Subject: [PATCH] Moved IsUserAdmin from Update, added Chat method --- dispatcher.go | 2 +- update.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/dispatcher.go b/dispatcher.go index 434ac27..6b127cd 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -30,7 +30,7 @@ func (d *Dispatcher) HandleCommand(command *Command, handler UpdateHandler) *Dis if !strings.HasPrefix(u.Text(), command.String()) { return false, nil } - if command.IsAdminOnly && !u.IsUserAdmin() && u.ChatID() < 0 { + if command.IsAdminOnly && !IsUserAdmin(u) && u.ChatID() < 0 { return false, nil } err := handler(u) diff --git a/update.go b/update.go index f6b8b7a..a9f0c45 100644 --- a/update.go +++ b/update.go @@ -33,6 +33,26 @@ func (u *Update) Upd() *echotron.Update { return (*echotron.Update)(&u.U) } +func (u *Update) Chat() echotron.Chat { + if u.IsMessage() { + return u.Message.Chat + } + if u.IsCallback() { + return u.CallbackQuery.Message.Chat + } + if u.EditedMessage != nil { + return u.EditedMessage.Chat + } + if u.ChatMember != nil { + return u.ChatMember.Chat + } + if u.MyChatMember != nil { + return u.MyChatMember.Chat + } + log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U) + return echotron.Chat{} +} + func (u *Update) ChatID() int64 { if u.IsMessage() { return u.Message.Chat.ID @@ -160,6 +180,15 @@ func (u *Update) From() *echotron.User { if u.IsMessage() { return u.Message.From } + if u.EditedMessage != nil { + return u.EditedMessage.From + } + if u.ChatMember != nil { + return &u.ChatMember.From + } + if u.MyChatMember != nil { + return &u.MyChatMember.From + } return u.CallbackQuery.From } @@ -177,7 +206,10 @@ func (u *Update) Entities() []*echotron.MessageEntity { return u.Message.CaptionEntities } -func (u *Update) IsUserAdmin() bool { +func IsUserAdmin(u *Update) bool { + if IsPrivate(u) { + return true + } member, err := u.Bot.GetChatMember(u.ChatID(), u.From().ID) if err != nil { return false @@ -185,6 +217,14 @@ func (u *Update) IsUserAdmin() bool { return member.Result.Status == "administrator" || member.Result.Status == "creator" } +func IsPrivate(u *Update) bool { + return u.ChatID() > 0 +} + +func IsForum(u *Update) bool { + return u.Chat().IsForum +} + func (u *Update) LogError(text string, e error, send bool) { if text != "" { text = "Ошибка: " + text