This commit is contained in:
cdle
2021-09-09 14:29:56 +08:00
parent 8f4146b7bb
commit b64fe087ff
6 changed files with 40 additions and 22 deletions
+15 -2
View File
@@ -19,6 +19,7 @@ type Function struct {
FindAll bool
Admin bool
Handle func(s im.Sender) interface{}
Regex bool
}
var pname = regexp.MustCompile(`/([^/\s]+)`).FindStringSubmatch(os.Args[0])[1]
@@ -108,8 +109,20 @@ func initToHandleMessage() {
}()
}
func AddCommand(cmd []Function) {
functions = append(functions, cmd...)
func AddCommand(prefix string, cmds []Function) {
for _, cmd := range cmds {
if !cmd.Regex {
for i := range cmd.Rules {
if prefix != "" {
cmd.Rules[i] += prefix + `\s+` + cmd.Rules[i]
}
cmd.Rules[i] = strings.Replace(cmd.Rules[i], " ", `\s+`, -1)
cmd.Rules[i] = strings.Replace(cmd.Rules[i], "?", `(\S+)`, -1)
cmd.Rules[i] += "^" + cmd.Rules[i] + "$"
}
}
functions = append(functions, cmd)
}
}
func handleMessage(sender im.Sender) {