forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restrict.go
47 lines (42 loc) · 974 Bytes
/
restrict.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package middleware
import tele "github.com/Ja7ad/telebot"
type RestrictConfig struct {
Chats []int64
In, Out tele.HandlerFunc
}
func Restrict(v RestrictConfig) tele.MiddlewareFunc {
return func(next tele.HandlerFunc) tele.HandlerFunc {
if v.In == nil {
v.In = next
}
if v.Out == nil {
v.Out = next
}
return func(c tele.Context) error {
for _, chat := range v.Chats {
if chat == c.Sender().ID {
return v.In(c)
}
}
return v.Out(c)
}
}
}
func Whitelist(chats ...int64) tele.MiddlewareFunc {
return func(next tele.HandlerFunc) tele.HandlerFunc {
return Restrict(RestrictConfig{
Chats: chats,
In: next,
Out: func(c tele.Context) error { return nil },
})(next)
}
}
func Blacklist(chats ...int64) tele.MiddlewareFunc {
return func(next tele.HandlerFunc) tele.HandlerFunc {
return Restrict(RestrictConfig{
Chats: chats,
Out: next,
In: func(c tele.Context) error { return nil },
})(next)
}
}