-
Notifications
You must be signed in to change notification settings - Fork 1
/
restrict.go
45 lines (40 loc) · 1.18 KB
/
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
package middleware
import tele "github.com/3JoB/telebot/v2"
// RestrictConfig defines config for Restrict middleware.
type RestrictConfig struct {
// Chats is a list of chats that are going to be affected
// by either In or Out function.
Chats []int64
}
// Restrict returns a middleware that handles a list of provided
// chats with the logic defined by In and Out functions.
// If the chat is found in the Chats field, In function will be called,
// otherwise Out function will be called.
func Restrict(v RestrictConfig) tele.HandlerFunc {
return func(c *tele.Context) error {
for _, chat := range v.Chats {
if chat == c.Sender().ID {
return c.Next()
}
}
return nil
}
}
// Blacklist returns a middleware that skips the update for users
// specified in the chats field.
func Blacklist(chats ...int64) tele.HandlerFunc {
return func(ctx *tele.Context) error {
return Restrict(RestrictConfig{
Chats: chats,
})(ctx)
}
}
// Whitelist returns a middleware that skips the update for users
// NOT specified in the chats field.
func Whitelist(chats ...int64) tele.HandlerFunc {
return func(ctx *tele.Context) error {
return Restrict(RestrictConfig{
Chats: chats,
})(ctx)
}
}