koishi-plugin-smmcat-selfhelp 0.0.1

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.
@@ -0,0 +1,3 @@
1
+ declare function setOrCreateFile(path: string, data: string): Promise<void>;
2
+ declare function getOrCreateFile(path: string, isArr?: boolean): Promise<string>;
3
+ export { setOrCreateFile, getOrCreateFile };
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "smmcat-selfhelp";
3
+ export interface Config {
4
+ mapAddress: string;
5
+ overtime: number;
6
+ debug: boolean;
7
+ }
8
+ export declare const Config: Schema<Config>;
9
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,273 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name2 in all)
10
+ __defProp(target, name2, { get: all[name2], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Config: () => Config,
34
+ apply: () => apply,
35
+ name: () => name
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+ var import_koishi = require("koishi");
39
+ var import_promises = __toESM(require("fs/promises"));
40
+ var import_path2 = __toESM(require("path"));
41
+
42
+ // src/mapTool.ts
43
+ var import_fs = __toESM(require("fs"));
44
+ var import_path = __toESM(require("path"));
45
+ var createPathMapByDir = /* @__PURE__ */ __name((folderPath) => {
46
+ const files = import_fs.default.readdirSync(folderPath);
47
+ let folders = [];
48
+ for (const file of files) {
49
+ const filePath = import_path.default.join(folderPath, file);
50
+ const stat = import_fs.default.statSync(filePath);
51
+ if (stat.isDirectory()) {
52
+ let folder = { name: file, child: [] };
53
+ const titleFilePath = import_path.default.join(filePath, "title.txt");
54
+ if (import_fs.default.existsSync(titleFilePath)) {
55
+ const titleContent = import_fs.default.readFileSync(titleFilePath, "utf8").trim();
56
+ folder.title = titleContent;
57
+ }
58
+ const resultFilePath = import_path.default.join(filePath, "result.txt");
59
+ if (import_fs.default.existsSync(resultFilePath)) {
60
+ const resultContent = import_fs.default.readFileSync(resultFilePath, "utf8").trim();
61
+ folder.child = resultContent;
62
+ } else {
63
+ folder.child = createPathMapByDir(filePath);
64
+ }
65
+ folders.push(folder);
66
+ }
67
+ }
68
+ return folders;
69
+ }, "createPathMapByDir");
70
+ var createDirMapByObject = /* @__PURE__ */ __name((folders, parentPath) => {
71
+ for (const folder of folders) {
72
+ const folderPath = import_path.default.join(parentPath, folder.name);
73
+ import_fs.default.mkdirSync(folderPath);
74
+ if (folder.title) {
75
+ const titleFilePath = import_path.default.join(folderPath, "title.txt");
76
+ import_fs.default.writeFileSync(titleFilePath, folder.title);
77
+ }
78
+ if (typeof folder.child === "string") {
79
+ const resultFilePath = import_path.default.join(folderPath, "result.txt");
80
+ import_fs.default.writeFileSync(resultFilePath, folder.child);
81
+ } else {
82
+ createDirMapByObject(folder.child, folderPath);
83
+ }
84
+ }
85
+ }, "createDirMapByObject");
86
+
87
+ // src/index.ts
88
+ var name = "smmcat-selfhelp";
89
+ var Config = import_koishi.Schema.object({
90
+ mapAddress: import_koishi.Schema.string().default("./data/selfHelp").description("菜单结构放置位置"),
91
+ overtime: import_koishi.Schema.number().default(3e4).description("菜单访问的超时时间"),
92
+ debug: import_koishi.Schema.boolean().default(false).description("日志查看更多信息")
93
+ });
94
+ function apply(ctx, config) {
95
+ const addTemplate = /* @__PURE__ */ __name(async (upath) => {
96
+ const obj = [
97
+ {
98
+ name: "1.自助服务",
99
+ child: [
100
+ {
101
+ name: "1.实例问题",
102
+ title: "这是通过 title.text 生成的提示标题",
103
+ child: [
104
+ { name: "1.koishi实例无法启动", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183327.png?x-oss-process=image/resize,h_130") + "可以社区发帖求助" },
105
+ { name: "2.无法访问插件市场", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183224.png?x-oss-process=image/resize,h_130") + "在 market 插件的配置项设置镜像源" }
106
+ ]
107
+ },
108
+ { name: "1.插件问题", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183321.png?x-oss-process=image/resize,h_130") + "插件问题找插件作者啊" }
109
+ ]
110
+ },
111
+ {
112
+ name: "2.自由内容",
113
+ child: [
114
+ { name: "1.快乐", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183237.png?x-oss-process=image/resize,h_130") + "但是并不快乐" },
115
+ { name: "2.喜欢", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183237.png?x-oss-process=image/resize,h_130") + "这可能是最后的帮助" },
116
+ {
117
+ name: "3.学习",
118
+ title: "这是一个目录下的标题",
119
+ child: [
120
+ { name: "1.女装的优雅姿态", child: import_koishi.h.image("https://static.kivo.wiki/images/students/%E9%99%86%E5%85%AB%E9%AD%94%20%E9%98%BF%E9%9C%B2/gallery/%E5%88%9D%E5%A7%8B%E7%AB%8B%E7%BB%98%E5%B7%AE%E5%88%86/QQ%E6%88%AA%E5%9B%BE20211115183327.png?x-oss-process=image/resize,h_130") + "通过女装可以锻炼自己,锻炼兄弟" }
121
+ ]
122
+ }
123
+ ]
124
+ },
125
+ { name: "3.说明文档", child: "这是 smmcat-helpself 的默认结构,您可以通过 result.text 文件创建最终给定的回复" }
126
+ ];
127
+ try {
128
+ await createDirMapByObject(obj, upath);
129
+ } catch (error) {
130
+ console.log(error);
131
+ }
132
+ }, "addTemplate");
133
+ const selfhelpMap = {
134
+ // 基地址
135
+ upath: import_path2.default.join(ctx.baseDir, config.mapAddress),
136
+ mapInfo: [],
137
+ // 初始化路径
138
+ async initPath() {
139
+ try {
140
+ await import_promises.default.access(this.upath);
141
+ } catch (error) {
142
+ try {
143
+ await import_promises.default.mkdir(this.upath, { recursive: true });
144
+ await addTemplate(this.upath);
145
+ } catch (error2) {
146
+ console.error(error2);
147
+ }
148
+ }
149
+ },
150
+ // 初始化菜单结构
151
+ async init() {
152
+ await this.initPath();
153
+ this.mapInfo = createPathMapByDir(this.upath);
154
+ config.debug && console.log(JSON.stringify(this.mapInfo, null, " "));
155
+ config.debug && console.log("[smmcat-selfhelp]:自助菜单构建完成");
156
+ },
157
+ getMenu(goal, callback) {
158
+ let selectMenu = this.mapInfo;
159
+ let end = false;
160
+ let indePath = [];
161
+ let PathName = [];
162
+ if (!goal) {
163
+ callback && callback({ selectMenu, lastPath: "", crumbs: "", end });
164
+ return;
165
+ }
166
+ let title = null;
167
+ const indexList = goal.split("-").map((item) => Number(item));
168
+ indexList.some((item) => {
169
+ indePath.push(item);
170
+ PathName.push(selectMenu[item - 1]?.name.length > 6 ? selectMenu[item - 1]?.name.slice(0, 6) + "..." : selectMenu[item - 1]?.name);
171
+ title = selectMenu[item - 1]?.title || null;
172
+ if (selectMenu.length < item) {
173
+ selectMenu = void 0;
174
+ indePath.pop();
175
+ PathName.pop();
176
+ callback && callback({ selectMenu, lastPath: indePath.join("-"), crumbs: PathName.slice(-3).join(">"), end });
177
+ return true;
178
+ }
179
+ if (selectMenu && typeof selectMenu === "object") {
180
+ selectMenu = selectMenu[item - 1].child;
181
+ if (typeof selectMenu === "string") {
182
+ end = true;
183
+ callback && callback({ selectMenu, lastPath: indePath.join("-"), crumbs: PathName.slice(-3).join(">"), end });
184
+ return true;
185
+ }
186
+ }
187
+ });
188
+ end || callback && callback({ selectMenu, title, lastPath: indePath.join("-"), crumbs: PathName.slice(-3).join(">"), end });
189
+ },
190
+ // 菜单渲染到界面
191
+ markScreen(pathLine) {
192
+ let goalItem = {};
193
+ this.getMenu(pathLine, (ev) => {
194
+ goalItem = ev;
195
+ });
196
+ return this.format(goalItem);
197
+ },
198
+ format(goalItem) {
199
+ if (!goalItem.selectMenu) {
200
+ return {
201
+ msg: "",
202
+ err: true
203
+ };
204
+ }
205
+ if (goalItem.end) {
206
+ return {
207
+ msg: goalItem.selectMenu.replace(/\\/g, "") + (goalItem.crumbs ? `
208
+
209
+ [当前位置] ${goalItem.crumbs}` : "\n\n主菜单") + "\n#.上页\n0.退出",
210
+ err: false
211
+ };
212
+ } else {
213
+ return {
214
+ msg: (goalItem.crumbs ? `[当前位置]
215
+ ${goalItem.crumbs}
216
+ ` : "主菜单\n") + `----------------------------
217
+ ` + (goalItem.title ? `${goalItem.title}
218
+
219
+ ` : "") + `${goalItem.selectMenu.map((item) => item.name).join("\n") + "\n\n#.上页\n0.退出"}
220
+ ----------------------------
221
+ `,
222
+ err: false
223
+ };
224
+ }
225
+ }
226
+ };
227
+ const useUserList = {};
228
+ ctx.on("ready", () => {
229
+ selfhelpMap.init();
230
+ });
231
+ ctx.command("自助菜单").action(async ({ session }) => {
232
+ await session.send("请回复任意内容进入自助菜单环节");
233
+ useUserList[session.userId] = true;
234
+ });
235
+ ctx.middleware(async (session, next) => {
236
+ if (!useUserList[session.userId]) {
237
+ return next();
238
+ }
239
+ const proce = [];
240
+ while (true) {
241
+ if (!useUserList[session.userId]) {
242
+ break;
243
+ }
244
+ const data = selfhelpMap.markScreen(proce.join("-"));
245
+ if (data.err) {
246
+ proce.pop();
247
+ const data2 = selfhelpMap.markScreen(proce.join("-"));
248
+ await session.send("操作不对,请重新输入:\n注意需要输入指定下标");
249
+ await session.send(data2.msg);
250
+ }
251
+ await session.send(data.msg);
252
+ const res = await session.prompt(config.overtime);
253
+ if (!res || res == "0") {
254
+ useUserList[session.userId] = false;
255
+ res == "0" && await session.send("已退出自助服务");
256
+ break;
257
+ }
258
+ if (res == "#") {
259
+ proce.pop();
260
+ } else {
261
+ proce.push(res);
262
+ }
263
+ }
264
+ return next();
265
+ });
266
+ }
267
+ __name(apply, "apply");
268
+ // Annotate the CommonJS export names for ESM import in node:
269
+ 0 && (module.exports = {
270
+ Config,
271
+ apply,
272
+ name
273
+ });
@@ -0,0 +1,13 @@
1
+ interface Folder {
2
+ name: string;
3
+ title?: string;
4
+ child: Folder[] | string;
5
+ }
6
+ interface pathItem {
7
+ name: string;
8
+ title?: string;
9
+ child: Folder[] | string;
10
+ }
11
+ export declare const createPathMapByDir: (folderPath: string) => Folder[];
12
+ export declare const createDirMapByObject: (folders: pathItem[], parentPath: string) => void;
13
+ export {};
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "koishi-plugin-smmcat-selfhelp",
3
+ "description": "用户自助服务系统",
4
+ "version": "0.0.1",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "chatbot",
14
+ "koishi",
15
+ "plugin"
16
+ ],
17
+ "peerDependencies": {
18
+ "koishi": "4.18.0"
19
+ }
20
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-smmcat-selfhelp
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-smmcat-selfhelp?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-smmcat-selfhelp)
4
+
5
+ 用户自助服务系统