nechotron/filters.go

68 lines
1.2 KiB
Go
Raw Normal View History

package nechotron
2023-02-13 01:39:57 +03:00
import (
"strings"
)
type FilterFn func(u *Update) bool
2023-02-13 01:39:57 +03:00
type FilteredValue interface{}
func TextStartsWith(text string) FilterFn {
return func(u *Update) bool {
return strings.HasPrefix(u.Text(), text)
}
}
func TextStartsWithAny(subs ...string) FilterFn {
return func(u *Update) bool {
text := u.Text()
for _, sub := range subs {
if strings.HasPrefix(text, sub) {
return true
}
}
return false
}
}
func TextHas(text string) FilterFn {
return func(u *Update) bool {
return strings.Contains(u.Text(), text)
}
}
func TextHasAny(subs ...string) FilterFn {
return func(u *Update) bool {
text := u.Text()
for _, sub := range subs {
if strings.Contains(text, sub) {
return true
}
}
return false
}
}
2023-01-24 23:16:58 +03:00
func IsPrivate(u *Update) bool {
return u.ChatID() > 0
}
func IsForum(u *Update) bool {
return u.Chat().IsForum
}
func IsReply(u *Update) bool {
return u.IsReply()
}
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
}
return member.Result.Status == "administrator" || member.Result.Status == "creator"
}