koishi-plugin-echo-cave 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # koishi-plugin-cave
2
2
 
3
- [![npm](https://img.shields.io/npm/v/koishi-plugin-cave?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-cave)
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-echo-cave?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-echo-cave)
4
4
 
5
5
  Group echo cave
@@ -0,0 +1,2 @@
1
+ import { Context, Session, h } from 'koishi';
2
+ export declare function saveImages(ctx: Context, session: Session, imgElement: h): Promise<string>;
package/lib/index.cjs CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name2 in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // src/index.ts
@@ -27,6 +37,31 @@ __export(index_exports, {
27
37
  module.exports = __toCommonJS(index_exports);
28
38
  var import_koishi = require("koishi");
29
39
  var import_koishi_plugin_adapter_onebot = require("koishi-plugin-adapter-onebot");
40
+
41
+ // src/image-helper.ts
42
+ var import_node_fs = require("node:fs");
43
+ var import_node_path = __toESM(require("node:path"), 1);
44
+ async function saveImages(ctx, session, imgElement) {
45
+ const imgUrl = imgElement.attrs.src;
46
+ const originalImgName = imgElement.attrs.file;
47
+ const lastDotIndex = originalImgName.lastIndexOf(".");
48
+ let imgExt = "";
49
+ if (lastDotIndex === -1) {
50
+ imgExt = "png";
51
+ } else {
52
+ imgExt = originalImgName.substring(lastDotIndex + 1);
53
+ }
54
+ const imgPath = import_node_path.default.join(ctx.baseDir, "data", "cave", "images");
55
+ const imgName = (/* @__PURE__ */ new Date()).toISOString();
56
+ let fullImgPath = import_node_path.default.join(imgPath, `${imgName}.${imgExt}`);
57
+ ctx.logger.info(`Saving image from URL: ${imgUrl} to path: ${fullImgPath}`);
58
+ const buffer = await ctx.http.get(imgUrl);
59
+ if (buffer.byteLength === 0) throw new Error("Image download failed");
60
+ await import_node_fs.promises.writeFile(fullImgPath, Buffer.from(buffer));
61
+ return fullImgPath;
62
+ }
63
+
64
+ // src/index.ts
30
65
  var name = "echo-cave";
31
66
  var inject = ["database"];
32
67
  var Config = import_koishi.Schema.object({});
@@ -39,7 +74,7 @@ function apply(ctx) {
39
74
  createTime: "timestamp",
40
75
  userId: "string",
41
76
  originUserId: "string",
42
- content: "text"
77
+ content: "string"
43
78
  },
44
79
  {
45
80
  primary: "id",
@@ -65,7 +100,7 @@ async function getCave(ctx, session) {
65
100
  if (caves.length === 0) {
66
101
  return '\u{1F680} \u56DE\u58F0\u6D1E\u4E2D\u6682\u65E0\u6D88\u606F\uFF0C\u5FEB\u4F7F\u7528 "cave.echo" \u547D\u4EE4\u6DFB\u52A0\u7B2C\u4E00\u6761\u6D88\u606F\u5427\uFF01';
67
102
  }
68
- return caves[Math.floor(Math.random() * caves.length)].content;
103
+ return import_koishi.h.parse(caves[Math.floor(Math.random() * caves.length)].content);
69
104
  }
70
105
  async function addCave(ctx, session) {
71
106
  if (!session.guildId) {
@@ -74,11 +109,17 @@ async function addCave(ctx, session) {
74
109
  if (!session.quote) {
75
110
  return "\u{1F4A1} \u8BF7\u5F15\u7528\u4E00\u6761\u6D88\u606F\u540E\u518D\u4F7F\u7528\u6B64\u547D\u4EE4\uFF01";
76
111
  }
77
- const { userId, channelId } = session;
78
- const content = session.quote.content;
79
- ctx.logger("echo-cave").info(
80
- `User ${userId} is adding a message to the echo cave: ${content}`
81
- );
112
+ const { userId, channelId, quote } = session;
113
+ const elements = quote.elements;
114
+ elements.forEach((element) => {
115
+ ctx.logger("echo-cave").info(
116
+ `Processing element type: ${element.type}`
117
+ );
118
+ if (element.type === "img") {
119
+ element.attrs.src = saveImages(ctx, session, element);
120
+ }
121
+ });
122
+ const content = JSON.stringify(elements);
82
123
  await ctx.database.get("echo_cave", { content }).then((existing) => {
83
124
  if (existing) {
84
125
  return "\u267B\uFE0F \u8BE5\u6D88\u606F\u5DF2\u5B58\u5728\u4E8E\u56DE\u58F0\u6D1E\u7A74\u4E2D\uFF01";
@@ -89,7 +130,7 @@ async function addCave(ctx, session) {
89
130
  channelId,
90
131
  createTime: /* @__PURE__ */ new Date(),
91
132
  userId,
92
- originUserId: userId,
133
+ originUserId: quote.user.id,
93
134
  content
94
135
  });
95
136
  if (session.onebot) {
@@ -97,7 +138,7 @@ async function addCave(ctx, session) {
97
138
  session.channelId,
98
139
  `\u2705 \u56DE\u58F0\u6D1E\u6D88\u606F\u5DF2\u6210\u529F\u5B58\u5165\uFF0C\u6D88\u606F ID\uFF1A${result.id}`
99
140
  );
100
- ctx.setInterval(
141
+ ctx.setTimeout(
101
142
  async () => await session.onebot.deleteMsg(messageId),
102
143
  5e3
103
144
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-echo-cave",
3
3
  "description": "Group echo cave",
4
- "version": "1.0.0",
4
+ "version": "1.1.1",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",