@meteorstream/koishi-plugin-auto-reply 0.0.6 → 0.0.8
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 +10 -0
- package/lib/index.js +41 -22
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface Config {
|
|
|
4
4
|
}
|
|
5
5
|
export declare const Config: Schema<Schemastery.ObjectS<{
|
|
6
6
|
debugger: Schema<boolean, boolean>;
|
|
7
|
+
sendErrMsg: Schema<boolean, boolean>;
|
|
8
|
+
errMsg: Schema<string, string>;
|
|
7
9
|
}> | Schemastery.ObjectS<{
|
|
8
10
|
replyList: Schema<Schemastery.ObjectS<{
|
|
9
11
|
command: Schema<string, string>;
|
|
@@ -12,10 +14,12 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
12
14
|
address: Schema<Schemastery.ObjectS<{
|
|
13
15
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
14
16
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
17
|
+
weight: Schema<number, number>;
|
|
15
18
|
link: Schema<string, string>;
|
|
16
19
|
}>[], Schemastery.ObjectT<{
|
|
17
20
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
18
21
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
22
|
+
weight: Schema<number, number>;
|
|
19
23
|
link: Schema<string, string>;
|
|
20
24
|
}>[]>;
|
|
21
25
|
}>[], Schemastery.ObjectT<{
|
|
@@ -25,15 +29,19 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
25
29
|
address: Schema<Schemastery.ObjectS<{
|
|
26
30
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
27
31
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
32
|
+
weight: Schema<number, number>;
|
|
28
33
|
link: Schema<string, string>;
|
|
29
34
|
}>[], Schemastery.ObjectT<{
|
|
30
35
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
31
36
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
37
|
+
weight: Schema<number, number>;
|
|
32
38
|
link: Schema<string, string>;
|
|
33
39
|
}>[]>;
|
|
34
40
|
}>[]>;
|
|
35
41
|
}>, {
|
|
36
42
|
debugger: boolean;
|
|
43
|
+
sendErrMsg: boolean;
|
|
44
|
+
errMsg: string;
|
|
37
45
|
} & import("cosmokit").Dict & {
|
|
38
46
|
replyList: Schemastery.ObjectT<{
|
|
39
47
|
command: Schema<string, string>;
|
|
@@ -42,10 +50,12 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
42
50
|
address: Schema<Schemastery.ObjectS<{
|
|
43
51
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
44
52
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
53
|
+
weight: Schema<number, number>;
|
|
45
54
|
link: Schema<string, string>;
|
|
46
55
|
}>[], Schemastery.ObjectT<{
|
|
47
56
|
source: Schema<"本地" | "url", "本地" | "url">;
|
|
48
57
|
fileType: Schema<"文本" | "图片" | "音频" | "视频", "文本" | "图片" | "音频" | "视频">;
|
|
58
|
+
weight: Schema<number, number>;
|
|
49
59
|
link: Schema<string, string>;
|
|
50
60
|
}>[]>;
|
|
51
61
|
}>[];
|
package/lib/index.js
CHANGED
|
@@ -41,6 +41,11 @@ var fs = __toESM(require("fs/promises"));
|
|
|
41
41
|
var path = __toESM(require("path"));
|
|
42
42
|
var name = "auto-reply";
|
|
43
43
|
var Config = import_koishi.Schema.intersect([
|
|
44
|
+
import_koishi.Schema.object({
|
|
45
|
+
debugger: import_koishi.Schema.boolean().default(false).description("日志调试模式"),
|
|
46
|
+
sendErrMsg: import_koishi.Schema.boolean().default(false).description("响应失败时是否发送文字提示"),
|
|
47
|
+
errMsg: import_koishi.Schema.string().default("发生错误").description("响应失败时发送的文字提示")
|
|
48
|
+
}).description("开发者选项"),
|
|
44
49
|
import_koishi.Schema.object({
|
|
45
50
|
replyList: import_koishi.Schema.array(import_koishi.Schema.object({
|
|
46
51
|
command: import_koishi.Schema.string().description("指令"),
|
|
@@ -49,14 +54,12 @@ var Config = import_koishi.Schema.intersect([
|
|
|
49
54
|
address: import_koishi.Schema.array(import_koishi.Schema.object({
|
|
50
55
|
source: import_koishi.Schema.union(["本地", "url"]).description("来源").default("url"),
|
|
51
56
|
fileType: import_koishi.Schema.union(["文本", "图片", "音频", "视频"]).description("文件类型").default("文本"),
|
|
57
|
+
weight: import_koishi.Schema.number().description("权重(仅随机生效)").role("slider").min(1).max(100).step(1).default(1),
|
|
52
58
|
link: import_koishi.Schema.string().description("路径")
|
|
53
59
|
})).description("响应情况").role("table")
|
|
54
60
|
})).description("响应指令").role("table")
|
|
55
61
|
// exitCommandList: Schema.array(String).role('table').description('退出选择指令。<br>一行一个指令(此指令 在歌单内容中默认没有使用提示)').default(["0", "不听了"]),
|
|
56
|
-
})
|
|
57
|
-
import_koishi.Schema.object({
|
|
58
|
-
debugger: import_koishi.Schema.boolean().default(false).description("日志调试模式")
|
|
59
|
-
}).description("开发者选项")
|
|
62
|
+
})
|
|
60
63
|
]);
|
|
61
64
|
var lastUserIndexMap = /* @__PURE__ */ new Map();
|
|
62
65
|
function apply(ctx, config) {
|
|
@@ -65,38 +68,54 @@ function apply(ctx, config) {
|
|
|
65
68
|
const root = path.join(ctx.baseDir, "data", name);
|
|
66
69
|
await fs.mkdir(root, { recursive: true });
|
|
67
70
|
ctx.on("message", (session) => {
|
|
71
|
+
logInfo("收到消息:", JSON.stringify(session));
|
|
68
72
|
const matchList = config.replyList.filter((item) => {
|
|
69
73
|
return item.type == "完全匹配" ? session.content == item.command : session.content.indexOf(item.command) > -1;
|
|
70
74
|
});
|
|
71
75
|
if (matchList && matchList.length > 0) {
|
|
72
76
|
let match = matchList[0];
|
|
73
|
-
let link = "";
|
|
74
77
|
let index = 0;
|
|
75
78
|
if (match.mode == "随机") {
|
|
76
|
-
|
|
79
|
+
const totalWeight = match.address.reduce((sum, item) => sum + item.weight ? item.weight : 1, 0);
|
|
80
|
+
const random = Math.random() * totalWeight;
|
|
81
|
+
let currentWeight = 0;
|
|
82
|
+
for (let i = 0; i < match.address.length; i++) {
|
|
83
|
+
currentWeight += match.address[i].weight ? match.address[i].weight : 1;
|
|
84
|
+
if (random < currentWeight) {
|
|
85
|
+
index = i;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
77
89
|
} else {
|
|
78
90
|
lastUserIndexMap.set(match.command, lastUserIndexMap.get(match.command) ? lastUserIndexMap.get(match.command) : 0);
|
|
79
91
|
index = lastUserIndexMap.get(match.command);
|
|
80
92
|
index = ++index > match.address.length - 1 ? 0 : index;
|
|
81
93
|
lastUserIndexMap.set(match.command, index);
|
|
82
94
|
}
|
|
83
|
-
link = match.address[index].source == "本地" ? (0, import_url.pathToFileURL)(path.join(root, match.address[index].link)).href : match.address[index].link;
|
|
95
|
+
const link = match.address[index].source == "本地" ? (0, import_url.pathToFileURL)(path.join(root, match.address[index].link)).href : match.address[index].link;
|
|
84
96
|
logInfo(`匹配到指令[${match.command}]${match.mode}[${index}]:${link}`);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
try {
|
|
98
|
+
switch (match.address[index].fileType) {
|
|
99
|
+
case "文本":
|
|
100
|
+
session.send(link);
|
|
101
|
+
break;
|
|
102
|
+
case "图片":
|
|
103
|
+
session.send(import_koishi.h.image(link));
|
|
104
|
+
break;
|
|
105
|
+
case "视频":
|
|
106
|
+
session.send(import_koishi.h.video(link));
|
|
107
|
+
break;
|
|
108
|
+
case "音频":
|
|
109
|
+
session.send(import_koishi.h.audio(link));
|
|
110
|
+
break;
|
|
111
|
+
default:
|
|
112
|
+
session.send(link);
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (config.sendErrMsg) {
|
|
116
|
+
session.send(config.errMsg);
|
|
117
|
+
}
|
|
118
|
+
logInfo("发送失败:", error);
|
|
100
119
|
}
|
|
101
120
|
}
|
|
102
121
|
});
|