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