update
This commit is contained in:
+2
-4
@@ -81,12 +81,10 @@ func AddCommand(prefix string, cmds []Function) {
|
||||
func handleMessage(sender Sender) {
|
||||
defer sender.Finish()
|
||||
key := fmt.Sprintf("u=%v&c=%v&i=%v", sender.GetUserID(), sender.GetChatID(), sender.GetImType())
|
||||
// fmt.Println(key, sender.GetContent())
|
||||
// fmt.Println(waits.Load(key))
|
||||
if v, ok := waits.Load(key); ok {
|
||||
c := v.(Carry)
|
||||
|
||||
c := v.(*Carry)
|
||||
if m := regexp.MustCompile(c.Pattern).FindString(sender.GetContent()); m != "" {
|
||||
c.Sender = sender
|
||||
c.Chan <- m
|
||||
sender.Reply(<-c.Result)
|
||||
return
|
||||
|
||||
+12
-8
@@ -30,7 +30,7 @@ type Sender interface {
|
||||
Finish()
|
||||
Continue()
|
||||
IsContinue() bool
|
||||
Await(Sender, func(string, error) interface{}, ...interface{})
|
||||
Await(Sender, func(string, Sender, error) interface{}, ...interface{})
|
||||
}
|
||||
|
||||
type Edit int
|
||||
@@ -221,19 +221,23 @@ type Carry struct {
|
||||
Chan chan interface{}
|
||||
Pattern string
|
||||
Result chan interface{}
|
||||
Sender Sender
|
||||
}
|
||||
|
||||
func (_ *BaseSender) Await(sender Sender, callback func(string, error) interface{}, params ...interface{}) {
|
||||
c := Carry{}
|
||||
func (_ *BaseSender) Await(sender Sender, callback func(string, Sender, error) interface{}, params ...interface{}) {
|
||||
c := &Carry{}
|
||||
timeout := time.Second * 20
|
||||
for _, param := range params {
|
||||
switch param.(type) {
|
||||
case string:
|
||||
c.Pattern = param.(string)
|
||||
case time.Duration:
|
||||
timeout = param.(time.Duration)
|
||||
du := param.(time.Duration)
|
||||
if du != 0 {
|
||||
timeout = du
|
||||
}
|
||||
case func() string:
|
||||
callback = param.(func(string, error) interface{})
|
||||
callback = param.(func(string, Sender, error) interface{})
|
||||
}
|
||||
}
|
||||
if callback == nil {
|
||||
@@ -255,14 +259,14 @@ func (_ *BaseSender) Await(sender Sender, callback func(string, error) interface
|
||||
switch result.(type) {
|
||||
case string:
|
||||
waits.Delete(key)
|
||||
c.Result <- callback(result.(string), nil)
|
||||
c.Result <- callback(result.(string), c.Sender, nil)
|
||||
return
|
||||
case error:
|
||||
waits.Delete(key)
|
||||
c.Result <- callback("", result.(error))
|
||||
c.Result <- callback("", c.Sender, result.(error))
|
||||
}
|
||||
case <-time.After(timeout):
|
||||
waits.Delete(key)
|
||||
c.Result <- callback("", TimeOutError)
|
||||
c.Result <- callback("", c.Sender, TimeOutError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +165,36 @@ func init123() {
|
||||
v, _ := otto.ToValue(s.GetUsername())
|
||||
return v
|
||||
})
|
||||
vm.Set("Await", func(call otto.Value) interface{} {
|
||||
patternV, _ := call.Object().Get("pattern")
|
||||
timeoutV, _ := call.Object().Get("timeout")
|
||||
timeout, _ := timeoutV.ToInteger()
|
||||
pattern, _ := patternV.ToString()
|
||||
s.Await(s, func(s1 string, s2 Sender, e error) interface{} {
|
||||
if e != nil {
|
||||
return nil
|
||||
}
|
||||
v, err := call.Object().Call("callback", s1)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
deleteV, _ := v.Object().Get("delete")
|
||||
delete, _ := deleteV.ToBoolean()
|
||||
if delete {
|
||||
s2.Delete()
|
||||
}
|
||||
disappearV, _ := call.Object().Get("disappear")
|
||||
disappear, _ := disappearV.ToInteger()
|
||||
s2.Disappear(time.Millisecond * time.Duration(disappear))
|
||||
replyV, _ := v.Object().Get("reply")
|
||||
reply, _ := replyV.ToString()
|
||||
if reply != "" {
|
||||
return reply
|
||||
}
|
||||
return nil
|
||||
}, pattern, time.Millisecond*time.Duration(timeout))
|
||||
return otto.Value{}
|
||||
})
|
||||
vm.Set("GetUserID", func() otto.Value {
|
||||
v, _ := otto.ToValue(s.GetUserID())
|
||||
return v
|
||||
|
||||
+2
-16
@@ -154,9 +154,8 @@ func initSys() {
|
||||
},
|
||||
{
|
||||
Admin: true,
|
||||
Rules: []string{"set ? ? ?"},
|
||||
Rules: []string{"set ? ? ?", "delete ? ?"},
|
||||
Handle: func(s Sender) interface{} {
|
||||
// s.Disappear()
|
||||
b := Bucket(s.Get(0))
|
||||
if !IsBucket(b) {
|
||||
return errors.New("不存在的存储桶")
|
||||
@@ -164,7 +163,7 @@ func initSys() {
|
||||
old := b.Get(s.Get(1))
|
||||
b.Set(s.Get(1), s.Get(2))
|
||||
go func() {
|
||||
s.Await(s, func(_ string, e error) interface{} {
|
||||
s.Await(s, func(_ string, _ Sender, e error) interface{} {
|
||||
if e != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -175,19 +174,6 @@ func initSys() {
|
||||
return "操作成功,在60s内可\"撤回\"。"
|
||||
},
|
||||
},
|
||||
{
|
||||
Admin: true,
|
||||
Rules: []string{"delete ? ?"},
|
||||
Handle: func(s Sender) interface{} {
|
||||
s.Disappear()
|
||||
b := Bucket(s.Get(0))
|
||||
if !IsBucket(b) {
|
||||
return errors.New("不存在的存储桶")
|
||||
}
|
||||
b.Set(s.Get(1), "")
|
||||
return "删除成功"
|
||||
},
|
||||
},
|
||||
{
|
||||
Admin: true,
|
||||
Rules: []string{"get ? ?"},
|
||||
|
||||
Reference in New Issue
Block a user