This commit is contained in:
1-6
2023-07-26 10:10:22 +08:00
parent 88cd60dfcd
commit 58c898aff2
9 changed files with 178 additions and 81 deletions
+2 -1
View File
@@ -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
+44 -41
View File
@@ -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 {
+4 -1
View File
@@ -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");`
}
+28
View File
@@ -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
-13
View File
@@ -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
+9 -3
View File
@@ -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<string | undefined>;
actionHandler?: (message: Message) => string | undefined | Promise<string | undefined>;
});
setActionHandler(func: (action: {}) => any): void;
receive(message: Message): Promise<Sender>;
@@ -84,4 +84,10 @@ declare class Adapter {
}
declare let sender: Sender;
declare function sleep(ms: number | undefined): Promise<unknown>;
export { Adapter, Bucket, sender, sleep };
declare let utils: {
parseCQText: (text: string, prefix?: string) => (string | {
type: string;
params: any;
})[];
};
export { Adapter, Bucket, sender, sleep, utils };
+40 -9
View File
@@ -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;
+50 -11
View File
@@ -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<string | undefined>;
actionHandler?: (
message: Message
) => string | undefined | Promise<string | undefined>;
}) {
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 };
+1 -2
View File
@@ -61,5 +61,4 @@ module.exports = {
allowlist: [/grpc/, "google-protobuf"],
}),
],
};
// npx webpack --config webpack.config.js
};