-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
help_command.go
94 lines (73 loc) · 2.48 KB
/
help_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package tgbot
import (
"fmt"
"strings"
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
"github.com/samber/lo"
)
type Command struct {
Command string
HelpMessage string
Handler Handler
}
type commandGroup struct {
name string
commands []Command
}
type helpCommandHandler struct {
defaultGroup commandGroup
commandGroups []commandGroup
}
func newHelpCommandHandler() *helpCommandHandler {
h := &helpCommandHandler{
commandGroups: make([]commandGroup, 0),
}
return h
}
func (h *helpCommandHandler) Command() string {
return "help"
}
func (h *helpCommandHandler) CommandHelp() string {
return "获取帮助"
}
func (h *helpCommandHandler) handle(c *Context) (Response, error) {
is, err := c.IsBotAdministrator()
if err != nil {
c.Logger.Error("failed to check if bot is administrator")
}
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)) &&
!lo.Contains([]string{
fmt.Sprintf("%s@%s", h.Command(), c.Bot.Self.UserName),
fmt.Sprintf("%s@%s", "start", c.Bot.Self.UserName),
}, c.Update.Message.CommandWithAt()) {
return nil, nil
}
helpMessage := strings.Builder{}
helpMessage.WriteString("你好,欢迎使用 Insights Bot!\n\n")
helpMessage.WriteString("我当前支持这些命令:\n\n")
commandGroupHelpMessages := make([]string, 0)
if len(h.defaultGroup.commands) > 0 {
h.commandGroups = append(h.commandGroups, h.defaultGroup)
}
for _, group := range h.commandGroups {
commandHelpMessages := make([]string, 0)
for _, cmd := range group.commands {
commandHelpMessage := strings.Builder{}
commandHelpMessage.WriteString("/")
commandHelpMessage.WriteString(cmd.Command)
commandHelpMessage.WriteString("@")
commandHelpMessage.WriteString(c.Bot.Self.UserName)
if cmd.HelpMessage != "" {
commandHelpMessage.WriteString(" - ")
commandHelpMessage.WriteString(cmd.HelpMessage)
}
commandHelpMessages = append(commandHelpMessages, commandHelpMessage.String())
}
commandGroupHelpMessages = append(commandGroupHelpMessages, fmt.Sprintf("%s%s", lo.Ternary(group.name != "", fmt.Sprintf("<b>%s</b>\n\n", EscapeHTMLSymbols(group.name)), ""), strings.Join(commandHelpMessages, "\n")))
}
helpMessage.WriteString(strings.Join(commandGroupHelpMessages, "\n\n"))
return c.NewMessageReplyTo(helpMessage.String(), c.Update.Message.MessageID).WithParseModeHTML(), nil
}