From 10f300626ac9efaee123be608113e86b50186c50 Mon Sep 17 00:00:00 2001 From: cdle Date: Tue, 6 Jun 2023 13:50:35 +0800 Subject: [PATCH] x --- README.md | 5 +++- core/adapter.go | 52 +++++++++++++++++++++++++----------------- core/node_script.go | 18 +++++++++++++++ core/plugin_publish.go | 3 +++ core/plugin_utils.go | 6 +++++ 5 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 core/node_script.go diff --git a/README.md b/README.md index c2dc682..4d0d5b6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ 一个不太有用的机器人,不生产消息,只搬运消息。 ## 特性 - - 简单易用的消息搬运功能。 - 简单强大的自定义回复功能。 - 完整支持 ECMAScript 5.1 的插件系统,基于 [otto](https://github.com/robertkrimen/otto)。 @@ -520,3 +519,7 @@ md5(string): string; //加密 running(): boolean; //服务是否运行 uuid(): string; //生成uuid ``` + +### 项目赞助 +打开微信扫一扫,深入了解作者~ +![](https://raw.githubusercontent.com/cdle/sillyGirl/main/appreciate.jpg) diff --git a/core/adapter.go b/core/adapter.go index cb56ef1..ccf9660 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -497,30 +497,40 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { // "uuid": utils.GenUUID(), } if sender.f.reply == nil { - matched := false + var any *common.Function + var one *common.Function for _, function := range Functions { - if function.Reply != nil && function.Reply.Platform == platform && (len(function.Reply.BotsID) == 0 || Contains(function.Reply.BotsID, platform)) { - message_id := "" - function.Handle(&Faker{ - Type: "*", - }, func(vm *goja.Runtime) { - obj := vm.NewObject() - for k, v := range msg { - obj.Set(k, v) - } - proxy := vm.NewProxy(obj, &goja.ProxyTrapConfig{ - Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) { - message_id = fmt.Sprint(value.Export()) - return true - }, - }) - vm.Set("msg", proxy) - vm.Set("message", proxy) - }) - return message_id, nil + if function.Reply != nil && function.Reply.Platform == platform { + if len(function.Reply.BotsID) == 0 { + any = function + } else if Contains(function.Reply.BotsID, sender.f.botid) { + one = function + } } } - if !matched { + if one == nil && any != nil { + one = any + } + if one != nil { + message_id := "" + one.Handle(&Faker{ + Type: "*", + }, func(vm *goja.Runtime) { + obj := vm.NewObject() + for k, v := range msg { + obj.Set(k, v) + } + proxy := vm.NewProxy(obj, &goja.ProxyTrapConfig{ + Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) { + message_id = fmt.Sprint(value.Export()) + return true + }, + }) + vm.Set("msg", proxy) + vm.Set("message", proxy) + }) + return message_id, nil + } else { c := MsgChan{ Msg: msg, Chan: make(chan string), diff --git a/core/node_script.go b/core/node_script.go new file mode 100644 index 0000000..b4eb2d4 --- /dev/null +++ b/core/node_script.go @@ -0,0 +1,18 @@ +package core + +func Script(uuid string) map[string]interface{} { + su := &ScriptUtils{ + script: plugins.GetString(uuid), + } + var o = map[string]interface{}{ + "get": su.GetValue, + "save": func() { + plugins.Set(uuid, su.script) + }, + } + o["set"] = func(key, value string) map[string]interface{} { + su.SetValue(key, value) + return o + } + return o +} diff --git a/core/plugin_publish.go b/core/plugin_publish.go index c702f1a..436d820 100644 --- a/core/plugin_publish.go +++ b/core/plugin_publish.go @@ -81,6 +81,9 @@ func publicScript(str string) string { if su.GetValue("title") == "" { su.SetValue("title", "无名脚本") } + if su.GetValue("message") == "" { + su.DeleteValue("message") + } create_at := su.GetValue("create_at") if _, err := time.Parse("2006-01-02 15:04:05", create_at); create_at == "" || err != nil { su.SetValue("create_at", time.Now().Format("2006-01-02 15:04:05")) diff --git a/core/plugin_utils.go b/core/plugin_utils.go index e192331..3280d28 100644 --- a/core/plugin_utils.go +++ b/core/plugin_utils.go @@ -404,6 +404,12 @@ func SetPluginMethod(vm *goja.Runtime, uuid string, on_start bool) { vm.Set("getReplyMessage", func(plt string, bots_id []string) *goja.Promise { return GetReplyMessage(vm, plt, bots_id) }) + vm.Set("Script", func(str string) interface{} { + if str == "" { + str = uuid + } + return Script(str) + }) } func EncryptPlugin(script string) string {