-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
cancel_command.go
63 lines (49 loc) · 1.41 KB
/
cancel_command.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tgbot
import (
"fmt"
"github.com/nekomeowww/fo"
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
"github.com/samber/lo"
)
type cancellableCommand struct {
shouldCancelFunc func(c *Context) (bool, error)
handler Handler
}
type cancelCommandHandler struct {
cancellableCommands []cancellableCommand
}
func newCancelCommandHandler() *cancelCommandHandler {
h := &cancelCommandHandler{
cancellableCommands: make([]cancellableCommand, 0),
}
return h
}
func (h *cancelCommandHandler) Command() string {
return "cancel"
}
func (h *cancelCommandHandler) CommandHelp() string {
return "取消正在进行的操作"
}
func (h *cancelCommandHandler) handle(c *Context) (Response, error) {
may := fo.NewMay[bool]()
is := may.Invoke(c.IsBotAdministrator())
if is &&
c.Update.Message != nil &&
c.Update.Message.Chat != nil &&
lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(c.Update.Message.Chat.Type)) &&
c.Update.Message.CommandWithAt() != fmt.Sprintf("%s@%s", h.Command(), c.Bot.Self.UserName) {
return nil, nil
}
for _, h := range h.cancellableCommands {
should := may.Invoke(h.shouldCancelFunc(c))
if should {
return h.handler.Handle(c)
}
continue
}
err := may.CollectAsError()
if err != nil {
return nil, err
}
return c.NewMessageReplyTo("已经没有正在进行的操作了", c.Update.Message.MessageID), nil
}