residentsleeper/commands/dispatcher.go

36 lines
587 B
Go

package commands
import "strings"
type CommandHandler func(ctx *CommandContext)
var (
commandMap = make(map[string]*CommandHolder)
)
func Register(prefix string, f CommandHandler) {
h := parseCommandUsage(prefix)
h.Handler = f
commandMap[h.Name] = h
}
func Find(prefix, name, full string) *CommandHolder {
if strings.Index(name, prefix) == 0 {
name = name[len(prefix):]
if holder := Handler(name); holder != nil {
return holder
}
}
return nil
}
func Handler(name string) *CommandHolder {
if holder, ok := commandMap[name]; ok {
return holder
}
return nil
}