From 58c898aff23381b5f52ce11c4ca8fbd2b77ad51e Mon Sep 17 00:00:00 2001 From: 1-6 Date: Wed, 26 Jul 2023 10:10:22 +0800 Subject: [PATCH] x --- core/grpc_adapter.go | 3 +- core/grpc_download.go | 85 +++++++++++++++++++++------------------- core/grpc_plugins.go | 5 ++- proto3/note.sh | 28 +++++++++++++ proto3/note.txt | 13 ------ proto3/sillygirl.d.ts | 12 ++++-- proto3/sillygirl.js | 49 ++++++++++++++++++----- proto3/sillygirl.ts | 61 ++++++++++++++++++++++------ proto3/webpack.config.js | 3 +- 9 files changed, 178 insertions(+), 81 deletions(-) create mode 100644 proto3/note.sh delete mode 100644 proto3/note.txt diff --git a/core/grpc_adapter.go b/core/grpc_adapter.go index 1d0ca2a..0e17032 100644 --- a/core/grpc_adapter.go +++ b/core/grpc_adapter.go @@ -83,8 +83,9 @@ func (sg *SillyGirlService) AdapterReceive(ctx context.Context, req *srpc.Adapte msgs := map[string]interface{}{} bot_id := req.GetBotId() platform := req.GetPlatform() + // fmt.Println("a ...any", bot_id, "=", platform, string(utils.JsonMarshal(msgs))) json.Unmarshal([]byte(req.Value), &msgs) - adapter, err := GetAdapter(bot_id, platform) + adapter, err := GetAdapter(platform, bot_id) if err == nil { s := adapter.Receive(msgs) return &srpc.Default{Value: s.SetID()}, nil diff --git a/core/grpc_download.go b/core/grpc_download.go index eec906f..3a42147 100644 --- a/core/grpc_download.go +++ b/core/grpc_download.go @@ -22,62 +22,65 @@ type Language struct { } var plugin_dir = utils.ExecPath + "/plugins" +var release = "20230726" var languages = []Language{ { Name: "node", - Version: "20230725", + Version: release, Os: "linux", Arch: "amd64", - Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_linux_amd64.zip"}, + Links: []string{"https://gitee.com/sillybot/binary/releases/download/" + release + "/node_linux_amd64.zip"}, }, { Name: "node", - Version: "20230725", + Version: release, Os: "darwin", Arch: "arm64", - Links: []string{"https://gitee.com/sillybot/binary/releases/download/20230725/node_darwin_arm64.zip"}, + Links: []string{"https://gitee.com/sillybot/binary/releases/download/" + release + "/node_darwin_arm64.zip"}, }, } -func init() { - go func() { - for _, item := range languages { - if !(item.Os == runtime.GOOS && item.Arch == runtime.GOARCH) { - continue - } - func() { - dir := utils.ExecPath + "/language/" + item.Name - data, _ := os.ReadFile(dir + "/version") - if string(data) == item.Version { - return - } - os.MkdirAll(utils.ExecPath+"/language/"+item.Name, 0755) - resp, err := http.Get(item.Links[0]) - if err != nil { - return - } - defer resp.Body.Close() - zipfile := dir + "/" + item.Name + ".zip" - f, err := os.OpenFile(zipfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) - if err != nil { - return - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - if err != nil { - // fmt.Println(err) - return - } - defer os.Remove(zipfile) - if err := unzip(zipfile, 0755); err == nil { - os.WriteFile(dir+"/version", []byte(item.Version), 0755) - } else { - // fmt.Println(err) - } - }() +func initLanguage() { + // go func() { + for _, item := range languages { + if !(item.Os == runtime.GOOS && item.Arch == runtime.GOARCH) { + continue } - }() + func() { + dir := utils.ExecPath + "/language/" + item.Name + data, _ := os.ReadFile(dir + "/version") + if string(data) == item.Version { + return + } + console.Log("正在安装", item.Name, "执行环境....") + os.MkdirAll(utils.ExecPath+"/language/"+item.Name, 0755) + resp, err := http.Get(item.Links[0]) + if err != nil { + return + } + defer resp.Body.Close() + zipfile := dir + "/" + item.Name + ".zip" + f, err := os.OpenFile(zipfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) + if err != nil { + return + } + defer f.Close() + _, err = io.Copy(f, resp.Body) + if err != nil { + // fmt.Println(err) + return + } + defer os.Remove(zipfile) + if err := unzip(zipfile, 0755); err == nil { + os.WriteFile(dir+"/version", []byte(item.Version), 0755) + } else { + // fmt.Println(err) + } + console.Log("安装", item.Name, "执行环境成功") + }() + } + // }() } func unzip(filename string, perm fs.FileMode) error { diff --git a/core/grpc_plugins.go b/core/grpc_plugins.go index f76218d..c460ad3 100644 --- a/core/grpc_plugins.go +++ b/core/grpc_plugins.go @@ -28,6 +28,7 @@ func init() { var processes sync.Map func initNodePlugins() { + initLanguage() root := utils.ExecPath + "/plugins" plugins := []string{root} os.Mkdir(root, 0755) @@ -426,5 +427,7 @@ func defaultScript(title string) string { * @description 🐒这个人很懒什么都没有留下 * @author ` + sillyGirl.GetString("author", "佚名") + ` * @version v1.0.0 - */` + */ + + const { sender: s, Bucket, Adapter, sleep } = require("sillygirl");` } diff --git a/proto3/note.sh b/proto3/note.sh new file mode 100644 index 0000000..71f0a11 --- /dev/null +++ b/proto3/note.sh @@ -0,0 +1,28 @@ +protoc-gen-grpc --js_out=import_style=commonjs:. --grpc_out=. srpc.proto +protoc-gen-grpc --ts_out=service=grpc-node:. --grpc_out=. srpc.proto + +protoc --go_out=. -I. --go-grpc_out=. srpc.proto + +protoc --plugin=protoc-gen-ts=$(which protoc-gen-ts) --js_out=import_style=commonjs,binary:./ --ts_out=./ srpc.proto + +protoc --js_out=import_style=commonjs,binary:. --grpc_out=. srpc.proto + + +#ok +protoc --go_out=. -I. --go-grpc_out=. srpc.proto +protoc-gen-grpc --ts_out=service=grpc-node:. --grpc_out=. srpc.proto + +#打包 +npx webpack --config webpack.config.js + +#linux编译: +scp /Users/a1-6/Code/sillyplus/proto3/dist/sillygirl.js root@imdraw.com:/root/node/node-18.16.1/lib +ssh root@imdraw.com +cd /root/node/node-18.16.1 && ninja -C out/Release && scp -P 20211 out/Release/node a1-6@frp2.echowxsy.cn:/Users/a1-6/Code/nodes/node_linux_amd64 +# +macos编译: +cp /Users/a1-6/Code/sillyplus/proto3/dist/sillygirl.js /Users/a1-6/Code/node/node-v18.16.1/lib/sillygirl.js && cd /Users/a1-6/Code/node/node-v18.16.1 && ninja -C out/Release && cp out/Release/node /Users/a1-6/Code/nodes/node_darwin_arm64 + +#压缩 +cd /Users/a1-6/Code/nodes/node_darwin_arm64 && zip node_darwin_arm64.zip node +cd /Users/a1-6/Code/nodes/node_linux_amd64 && zip node_linux_amd64.zip node \ No newline at end of file diff --git a/proto3/note.txt b/proto3/note.txt deleted file mode 100644 index 863d16f..0000000 --- a/proto3/note.txt +++ /dev/null @@ -1,13 +0,0 @@ -protoc-gen-grpc --js_out=import_style=commonjs:. --grpc_out=. srpc.proto -protoc-gen-grpc --ts_out=service=grpc-node:. --grpc_out=. srpc.proto - -protoc --go_out=. -I. --go-grpc_out=. srpc.proto - -protoc --plugin=protoc-gen-ts=$(which protoc-gen-ts) --js_out=import_style=commonjs,binary:./ --ts_out=./ srpc.proto - -protoc --js_out=import_style=commonjs,binary:. --grpc_out=. srpc.proto - - -//ok -protoc --go_out=. -I. --go-grpc_out=. srpc.proto -protoc-gen-grpc --ts_out=service=grpc-node:. --grpc_out=. srpc.proto \ No newline at end of file diff --git a/proto3/sillygirl.d.ts b/proto3/sillygirl.d.ts index bb14ed6..68399ae 100644 --- a/proto3/sillygirl.d.ts +++ b/proto3/sillygirl.d.ts @@ -73,8 +73,8 @@ declare class Adapter { constructor(options: { platform?: string; bot_id?: string; - replyHandler?: (message: Message) => string | undefined; - actionHandler?: (message: Message) => string | undefined; + replyHandler?: (message: Message) => string | undefined | Promise; + actionHandler?: (message: Message) => string | undefined | Promise; }); setActionHandler(func: (action: {}) => any): void; receive(message: Message): Promise; @@ -84,4 +84,10 @@ declare class Adapter { } declare let sender: Sender; declare function sleep(ms: number | undefined): Promise; -export { Adapter, Bucket, sender, sleep }; +declare let utils: { + parseCQText: (text: string, prefix?: string) => (string | { + type: string; + params: any; + })[]; +}; +export { Adapter, Bucket, sender, sleep, utils }; diff --git a/proto3/sillygirl.js b/proto3/sillygirl.js index 8bfc65e..6431418 100644 --- a/proto3/sillygirl.js +++ b/proto3/sillygirl.js @@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.sleep = exports.sender = exports.Bucket = exports.Adapter = void 0; +exports.utils = exports.sleep = exports.sender = exports.Bucket = exports.Adapter = void 0; const srpc_1 = require("./srpc"); const grpc_1 = __importStar(require("@grpc/grpc-js")); let client = new srpc_1.srpc.SillyGirlServiceClient("localhost:50051", grpc_1.credentials.createInsecure()); @@ -538,23 +538,23 @@ class Adapter { this.bot_id = options.bot_id; if (options.replyHandler) { const call = client.AdapterRegist(); - let callback = options.replyHandler; - call.on("data", (response) => { - // console.log("start on data") + call.on("data", async (response) => { let message = JSON.parse(response.value); const { echo, __type__ } = message; delete message.__type__; delete message.echo; - if (__type__ == "reply") { + if (__type__ == "reply" && options.replyHandler) { + let v = (await options.replyHandler(message)) ?? ""; call.write(new srpc_1.srpc.AdapterRegistRequest({ bot_id: echo, - platform: callback(message), + platform: v, })); } if (__type__ == "action" && options.actionHandler) { + let v = await options.actionHandler(message); call.write(new srpc_1.srpc.AdapterRegistRequest({ bot_id: echo, - platform: options.actionHandler(message), + platform: v, })); } // console.log("end on data") @@ -637,9 +637,40 @@ async function sleep(ms) { } exports.sleep = sleep; class Console { - error = (message, ...optionalParams) => { - }; + error = (message, ...optionalParams) => { }; info = (message, ...optionalParams) => { }; log = (message, ...optionalParams) => { }; debug = (message, ...optionalParams) => { }; } +let utils = { + parseCQText: (text, prefix = "CQ") => { + const cqRegex = new RegExp(`\\[${prefix}:(\\w+)(.*?)\\]`, "g"); + const cqMatches = text.matchAll(cqRegex); + const result = []; + let lastIndex = 0; + for (const match of cqMatches) { + // 添加 CQ 码前的文本 + const matchIndex = text.indexOf(match[0], lastIndex); + if (matchIndex > lastIndex) { + result.push(text.slice(lastIndex, matchIndex)); + } + // 解析 CQ 码 + const params = {}; + const paramRegex = /(\w+)=([^,]+)/g; + const paramMatches = match[2].matchAll(paramRegex); + for (const paramMatch of paramMatches) { + params[paramMatch[1]] = paramMatch[2].trim(); + } + result.push({ + type: match[1], + params: params, + }); + lastIndex = matchIndex + match[0].length; + } + if (lastIndex < text.length) { + result.push(text.slice(lastIndex)); + } + return result; + }, +}; +exports.utils = utils; diff --git a/proto3/sillygirl.ts b/proto3/sillygirl.ts index 0062e87..2cc4e6f 100644 --- a/proto3/sillygirl.ts +++ b/proto3/sillygirl.ts @@ -619,33 +619,39 @@ class Adapter { constructor(options: { platform?: string; bot_id?: string; - replyHandler?: (message: Message) => string | undefined; - actionHandler?: (message: Message) => string | undefined; + replyHandler?: ( + message: Message + ) => string | undefined | Promise; + actionHandler?: ( + message: Message + ) => string | undefined | Promise; }) { this.platform = options.platform; this.bot_id = options.bot_id; if (options.replyHandler) { const call = client.AdapterRegist(); - let callback: any = options.replyHandler; - call.on("data", (response) => { + // let callback: any = ; + call.on("data", async (response) => { // console.log("start on data") let message = JSON.parse(response.value); const { echo, __type__ } = message; delete message.__type__; delete message.echo; - if (__type__ == "reply") { + if (__type__ == "reply" && options.replyHandler) { + let v = (await options.replyHandler(message)) ?? ""; call.write( new srpc.AdapterRegistRequest({ bot_id: echo, - platform: callback(message), + platform: v, }) ); } if (__type__ == "action" && options.actionHandler) { + let v = await options.actionHandler(message); call.write( new srpc.AdapterRegistRequest({ bot_id: echo, - platform: options.actionHandler(message), + platform: v, }) ); } @@ -737,12 +743,45 @@ async function sleep(ms: number | undefined) { } class Console { - error = (message?: any, ...optionalParams: any[]) => { - - }; + error = (message?: any, ...optionalParams: any[]) => {}; info = (message?: any, ...optionalParams: any[]) => {}; log = (message?: any, ...optionalParams: any[]) => {}; debug = (message?: any, ...optionalParams: any[]) => {}; } -export { Adapter, Bucket, sender, sleep }; +let utils = { + parseCQText: (text: string, prefix = "CQ") => { + const cqRegex = new RegExp(`\\[${prefix}:(\\w+)(.*?)\\]`, "g"); + const cqMatches = text.matchAll(cqRegex); + const result = []; + + let lastIndex = 0; + for (const match of cqMatches) { + // 添加 CQ 码前的文本 + const matchIndex = text.indexOf(match[0], lastIndex); + if (matchIndex > lastIndex) { + result.push(text.slice(lastIndex, matchIndex)); + } + + // 解析 CQ 码 + const params: any = {}; + const paramRegex = /(\w+)=([^,]+)/g; + const paramMatches = match[2].matchAll(paramRegex); + for (const paramMatch of paramMatches) { + params[paramMatch[1]] = paramMatch[2].trim(); + } + result.push({ + type: match[1], + params: params, + }); + + lastIndex = matchIndex + match[0].length; + } + if (lastIndex < text.length) { + result.push(text.slice(lastIndex)); + } + return result; + }, +}; + +export { Adapter, Bucket, sender, sleep, utils }; diff --git a/proto3/webpack.config.js b/proto3/webpack.config.js index 856d922..3ded44f 100644 --- a/proto3/webpack.config.js +++ b/proto3/webpack.config.js @@ -61,5 +61,4 @@ module.exports = { allowlist: [/grpc/, "google-protobuf"], }), ], -}; -// npx webpack --config webpack.config.js +}; \ No newline at end of file