-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdlist.go
141 lines (111 loc) · 3.03 KB
/
cmdlist.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package chatbot
import (
"context"
"github.com/golang/protobuf/proto"
chatbotbase "github.com/zhs007/chatbot/base"
chatbotpb "github.com/zhs007/chatbot/chatbotpb"
"go.uber.org/zap"
)
// CommondsList - command list
type CommondsList struct {
cmds map[string]Command
}
// CommandData - command data
type CommandData struct {
Cmd string
Params interface{}
}
// NewCommondsList - new CommondsList
func NewCommondsList() *CommondsList {
return &CommondsList{
cmds: make(map[string]Command),
}
}
// AddCommand - add command
func (cmds *CommondsList) AddCommand(cmd string) error {
c := mgrCommands.GetCommand(cmd)
if c == nil {
return chatbotbase.ErrCmdNoCmd
}
cmds.cmds[cmd] = c
return nil
}
// MultiParseInChat - parse in chat
func (cmds *CommondsList) MultiParseInChat(chat *chatbotpb.ChatMsg) ([]*CommandData, error) {
lstlines := SplitMultiCommandString(chat.Msg)
if len(lstlines) <= 0 {
return nil, chatbotbase.ErrCmdEmptyCmd
}
lstcmds := []*CommandData{}
for _, v := range lstlines {
lst := SplitCommandString(v)
if len(lst) <= 0 {
chatbotbase.Error("CommondsList.MultiParseInChat.SplitCommandString",
zap.String("cmd", v),
zap.Error(chatbotbase.ErrCmdEmptyCmd))
continue
}
cmd := lst[0]
c, ok := cmds.cmds[cmd]
if ok {
params, err := c.ParseCommandLine(lst, chat)
if err != nil {
chatbotbase.Error("CommondsList.MultiParseInChat.ParseCommandLine",
zap.String("cmd", cmd),
zap.String("cmd string", v),
zap.Error(err))
continue
}
lstcmds = append(lstcmds, &CommandData{
Cmd: cmd,
Params: params,
})
}
}
if len(lstcmds) <= 0 {
return nil, chatbotbase.ErrCmdEmptyCmd
}
return lstcmds, nil
}
// ParseInChat - parse in chat
func (cmds *CommondsList) ParseInChat(chat *chatbotpb.ChatMsg) (string, interface{}, error) {
lst := SplitCommandString(chat.Msg)
if len(lst) <= 0 {
return "", nil, chatbotbase.ErrCmdEmptyCmd
}
cmd := lst[0]
c, ok := cmds.cmds[cmd]
if ok {
params, err := c.ParseCommandLine(lst, chat)
return cmd, params, err
}
return "", nil, chatbotbase.ErrCmdNoCmd
}
// RunInChat - run func with cmd
func (cmds *CommondsList) RunInChat(ctx context.Context, cmd string, serv *Serv, params interface{},
chat *chatbotpb.ChatMsg, ui *chatbotpb.UserInfo, ud proto.Message,
scs chatbotpb.ChatBotService_SendChatServer) (bool, []*chatbotpb.ChatMsg, error) {
c, ok := cmds.cmds[cmd]
if ok {
return c.RunCommand(ctx, serv, params, chat, ui, ud, scs)
}
return true, nil, chatbotbase.ErrCmdNoCmd
}
// OnMessage - get message
func (cmds *CommondsList) OnMessage(ctx context.Context, cmd string, serv *Serv, chat *chatbotpb.ChatMsg,
ui *chatbotpb.UserInfo, ud proto.Message,
scs chatbotpb.ChatBotService_SendChatServer) (bool, []*chatbotpb.ChatMsg, error) {
c, ok := cmds.cmds[cmd]
if ok {
return c.OnMessage(ctx, serv, chat, ui, ud, scs)
}
return true, nil, chatbotbase.ErrCmdNoCmd
}
// HasCommand - has command
func (cmds *CommondsList) HasCommand(cmd string) bool {
_, ok := cmds.cmds[cmd]
if ok {
return true
}
return false
}