This commit is contained in:
cdle
2021-09-16 15:52:32 +08:00
parent 241f487d14
commit cac60b4f8d
4 changed files with 35 additions and 18 deletions
+17 -16
View File
@@ -23,7 +23,6 @@ type Function struct {
FindAll bool FindAll bool
Admin bool Admin bool
Handle func(s im.Sender) interface{} Handle func(s im.Sender) interface{}
Regex bool
Cron string Cron string
} }
@@ -91,23 +90,25 @@ func initToHandleMessage() {
} }
func AddCommand(prefix string, cmds []Function) { func AddCommand(prefix string, cmds []Function) {
for _, cmd := range cmds { for j := range cmds {
if !cmd.Regex { for i := range cmds[j].Rules {
for i := range cmd.Rules { if strings.Contains(cmds[j].Rules[i], "raw ") {
if prefix != "" { cmds[j].Rules[i] = strings.Replace(cmds[j].Rules[i], "raw ", "", -1)
cmd.Rules[i] = prefix + `\s+` + cmd.Rules[i] continue
}
cmd.Rules[i] = strings.Replace(cmd.Rules[i], "(", `[(]`, -1)
cmd.Rules[i] = strings.Replace(cmd.Rules[i], ")", `[)]`, -1)
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] + "$"
} }
if prefix != "" {
cmds[j].Rules[i] = prefix + `\s+` + cmds[j].Rules[i]
}
cmds[j].Rules[i] = strings.Replace(cmds[j].Rules[i], "(", `[(]`, -1)
cmds[j].Rules[i] = strings.Replace(cmds[j].Rules[i], ")", `[)]`, -1)
cmds[j].Rules[i] = strings.Replace(cmds[j].Rules[i], " ", `\s+`, -1)
cmds[j].Rules[i] = strings.Replace(cmds[j].Rules[i], "?", `(\S+)`, -1)
cmds[j].Rules[i] = "^" + cmds[j].Rules[i] + "$"
} }
functions = append(functions, cmd) functions = append(functions, cmds[j])
if cmd.Cron != "" { if cmds[j].Cron != "" {
c.AddFunc(cmd.Cron, func() { c.AddFunc(cmds[j].Cron, func() {
cmd.Handle(nil) cmds[j].Handle(nil)
}) })
} }
} }
+6 -2
View File
@@ -53,7 +53,9 @@ func (bucket Bucket) Get(kv ...string) string {
if b == nil { if b == nil {
return nil return nil
} }
value = string(b.Get([]byte(key))) if v := string(b.Get([]byte(key))); v != "" {
value = v
}
return nil return nil
}) })
return value return value
@@ -78,7 +80,9 @@ func (bucket Bucket) GetInt(key string, vs ...int) int {
func (bucket Bucket) Foreach(f func(k, v []byte) error) { func (bucket Bucket) Foreach(f func(k, v []byte) error) {
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket)) b := tx.Bucket([]byte(bucket))
b.ForEach(f) if b != nil {
b.ForEach(f)
}
return nil return nil
}) })
} }
+11
View File
@@ -2,12 +2,23 @@ package core
import ( import (
"errors" "errors"
"strings"
"github.com/cdle/sillyGirl/im" "github.com/cdle/sillyGirl/im"
) )
func init() { func init() {
AddCommand("", []Function{ AddCommand("", []Function{
{
Rules: []string{"raw 命令"},
Handle: func(_ im.Sender) interface{} {
ss := []string{}
for _, f := range functions {
ss = append(ss, strings.Join(f.Rules, " "))
}
return strings.Join(ss, "\n")
},
},
{ {
Admin: true, Admin: true,
Rules: []string{"set ? ? ?"}, Rules: []string{"set ? ? ?"},
+1
View File
@@ -35,6 +35,7 @@ func init() {
Token: token, Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second}, Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}) })
if err != nil { if err != nil {
logs.Warn("监听telegram机器人失败:%v", err) logs.Warn("监听telegram机器人失败:%v", err)
return return