This commit is contained in:
cdle
2021-09-14 20:44:05 +08:00
parent 802ed85ea5
commit 2545606147
7 changed files with 102 additions and 4 deletions
+14
View File
@@ -11,15 +11,24 @@ import (
"github.com/beego/beego/v2/core/logs"
"github.com/cdle/sillyGirl/im"
"github.com/cdle/sillyGirl/im/tg"
cron "github.com/robfig/cron/v3"
tb "gopkg.in/tucnak/telebot.v2"
)
var c *cron.Cron
func init() {
c = cron.New()
c.Start()
}
type Function struct {
Rules []string
FindAll bool
Admin bool
Handle func(s im.Sender) interface{}
Regex bool
Cron string
}
var pname = regexp.MustCompile(`/([^/\s]+)`).FindStringSubmatch(os.Args[0])[1]
@@ -124,6 +133,11 @@ func AddCommand(prefix string, cmds []Function) {
}
}
functions = append(functions, cmd)
if cmd.Cron != "" {
c.AddFunc(cmd.Cron, func() {
cmd.Handle(nil)
})
}
}
}
+39
View File
@@ -0,0 +1,39 @@
package core
import (
"github.com/boltdb/bolt"
)
var name = "sillyGirl"
var db *bolt.DB
func init() {
var err error
db, err = bolt.Open(ExecPath+"/"+name+".cache", 0600, nil)
if err != nil {
panic(err)
}
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(name))
if b == nil {
tx.CreateBucket([]byte(name))
}
return nil
})
}
func Set(key string, value string) {
db.Update(func(tx *bolt.Tx) error {
tx.Bucket([]byte(name)).Put([]byte(key), []byte(value))
return nil
})
}
func Get(key string) string {
value := ""
db.View(func(tx *bolt.Tx) error {
value = string(tx.Bucket([]byte(name)).Get([]byte(key)))
return nil
})
return value
}
+36
View File
@@ -0,0 +1,36 @@
package core
import (
"errors"
"github.com/cdle/sillyGirl/im"
)
func init() {
AddCommand("", []Function{
{
Rules: []string{"set ? ?"},
Handle: func(s im.Sender) interface{} {
Set(s.Get(0), s.Get(1))
return "设置成功"
},
},
{
Rules: []string{"delete ?"},
Handle: func(s im.Sender) interface{} {
Set(s.Get(), "")
return "删除成功"
},
},
{
Rules: []string{"get ?"},
Handle: func(s im.Sender) interface{} {
v := Get(s.Get())
if v == "" {
return errors.New("空值")
}
return v
},
},
})
}