-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin_chatcmd.go
64 lines (50 loc) · 1.16 KB
/
plugin_chatcmd.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
package proxy
import (
"sync"
)
// A ChatCmd holds information on how to handle a chat command.
type ChatCmd struct {
Name string
Perm string
Help string
Usage string
Handler func(*ClientConn, ...string) string
}
var chatCmds map[string]ChatCmd
var chatCmdsMu sync.RWMutex
var chatCmdsOnce sync.Once
// ChatCmds returns a map of all ChatCmds indexed by their names.
func ChatCmds() map[string]ChatCmd {
initChatCmds()
chatCmdsMu.RLock()
defer chatCmdsMu.RUnlock()
cmds := make(map[string]ChatCmd)
for name, cmd := range chatCmds {
cmds[name] = cmd
}
return cmds
}
// ChatCmdExists reports if a ChatCmd exists.
func ChatCmdExists(name string) bool {
_, ok := ChatCmds()[name]
return ok
}
// RegisterChatCmd adds a new ChatCmd. It returns true on success
// and false if a command with the same name already exists.
func RegisterChatCmd(cmd ChatCmd) bool {
initChatCmds()
if ChatCmdExists(cmd.Name) {
return false
}
chatCmdsMu.Lock()
defer chatCmdsMu.Unlock()
chatCmds[cmd.Name] = cmd
return true
}
func initChatCmds() {
chatCmdsOnce.Do(func() {
chatCmdsMu.Lock()
defer chatCmdsMu.Unlock()
chatCmds = make(map[string]ChatCmd)
})
}