Moved IsUserAdmin from Update, added Chat method

This commit is contained in:
nefrace 2023-01-24 02:25:08 +03:00
parent 906c769453
commit e396d6a885
2 changed files with 42 additions and 2 deletions

View File

@ -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)

View File

@ -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