-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdmgr.go
80 lines (62 loc) · 1.64 KB
/
cmdmgr.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
package chatbot
// CommandsMgr - command manager
type CommandsMgr struct {
mapCmd map[string]Command
}
// newCommandsMgr - new CommandMgr
func newCommandsMgr() *CommandsMgr {
return &CommandsMgr{
mapCmd: make(map[string]Command),
}
}
// RegisterCommand - add command with cmd
func (m *CommandsMgr) RegisterCommand(cmd string, c Command) {
m.mapCmd[cmd] = c
}
// // ParseInChat - parse in chat
// func (m *CommandsMgr) ParseInChat(chat *chatbotpb.ChatMsg) (string, proto.Message, error) {
// lst := SplitCommandString(chat.Msg)
// if len(lst) <= 0 {
// return "", nil, chatbotbase.ErrCmdEmptyCmd
// }
// cmd := lst[0]
// c, ok := m.mapCmd[cmd]
// if ok {
// params, err := c.ParseCommandLine(lst, chat)
// return cmd, params, err
// }
// return "", nil, chatbotbase.ErrCmdNoCmd
// }
// // RunInChat - run func with cmd
// func (m *CommandsMgr) RunInChat(ctx context.Context, cmd string, serv *Serv, params proto.Message,
// chat *chatbotpb.ChatMsg) ([]*chatbotpb.ChatMsg, error) {
// c, ok := m.mapCmd[cmd]
// if ok {
// return c.RunCommand(ctx, serv, params, chat)
// }
// return nil, chatbotbase.ErrCmdNoCmd
// }
// // HasCommand - has command
// func (m *CommandsMgr) HasCommand(cmd string) bool {
// _, ok := m.mapCmd[cmd]
// if ok {
// return true
// }
// return false
// }
// GetCommand - get command
func (m *CommandsMgr) GetCommand(cmd string) Command {
c, ok := m.mapCmd[cmd]
if ok {
return c
}
return nil
}
var mgrCommands *CommandsMgr
func init() {
mgrCommands = newCommandsMgr()
}
// RegisterCommand - add command with cmd
func RegisterCommand(cmd string, c Command) {
mgrCommands.RegisterCommand(cmd, c)
}