@dgck81lnn/koishi-plugin-music 0.1.6 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +3 -2
- package/lib/index.js +18 -8
- package/package.json +1 -1
- package/browser/index.html +0 -8
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Computed, Context, Schema } from "koishi";
|
|
2
|
-
export declare const name = "
|
|
2
|
+
export declare const name = "musicjs";
|
|
3
3
|
export declare const inject: string[];
|
|
4
4
|
export interface Config {
|
|
5
|
-
evalCommand:
|
|
5
|
+
evalCommand: string;
|
|
6
|
+
evalReturnMethod: "expr" | "process.stdout";
|
|
6
7
|
noise: Computed<boolean>;
|
|
7
8
|
}
|
|
8
9
|
export declare const Config: Schema<Config>;
|
package/lib/index.js
CHANGED
|
@@ -3,14 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Config = exports.inject = exports.name = void 0;
|
|
4
4
|
exports.apply = apply;
|
|
5
5
|
const koishi_1 = require("koishi");
|
|
6
|
+
const promises_1 = require("fs/promises");
|
|
6
7
|
const path_1 = require("path");
|
|
7
|
-
|
|
8
|
-
exports.name = "music";
|
|
8
|
+
exports.name = "musicjs";
|
|
9
9
|
exports.inject = ["puppeteer"];
|
|
10
10
|
exports.Config = koishi_1.Schema.object({
|
|
11
|
-
evalCommand: koishi_1.Schema.
|
|
12
|
-
.
|
|
11
|
+
evalCommand: koishi_1.Schema.string()
|
|
12
|
+
.role("textarea")
|
|
13
|
+
.default("glot --language=javascript")
|
|
13
14
|
.description("用于安全执行 js 代码的指令。"),
|
|
15
|
+
evalReturnMethod: koishi_1.Schema.union([
|
|
16
|
+
koishi_1.Schema.from("process.stdout").description("process.stdout.write()(适用 glot 等指令)"),
|
|
17
|
+
koishi_1.Schema.from("expr").description("表达式(适用 eval 等指令)"),
|
|
18
|
+
])
|
|
19
|
+
.default("process.stdout")
|
|
20
|
+
.description("在 js 代码执行指令中返回结果的方式。"),
|
|
14
21
|
noise: koishi_1.Schema.computed(Boolean)
|
|
15
22
|
.default(true)
|
|
16
23
|
.description("是否添加白噪音来尝试规避 QQ 的语音编码杂音问题。"),
|
|
@@ -88,6 +95,7 @@ const gutterFunc = (f) => {
|
|
|
88
95
|
};
|
|
89
96
|
function apply(ctx, config) {
|
|
90
97
|
ctx.i18n.define("zh", require("./locales/zh"));
|
|
98
|
+
const synthCode = (0, promises_1.readFile)((0, path_1.resolve)(__dirname, "../browser/synth.js"), "utf-8");
|
|
91
99
|
ctx
|
|
92
100
|
.command("musicjs <code:rawtext>", { strictOptions: true })
|
|
93
101
|
.action(async ({ session }, code) => {
|
|
@@ -100,9 +108,9 @@ function apply(ctx, config) {
|
|
|
100
108
|
return koishi_1.h.text(String(e));
|
|
101
109
|
}
|
|
102
110
|
let gutteredCode = `(${gutterFunc})(function($){with($){\n${code}\n}})`;
|
|
103
|
-
if (config.
|
|
111
|
+
if (config.evalReturnMethod === "process.stdout")
|
|
104
112
|
gutteredCode = `process.stdout.write(${gutteredCode})`;
|
|
105
|
-
ctx.logger.debug(config.evalCommand, gutteredCode);
|
|
113
|
+
ctx.logger.debug("%o (%s) %o", config.evalCommand, config.evalReturnMethod, gutteredCode);
|
|
106
114
|
const evalArgv = koishi_1.Argv.parse(config.evalCommand);
|
|
107
115
|
evalArgv.tokens.push({
|
|
108
116
|
content: koishi_1.h.escape(gutteredCode),
|
|
@@ -120,11 +128,13 @@ function apply(ctx, config) {
|
|
|
120
128
|
return koishi_1.h.text(data);
|
|
121
129
|
}
|
|
122
130
|
const page = await ctx.puppeteer.page();
|
|
123
|
-
await page.goto((0, url_1.pathToFileURL)((0, path_1.resolve)(__dirname, "../browser/index.html")).href);
|
|
124
131
|
const opt = {
|
|
125
132
|
noise: session.resolve(config.noise),
|
|
126
133
|
};
|
|
127
|
-
|
|
134
|
+
ctx.logger.debug("synth options: %o", opt);
|
|
135
|
+
const base64 = (await page.evaluate(
|
|
136
|
+
// prettier-ignore
|
|
137
|
+
`${await synthCode}; synth(${data}, ${JSON.stringify(opt)}).then(encodeWav).then(arrayBufferToBase64)`));
|
|
128
138
|
page.close().catch(() => { });
|
|
129
139
|
return koishi_1.h.audio("data:audio/wav;base64," + base64);
|
|
130
140
|
});
|
package/package.json
CHANGED