chore: moved packages to v1
This commit is contained in:
1 parent
522ba1b639
commit
43e3a8a397
15 files changed
+26
-14
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package filters
|
||||
|
||||
import (
|
||||
mcontext "github.com/abelith/gomax/v1/context"
|
||||
"github.com/abelith/gomax/v1/state"
|
||||
"github.com/max-messenger/max-bot-api-client-go/v2/model"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Filter func(c *mcontext.Context) bool
|
||||
|
||||
type CallbackFilter Filter
|
||||
|
||||
type MessageFilter Filter
|
||||
|
||||
func And(filters ...func(c *mcontext.Context) bool) func(c *mcontext.Context) bool {
|
||||
return func(c *mcontext.Context) bool {
|
||||
for _, filter := range filters {
|
||||
if !filter(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func Or(filters ...func(c *mcontext.Context) bool) func(c *mcontext.Context) bool {
|
||||
return func(c *mcontext.Context) bool {
|
||||
for _, filter := range filters {
|
||||
if filter(c) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func StartCommand() Filter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
upd := c.Update()
|
||||
if upd.UpdateType == model.UpdateBotStarted {
|
||||
return true
|
||||
}
|
||||
|
||||
return strings.HasPrefix(upd.GetCommand().Command, "/start")
|
||||
}
|
||||
}
|
||||
|
||||
func Command(cmd string) Filter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
upd := c.Update()
|
||||
return upd.UpdateType == model.UpdateMessageCreated && strings.HasPrefix(upd.GetCommand().Command, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
func State(state state.State) Filter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
return c.State().State() == state
|
||||
}
|
||||
}
|
||||
|
||||
func BotStopped() Filter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
return c.Update().UpdateType == model.UpdateBotStopped
|
||||
}
|
||||
}
|
||||
|
||||
func Message() MessageFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
return c.Update().UpdateType == model.UpdateMessageCreated && c.Update().Message != nil
|
||||
}
|
||||
}
|
||||
|
||||
func (mf MessageFilter) Text(s string) MessageFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
if !mf(c) {
|
||||
return false
|
||||
}
|
||||
|
||||
return c.Update().Message.Body.Text == s
|
||||
}
|
||||
}
|
||||
|
||||
func (mf MessageFilter) Regex(re *regexp.Regexp) MessageFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
if !mf(c) {
|
||||
return false
|
||||
}
|
||||
|
||||
return re.MatchString(c.Update().Message.Body.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func (mf MessageFilter) Geo() MessageFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
if !mf(c) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, attachment := range c.Update().Message.Body.Attachments {
|
||||
if attachment.Type == model.AttachLocation {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func Callback() CallbackFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
return c.Update().UpdateType == model.UpdateMessageCallback
|
||||
}
|
||||
}
|
||||
|
||||
func (cf CallbackFilter) StartsWith(prefix string) CallbackFilter {
|
||||
return func(c *mcontext.Context) bool {
|
||||
if !cf(c) {
|
||||
return false
|
||||
}
|
||||
|
||||
upd := c.Update().Callback
|
||||
if upd == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.HasPrefix(upd.Payload, prefix)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user