-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
98 lines (83 loc) · 2.32 KB
/
commands.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
package singularity
import (
"errors"
"strings"
"sync"
)
// CommandHandler holds command handlers
// mutex for locking when adding handlers
// handlers is a map holding all CommandHandlers
type CommandHandler struct {
sync.Mutex
caseSensitive bool
prefix string
handlers map[string]func(Command)
}
func (commandHandler *CommandHandler) setPrefix(prefix string) {
commandHandler.Lock()
defer commandHandler.Unlock()
commandHandler.prefix = prefix
}
//NewCommandHandler returns a new command handler
func NewCommandHandler() *CommandHandler {
return &CommandHandler{handlers: make(map[string]func(Command))}
}
// Command type
type Command struct {
Command string
Args []string
Instance *SlackInstance
Channel Channel
User User
Team Team
}
func (commandHandler *CommandHandler) execute(command Command) error {
defer func() {
if err := recover(); err != nil {
//TODO handle error.
}
}()
command.Command = strings.Replace(command.Command, commandHandler.prefix, "", 1)
function := commandHandler.getCommand(command.Command)
if function != nil {
(*function)(command)
}
return nil
}
func (commandHandler *CommandHandler) getCommand(command string) *func(Command) {
commandHandler.Lock()
defer commandHandler.Unlock()
if function, ok := commandHandler.handlers[commandHandler.toLower(command)]; ok {
return &function
}
return nil
}
// IsCommand check if we have a handler for this command
func (commandHandler *CommandHandler) IsCommand(command string) bool {
if command == "" {
return false
}
commandHandler.Lock()
defer commandHandler.Unlock()
if !strings.HasPrefix(command, commandHandler.prefix) {
return false
}
command = strings.Replace(command, commandHandler.prefix, "", 1)
_, ok := commandHandler.handlers[commandHandler.toLower(command)]
return ok
}
func (commandHandler *CommandHandler) registerCommand(command string, function func(Command)) error {
commandHandler.Lock()
defer commandHandler.Unlock()
if _, ok := commandHandler.handlers[strings.ToLower(command)]; ok {
return errors.New("Command already registered!") //TODO Maybe replace?
}
commandHandler.handlers[commandHandler.toLower(command)] = function
return nil
}
func (commandHandler *CommandHandler) toLower(s string) string {
if !commandHandler.caseSensitive {
s = strings.ToLower(s)
}
return s
}