koishi-plugin-echo-cave 1.16.16 → 1.18.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/README.md CHANGED
@@ -55,25 +55,9 @@ npm install koishi-plugin-echo-cave
55
55
  5. **查看自己的投稿**:发送命令 `cave.listen` 查看自己曾经存入的所有回声洞消息
56
56
  6. **查看自己的发言**:发送命令 `cave.trace` 查看自己曾经被他人存入的回声洞消息
57
57
 
58
- ## 📁 文件结构
59
-
60
- ```
61
- src/
62
- ├── index.ts # 插件主入口,命令注册和核心功能
63
- ├── cave-helper.ts # 回声洞消息发送辅助函数
64
- ├── forward-helper.ts # 转发消息处理辅助函数
65
- ├── media-helper.ts # 媒体文件保存辅助函数
66
- ├── msg-helper.ts # 消息发送辅助函数
67
- ├── cqcode-helper.ts # CQ 码处理辅助函数
68
- ├── onebot-helper.ts # OneBot 适配器辅助函数
69
- └── locales/
70
- └── zh-CN.json # 中文语言包
71
- ```
72
-
73
58
  ## 🔧 技术说明
74
59
 
75
60
  - 插件使用 Koishi 数据库系统存储消息记录
76
- - 图片会保存在 `data/cave/images` 目录下
77
61
  - 支持嵌套转发消息的处理
78
62
  - 自动检测重复消息,避免存储重复内容
79
63
  - 支持媒体文件大小限制
@@ -82,15 +66,15 @@ src/
82
66
 
83
67
  ## 🛠️ 配置选项
84
68
 
85
- | 配置项 | 类型 | 默认值 | 说明 |
86
- |-------|------|-------|------|
69
+ | 配置项 | 类型 | 默认值 | 说明 |
70
+ |--------------------------|---------|---------|-------------------------------|
87
71
  | `adminMessageProtection` | boolean | `false` | 开启管理员消息保护,使管理员发布的消息只能由其他管理员删除 |
88
- | `allowContributorDelete` | boolean | `true` | 允许消息投稿者删除自己投稿的消息 |
89
- | `allowSenderDelete` | boolean | `true` | 允许原始消息发送者删除自己被投稿的消息 |
90
- | `enableSizeLimit` | boolean | `false` | 启用媒体文件大小限制 |
91
- | `maxImageSize` | number | `2048` | 最大图片大小(KB) |
92
- | `maxVideoSize` | number | `512` | 最大视频大小(MB) |
93
- | `maxFileSize` | number | `512` | 最大文件大小(MB) |
72
+ | `allowContributorDelete` | boolean | `true` | 允许消息投稿者删除自己投稿的消息 |
73
+ | `allowSenderDelete` | boolean | `true` | 允许原始消息发送者删除自己被投稿的消息 |
74
+ | `enableSizeLimit` | boolean | `false` | 启用媒体文件大小限制 |
75
+ | `maxImageSize` | number | `2048` | 最大图片大小(KB) |
76
+ | `maxVideoSize` | number | `512` | 最大视频大小(MB) |
77
+ | `maxFileSize` | number | `512` | 最大文件大小(MB) |
94
78
 
95
79
  ## 📝 注意事项
96
80
 
@@ -1,4 +1,5 @@
1
1
  import { Context, Session } from 'koishi';
2
+ export declare function getUserIdFromNickname(session: Session, nickname: string, userId: number): Promise<number>;
2
3
  export declare function getUserName(ctx: Context, session: Session, userId: string): Promise<string>;
3
4
  /**
4
5
  * 检查用户是否属于指定群组
@@ -0,0 +1,14 @@
1
+ import { Schema } from 'koishi';
2
+ export interface Config {
3
+ adminMessageProtection?: boolean;
4
+ allowContributorDelete?: boolean;
5
+ allowSenderDelete?: boolean;
6
+ deleteMediaWhenDeletingMsg?: boolean;
7
+ enableSizeLimit?: boolean;
8
+ maxImageSize?: number;
9
+ maxVideoSize?: number;
10
+ maxFileSize?: number;
11
+ maxRecordSize?: number;
12
+ useBase64ForMedia?: boolean;
13
+ }
14
+ export declare const Config: Schema<Config>;
@@ -0,0 +1,3 @@
1
+ import { Config } from '../../config/config';
2
+ import { Context, Session } from 'koishi';
3
+ export declare function addCave(ctx: Context, session: Session, cfg: Config, userIds?: string[]): Promise<string>;
@@ -0,0 +1,4 @@
1
+ import { Config } from '../../config/config';
2
+ import { Context, Session } from 'koishi';
3
+ export declare function deleteCave(ctx: Context, session: Session, cfg: Config, id: number): Promise<string>;
4
+ export declare function deleteCaves(ctx: Context, session: Session, cfg: Config, ids: number[]): Promise<string>;
@@ -0,0 +1,5 @@
1
+ import { Config } from '../../config/config';
2
+ import { Context, Session } from 'koishi';
3
+ export declare function getCaveListByUser(ctx: Context, session: Session): Promise<string>;
4
+ export declare function getCaveListByOriginUser(ctx: Context, session: Session): Promise<string>;
5
+ export declare function getCave(ctx: Context, session: Session, cfg: Config, id: number): Promise<string>;
@@ -0,0 +1,2 @@
1
+ import { Context, Session } from 'koishi';
2
+ export declare function bindUsersToCave(ctx: Context, session: Session, id: number, userIds: string[]): Promise<string>;
@@ -0,0 +1,2 @@
1
+ import { Context, Session } from 'koishi';
2
+ export declare function searchCave(ctx: Context, session: Session, userIds: number): Promise<string>;
@@ -1,4 +1,5 @@
1
- import { Config, EchoCave } from './index';
1
+ import { Config } from '../../config/config';
2
+ import { EchoCave } from '../../index';
2
3
  import { Context, Session } from 'koishi';
3
4
  export declare function sendCaveMsg(ctx: Context, session: Session, caveMsg: EchoCave, cfg: Config): Promise<void>;
4
5
  export declare function formatDate(date: Date): string;
@@ -1,4 +1,4 @@
1
- import { Config } from './index';
1
+ import { Config } from '../../config/config';
2
2
  import { CQCode } from '@pynickle/koishi-plugin-adapter-onebot';
3
3
  import { Message } from '@pynickle/koishi-plugin-adapter-onebot/lib/types';
4
4
  import { Context, Session } from 'koishi';
@@ -1,4 +1,4 @@
1
- import { Config } from './index';
1
+ import { Config } from '../../config/config';
2
2
  import { CQCode } from '@pynickle/koishi-plugin-adapter-onebot';
3
3
  import { Context } from 'koishi';
4
4
  export declare function processMessageContent(ctx: Context, msg: CQCode[], cfg: Config): Promise<CQCode[]>;
package/lib/index.cjs CHANGED
@@ -51,6 +51,10 @@ var require_zh_CN = __commonJS({
51
51
  noMsgWithId: "\u{1F50D} \u672A\u627E\u5230\u8BE5 ID \u7684\u56DE\u58F0\u6D1E\u6D88\u606F",
52
52
  noTemplatesConfigured: "\u274C \u672A\u914D\u7F6E\u56DE\u58F0\u6D1E\u6A21\u677F\uFF0C\u8BF7\u5148\u914D\u7F6E\u6A21\u677F\uFF01"
53
53
  },
54
+ user: {
55
+ invalidAllMention: "\u274C \u4E0D\u652F\u6301 @\u5168\u4F53\u6210\u5458\uFF0C\u8BF7\u76F4\u63A5\u6307\u5B9A\u5177\u4F53\u7528\u6237\uFF01",
56
+ userNotInGroup: "\u274C \u63D0\u4F9B\u7684\u7528\u6237 ID \u4E0D\u5168\u5C5E\u4E8E\u8BE5\u7FA4\u7EC4\uFF01"
57
+ },
54
58
  templates: {
55
59
  forward: [
56
60
  "\u{1F300} \u56DE\u58F0\u6D1E #{id}\n\n\u4E00\u5219\u56DE\u58F0\u4ECE\u65F6\u5149\u4E2D\u98D8\u6765\u2014\u2014\n\u{1F4C5} {date} \xB7 \u6765\u81EA @{originName} \n\u{1F4EE} \u7531 @{userName} \u6295\u9012",
@@ -96,20 +100,36 @@ var require_zh_CN = __commonJS({
96
100
  noMsgQuoted: "\u{1F4A1} \u8BF7\u5F15\u7528\u4E00\u6761\u6D88\u606F\u540E\u518D\u4F7F\u7528\u6B64\u547D\u4EE4\uFF01",
97
101
  existingMsg: "\u267B\uFE0F \u8BE5\u6D88\u606F\u5DF2\u5B58\u5728\u4E8E\u56DE\u58F0\u6D1E\u7A74\u4E2D\uFF01",
98
102
  msgSaved: "\u2705 \u56DE\u58F0\u6D1E\u6D88\u606F\u5DF2\u6210\u529F\u5B58\u5165\uFF0C\u6D88\u606F ID\uFF1A{0}",
99
- msgFailedToSave: "\u274C \u56DE\u58F0\u6D1E\u4FDD\u5B58\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\uFF01",
100
- userNotInGroup: "\u274C \u63D0\u4F9B\u7684\u7528\u6237 ID \u4E0D\u5168\u5C5E\u4E8E\u8BE5\u7FA4\u7EC4\uFF01",
101
- invalidAllMention: "\u274C \u4E0D\u652F\u6301 @\u5168\u4F53\u6210\u5458\uFF0C\u8BF7\u76F4\u63A5\u6307\u5B9A\u5177\u4F53\u7528\u6237\uFF01"
103
+ msgFailedToSave: "\u274C \u56DE\u58F0\u6D1E\u4FDD\u5B58\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\uFF01"
102
104
  }
103
105
  },
104
- "cave.wipe": {
106
+ "cave.drop": {
105
107
  description: "\u62B9\u53BB\u7279\u5B9A id \u7684\u56DE\u58F0\u6D1E\u4FE1\u606F",
106
108
  messages: {
107
109
  noIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u5220\u9664\u7684\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF01",
108
110
  adminOnly: "\u26D4 \u8BE5\u6D88\u606F\u7531\u7BA1\u7406\u5458\u53D1\u5E03\uFF0C\u5DF2\u5F00\u542F\u7BA1\u7406\u5458\u6D88\u606F\u4FDD\u62A4\uFF0C\u53EA\u6709\u7BA1\u7406\u5458\u53EF\u4EE5\u5220\u9664\u3002",
109
111
  permissionDenied: "\u26D4 \u60A8\u6CA1\u6709\u6743\u9650\u5220\u9664\u6B64\u6D88\u606F\uFF01\u53EA\u6709\u7BA1\u7406\u5458\u53EF\u4EE5\u5220\u9664\u3002",
110
112
  contributorDeleteDenied: "\u26D4 \u60A8\u6CA1\u6709\u6743\u9650\u5220\u9664\u6B64\u6D88\u606F\uFF01\u5F53\u524D\u914D\u7F6E\u4E0D\u5141\u8BB8\u6295\u7A3F\u8005\u5220\u9664\u56DE\u58F0\u6D1E\u3002",
111
- senderDeleteDenied: "\u26D4 \u60A8\u6CA1\u6709\u6743\u9650\u5220\u9664\u6B64\u6D88\u606F\uFF01\u5F53\u524D\u914D\u7F6E\u4E0D\u5141\u8BB8\u539F\u59CB\u53D1\u9001\u8005\u5220\u9664\u56DE\u58F0\u6D1E\u3002",
112
- msgDeleted: "\u2705 \u5DF2\u6210\u529F\u62B9\u53BB\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF1A{0}"
113
+ senderDeleteDenied: "\u26D4 \u60A8\u6CA1\u6709\u6743\u9650\u5220\u9664\u6B64\u6D88\u606F\uFF01\u5F53\u524D\u914D\u7F6E\u4E0D\u5141\u8BB8\u539F\u59CB\u53D1\u9001\u8005\u5220\u9664\u56DE\u58F0\u6D1E\u3002"
114
+ }
115
+ },
116
+ "cave.purge": {
117
+ description: "\u62B9\u53BB\u591A\u4E2A id \u7684\u56DE\u58F0\u6D1E\u4FE1\u606F",
118
+ messages: {
119
+ noIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u5220\u9664\u7684\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF01",
120
+ msgDeleted: "\u2705 \u5DF2\u6210\u529F\u62B9\u53BB\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF1A{0}",
121
+ msgDeletedMultiple: "\u2705 \u5DF2\u6210\u529F\u62B9\u53BB {0} \u6761\u56DE\u58F0\u6D1E\u6D88\u606F\uFF01",
122
+ msgDeleteFailedAll: "\u274C \u6240\u6709\u6307\u5B9A\u7684\u56DE\u58F0\u6D1E\u6D88\u606F ID \u5747\u5220\u9664\u5931\u8D25\uFF1A{0}",
123
+ msgDeletePartial: "\u26A0\uFE0F \u90E8\u5206\u56DE\u58F0\u6D1E\u6D88\u606F\u5220\u9664\u5931\u8D25\uFF0C\u5931\u8D25 ID\uFF1A{0}"
124
+ }
125
+ },
126
+ "cave.search": {
127
+ description: "\u641C\u7D22\u4E0E\u6307\u5B9A\u7528\u6237\u76F8\u5173\u7684\u56DE\u58F0\u6D1E",
128
+ messages: {
129
+ noUserIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u641C\u7D22\u7684\u7528\u6237 ID\uFF01",
130
+ noValidUserIdProvided: "\u274C \u672A\u63D0\u4F9B\u6709\u6548\u7684\u7528\u6237 ID\uFF01",
131
+ noMatchingCaves: "\u{1F50D} \u672A\u627E\u5230\u4E0E\u7528\u6237 {0} \u76F8\u5173\u7684\u56DE\u58F0\u6D1E\u6D88\u606F",
132
+ searchResult: "\u{1F50D} \u5171\u627E\u5230 {0} \u6761\u4E0E\u8BE5\u7528\u6237\u76F8\u5173\u7684\u56DE\u58F0\u6D1E\u6D88\u606F\uFF1A\nID \u5217\u8868\uFF1A{1}"
113
133
  }
114
134
  },
115
135
  "cave.listen": {
@@ -134,9 +154,7 @@ var require_zh_CN = __commonJS({
134
154
  noIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u7ED1\u5B9A\u7528\u6237\u7684\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF01",
135
155
  noUserIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u7ED1\u5B9A\u7684\u7528\u6237 ID\uFF01",
136
156
  msgNotFound: "\u{1F50D} \u672A\u627E\u5230\u8BE5 ID \u7684\u56DE\u58F0\u6D1E\u6D88\u606F",
137
- userBoundSuccess: "\u2705 \u5DF2\u6210\u529F\u5C06\u7528\u6237\u7ED1\u5B9A\u5230\u56DE\u58F0\u6D1E #{0}\uFF01",
138
- userNotInGroup: "\u274C \u63D0\u4F9B\u7684\u7528\u6237 ID \u4E0D\u5168\u5C5E\u4E8E\u8BE5\u7FA4\u7EC4\uFF01",
139
- invalidAllMention: "\u274C \u4E0D\u652F\u6301 @\u5168\u4F53\u6210\u5458\uFF0C\u8BF7\u76F4\u63A5\u6307\u5B9A\u5177\u4F53\u7528\u6237\uFF01"
157
+ userBoundSuccess: "\u2705 \u5DF2\u6210\u529F\u5C06\u7528\u6237\u7ED1\u5B9A\u5230\u56DE\u58F0\u6D1E #{0}\uFF01"
140
158
  }
141
159
  }
142
160
  }
@@ -147,24 +165,44 @@ var require_zh_CN = __commonJS({
147
165
  // src/index.ts
148
166
  var index_exports = {};
149
167
  __export(index_exports, {
150
- Config: () => Config,
151
168
  apply: () => apply,
152
169
  inject: () => inject,
153
170
  name: () => name
154
171
  });
155
172
  module.exports = __toCommonJS(index_exports);
156
- var import_koishi_plugin_adapter_onebot = require("@pynickle/koishi-plugin-adapter-onebot");
173
+ var import_koishi_plugin_adapter_onebot2 = require("@pynickle/koishi-plugin-adapter-onebot");
157
174
 
158
- // src/cqcode-helper.ts
159
- var import_koishi = require("koishi");
160
- function createTextMsg(content) {
161
- return {
162
- type: "text",
163
- data: {
164
- text: content
165
- }
166
- };
175
+ // src/adapters/onebot/user.ts
176
+ async function getUserIdFromNickname(session, nickname, userId) {
177
+ const memberInfos = await session.onebot.getGroupMemberList(session.channelId);
178
+ const matches = memberInfos.filter((m) => m.nickname === nickname);
179
+ if (matches.length === 1) {
180
+ return matches[0].user_id;
181
+ }
182
+ return userId;
183
+ }
184
+ async function getUserName(ctx, session, userId) {
185
+ try {
186
+ const memberInfo = await session.onebot.getGroupMemberInfo(session.channelId, userId);
187
+ return memberInfo.card || memberInfo.nickname || userId;
188
+ } catch (error) {
189
+ ctx.logger.warn(`Failed to get group member info (userId: ${userId}):`, error);
190
+ return userId;
191
+ }
192
+ }
193
+ async function checkUsersInGroup(ctx, session, userIds) {
194
+ try {
195
+ const groupMembers = await session.onebot.getGroupMemberList(session.channelId);
196
+ const memberIds = groupMembers.map((member) => member.user_id.toString());
197
+ return userIds.every((userId) => memberIds.includes(userId));
198
+ } catch (error) {
199
+ ctx.logger.warn(`Failed to get group member list:`, error);
200
+ return false;
201
+ }
167
202
  }
203
+
204
+ // src/utils/msg/element-helper.ts
205
+ var import_koishi = require("koishi");
168
206
  function parseUserIds(userIds) {
169
207
  const parsedUserIds = [];
170
208
  for (const userId of userIds) {
@@ -190,7 +228,7 @@ function parseUserIds(userIds) {
190
228
  };
191
229
  }
192
230
 
193
- // src/media-helper.ts
231
+ // src/utils/media/media-helper.ts
194
232
  var import_axios = __toESM(require("axios"), 1);
195
233
  var import_node_fs = require("node:fs");
196
234
  var import_node_path = __toESM(require("node:path"), 1);
@@ -388,28 +426,228 @@ async function deleteMediaFilesFromMessage(ctx, content) {
388
426
  }
389
427
  }
390
428
 
391
- // src/onebot-helper.ts
392
- async function getUserName(ctx, session, userId) {
393
- try {
394
- const memberInfo = await session.onebot.getGroupMemberInfo(session.channelId, userId);
395
- return memberInfo.card || memberInfo.nickname || userId;
396
- } catch (error) {
397
- ctx.logger.warn(`Failed to get group member info (userId: ${userId}):`, error);
398
- return userId;
429
+ // src/core/parser/forward-parser.ts
430
+ async function reconstructForwardMsg(ctx, session, message, cfg) {
431
+ return Promise.all(
432
+ message.map(async (msg) => {
433
+ const content = await processForwardMessageContent(ctx, session, msg, cfg);
434
+ const senderNickname = msg.sender.nickname;
435
+ let senderUserId = msg.sender.user_id;
436
+ senderUserId = senderUserId === 1094950020 ? await getUserIdFromNickname(session, senderNickname, senderUserId) : senderUserId;
437
+ return {
438
+ type: "node",
439
+ data: {
440
+ user_id: senderUserId,
441
+ nickname: senderNickname,
442
+ content
443
+ }
444
+ };
445
+ })
446
+ );
447
+ }
448
+ async function processForwardMessageContent(ctx, session, msg, cfg) {
449
+ if (typeof msg.message === "string") {
450
+ return msg.message;
451
+ }
452
+ const firstElement = msg.message[0];
453
+ if (firstElement?.type === "forward") {
454
+ return reconstructForwardMsg(ctx, session, firstElement.data.content, cfg);
399
455
  }
456
+ return Promise.all(
457
+ msg.message.map(async (element) => {
458
+ return processMediaElement(ctx, element, cfg);
459
+ })
460
+ );
400
461
  }
401
- async function checkUsersInGroup(ctx, session, userIds) {
462
+
463
+ // src/core/parser/msg-parser.ts
464
+ async function processMessageContent(ctx, msg, cfg) {
465
+ return Promise.all(
466
+ msg.map(async (element) => {
467
+ if (element.type === "reply") {
468
+ return element;
469
+ }
470
+ return processMediaElement(ctx, element, cfg);
471
+ })
472
+ );
473
+ }
474
+
475
+ // src/core/command/add-cave.ts
476
+ var import_koishi_plugin_adapter_onebot = require("@pynickle/koishi-plugin-adapter-onebot");
477
+ async function addCave(ctx, session, cfg, userIds) {
478
+ if (!session.guildId) {
479
+ return session.text("echo-cave.general.privateChatReminder");
480
+ }
481
+ if (!session.quote) {
482
+ return session.text(".noMsgQuoted");
483
+ }
484
+ const { userId, channelId, quote } = session;
485
+ const messageId = quote.id;
486
+ let parsedUserIds = [];
487
+ if (userIds && userIds.length > 0) {
488
+ ctx.logger.info(`Original userIds in addCave: ${JSON.stringify(userIds)}`);
489
+ const result = parseUserIds(userIds);
490
+ if (result.error === "invalid_all_mention") {
491
+ return session.text("echo-cave.user.invalidAllMention");
492
+ }
493
+ parsedUserIds = result.parsedUserIds;
494
+ const isAllUsersInGroup = await checkUsersInGroup(ctx, session, parsedUserIds);
495
+ if (!isAllUsersInGroup) {
496
+ return session.text(".userNotInGroup");
497
+ }
498
+ }
499
+ let content;
500
+ let type;
501
+ if (quote.elements[0].type === "forward") {
502
+ type = "forward";
503
+ const message = await reconstructForwardMsg(
504
+ ctx,
505
+ session,
506
+ await session.onebot.getForwardMsg(messageId),
507
+ cfg
508
+ );
509
+ content = JSON.stringify(message);
510
+ } else {
511
+ type = "msg";
512
+ const message = (await session.onebot.getMsg(messageId)).message;
513
+ let msgJson;
514
+ if (typeof message === "string") {
515
+ msgJson = import_koishi_plugin_adapter_onebot.CQCode.parse(message);
516
+ } else {
517
+ if (message[0].type === "video" || message[0].type === "file") {
518
+ type = "forward";
519
+ }
520
+ msgJson = message;
521
+ }
522
+ content = JSON.stringify(await processMessageContent(ctx, msgJson, cfg));
523
+ }
524
+ await ctx.database.get("echo_cave", { content }).then((existing) => {
525
+ if (existing) {
526
+ return session.text(".existingMsg");
527
+ }
528
+ });
402
529
  try {
403
- const groupMembers = await session.onebot.getGroupMemberList(session.channelId);
404
- const memberIds = groupMembers.map((member) => member.user_id.toString());
405
- return userIds.every((userId) => memberIds.includes(userId));
530
+ const result = await ctx.database.create("echo_cave", {
531
+ channelId,
532
+ createTime: /* @__PURE__ */ new Date(),
533
+ userId,
534
+ originUserId: quote.user.id,
535
+ type,
536
+ content,
537
+ relatedUsers: parsedUserIds || []
538
+ });
539
+ return session.text(".msgSaved", [result.id]);
406
540
  } catch (error) {
407
- ctx.logger.warn(`Failed to get group member list:`, error);
408
- return false;
541
+ return session.text(".msgFailedToSave");
542
+ }
543
+ }
544
+
545
+ // src/core/command/delete-cave.ts
546
+ async function deleteCave(ctx, session, cfg, id) {
547
+ if (!session.guildId) {
548
+ return session.text("echo-cave.general.privateChatReminder");
549
+ }
550
+ if (!id) {
551
+ return session.text(".noIdProvided");
552
+ }
553
+ const caves = await ctx.database.get("echo_cave", id);
554
+ if (caves.length === 0) {
555
+ return session.text("echo-cave.general.noMsgWithId");
556
+ }
557
+ const caveMsg = caves[0];
558
+ const currentUserId = session.userId;
559
+ const user = await ctx.database.getUser(session.platform, currentUserId);
560
+ const userAuthority = user.authority;
561
+ const isCurrentUserAdmin = userAuthority >= 4;
562
+ if (cfg.adminMessageProtection) {
563
+ const caveUser = await ctx.database.getUser(session.platform, caveMsg.userId);
564
+ const isCaveUserAdmin = caveUser.authority >= 4;
565
+ if (isCaveUserAdmin && !isCurrentUserAdmin) {
566
+ return session.text(".adminOnly");
567
+ }
568
+ }
569
+ if (!isCurrentUserAdmin) {
570
+ if (currentUserId === caveMsg.userId) {
571
+ if (!cfg.allowContributorDelete) {
572
+ return session.text(".contributorDeleteDenied");
573
+ }
574
+ } else if (currentUserId === caveMsg.originUserId) {
575
+ if (!cfg.allowSenderDelete) {
576
+ return session.text(".senderDeleteDenied");
577
+ }
578
+ } else {
579
+ return session.text(".permissionDenied");
580
+ }
581
+ }
582
+ if (cfg.deleteMediaWhenDeletingMsg) {
583
+ await deleteMediaFilesFromMessage(ctx, caveMsg.content);
584
+ }
585
+ await ctx.database.remove("echo_cave", id);
586
+ return session.text(".msgDeleted", [id]);
587
+ }
588
+ async function deleteCaves(ctx, session, cfg, ids) {
589
+ if (!session.guildId) {
590
+ return session.text("echo-cave.general.privateChatReminder");
591
+ }
592
+ if (!ids) {
593
+ return session.text(".noIdProvided");
594
+ }
595
+ const failedIds = [];
596
+ const currentUserId = session.userId;
597
+ const user = await ctx.database.getUser(session.platform, currentUserId);
598
+ const userAuthority = user.authority;
599
+ const isCurrentUserAdmin = userAuthority >= 4;
600
+ const caves = await ctx.database.get("echo_cave", ids);
601
+ for (const cave of caves) {
602
+ const caveMsg = caves[0];
603
+ if (cfg.adminMessageProtection) {
604
+ const caveUser = await ctx.database.getUser(session.platform, caveMsg.userId);
605
+ const isCaveUserAdmin = caveUser.authority >= 4;
606
+ if (isCaveUserAdmin && !isCurrentUserAdmin) {
607
+ failedIds.push(cave.id);
608
+ continue;
609
+ }
610
+ }
611
+ let hasPermission = isCurrentUserAdmin;
612
+ if (!hasPermission) {
613
+ if (currentUserId === caveMsg.userId) {
614
+ hasPermission = cfg.allowContributorDelete;
615
+ } else if (currentUserId === caveMsg.originUserId) {
616
+ hasPermission = cfg.allowSenderDelete;
617
+ }
618
+ }
619
+ if (!hasPermission) {
620
+ failedIds.push(cave.id);
621
+ continue;
622
+ }
623
+ if (cfg.deleteMediaWhenDeletingMsg) {
624
+ await deleteMediaFilesFromMessage(ctx, caveMsg.content);
625
+ }
626
+ await ctx.database.remove("echo_cave", cave.id);
627
+ }
628
+ const foundIds = new Set(caves.map((r) => r.id));
629
+ const missingIds = ids.filter((id) => !foundIds.has(id));
630
+ failedIds.push(...missingIds);
631
+ if (failedIds.length === 0) {
632
+ return session.text(".msgDeletedMultiple", [ids.length]);
633
+ } else if (failedIds.length === ids.length) {
634
+ return session.text(".msgDeleteFailedAll", [failedIds.join(", ")]);
635
+ } else {
636
+ return session.text(".msgDeletePartial", [failedIds.join(", ")]);
409
637
  }
410
638
  }
411
639
 
412
- // src/cave-helper.ts
640
+ // src/utils/msg/cqcode-helper.ts
641
+ function createTextMsg(content) {
642
+ return {
643
+ type: "text",
644
+ data: {
645
+ text: content
646
+ }
647
+ };
648
+ }
649
+
650
+ // src/core/formatter/msg-formatter.ts
413
651
  async function sendCaveMsg(ctx, session, caveMsg, cfg) {
414
652
  const { channelId } = session;
415
653
  let content = JSON.parse(caveMsg.content);
@@ -481,115 +719,7 @@ function formatDate(date) {
481
719
  });
482
720
  }
483
721
 
484
- // src/forward-helper.ts
485
- async function reconstructForwardMsg(ctx, session, message, cfg) {
486
- return Promise.all(
487
- message.map(async (msg) => {
488
- const content = await processForwardMessageContent(ctx, session, msg, cfg);
489
- const senderNickname = msg.sender.nickname;
490
- let senderUserId = msg.sender.user_id;
491
- senderUserId = senderUserId === 1094950020 ? await getUserIdFromNickname(session, senderNickname, senderUserId) : senderUserId;
492
- return {
493
- type: "node",
494
- data: {
495
- user_id: senderUserId,
496
- nickname: senderNickname,
497
- content
498
- }
499
- };
500
- })
501
- );
502
- }
503
- async function getUserIdFromNickname(session, nickname, userId) {
504
- const memberInfos = await session.onebot.getGroupMemberList(session.channelId);
505
- const matches = memberInfos.filter((m) => m.nickname === nickname);
506
- if (matches.length === 1) {
507
- return matches[0].user_id;
508
- }
509
- return userId;
510
- }
511
- async function processForwardMessageContent(ctx, session, msg, cfg) {
512
- if (typeof msg.message === "string") {
513
- return msg.message;
514
- }
515
- const firstElement = msg.message[0];
516
- if (firstElement?.type === "forward") {
517
- return reconstructForwardMsg(ctx, session, firstElement.data.content, cfg);
518
- }
519
- return Promise.all(
520
- msg.message.map(async (element) => {
521
- return processMediaElement(ctx, element, cfg);
522
- })
523
- );
524
- }
525
-
526
- // src/msg-helper.ts
527
- async function processMessageContent(ctx, msg, cfg) {
528
- return Promise.all(
529
- msg.map(async (element) => {
530
- if (element.type === "reply") {
531
- return element;
532
- }
533
- return processMediaElement(ctx, element, cfg);
534
- })
535
- );
536
- }
537
-
538
- // src/index.ts
539
- var import_koishi_plugin_adapter_onebot2 = require("@pynickle/koishi-plugin-adapter-onebot");
540
- var import_koishi2 = require("koishi");
541
- var name = "echo-cave";
542
- var inject = ["database"];
543
- var Config = import_koishi2.Schema.object({
544
- adminMessageProtection: import_koishi2.Schema.boolean().default(false),
545
- allowContributorDelete: import_koishi2.Schema.boolean().default(true),
546
- allowSenderDelete: import_koishi2.Schema.boolean().default(true),
547
- deleteMediaWhenDeletingMsg: import_koishi2.Schema.boolean().default(true),
548
- enableSizeLimit: import_koishi2.Schema.boolean().default(false),
549
- maxImageSize: import_koishi2.Schema.number().default(2048),
550
- maxVideoSize: import_koishi2.Schema.number().default(512),
551
- maxFileSize: import_koishi2.Schema.number().default(512),
552
- maxRecordSize: import_koishi2.Schema.number().default(512),
553
- useBase64ForMedia: import_koishi2.Schema.boolean().default(false)
554
- }).i18n({
555
- "zh-CN": require_zh_CN()._config
556
- });
557
- function apply(ctx, cfg) {
558
- ctx.i18n.define("zh-CN", require_zh_CN());
559
- ctx.model.extend(
560
- "echo_cave",
561
- {
562
- id: "unsigned",
563
- channelId: "string",
564
- createTime: "timestamp",
565
- userId: "string",
566
- originUserId: "string",
567
- type: "string",
568
- content: "text",
569
- relatedUsers: "list"
570
- },
571
- {
572
- primary: "id",
573
- autoInc: true
574
- }
575
- );
576
- ctx.command("cave [id:number]").action(
577
- async ({ session }, id) => await getCave(ctx, session, cfg, id)
578
- );
579
- ctx.command("cave.echo [...userIds]").action(
580
- async ({ session }, ...userIds) => await addCave(ctx, session, cfg, userIds)
581
- );
582
- ctx.command("cave.wipe <id:number>").action(
583
- async ({ session }, id) => await deleteCave(ctx, session, cfg, id)
584
- );
585
- ctx.command("cave.listen").action(async ({ session }) => await getCaveListByUser(ctx, session));
586
- ctx.command("cave.trace").action(
587
- async ({ session }) => await getCaveListByOriginUser(ctx, session)
588
- );
589
- ctx.command("cave.bind <id:number> <...userIds>", { authority: 4 }).action(
590
- async ({ session }, id, ...userIds) => await bindUsersToCave(ctx, session, id, userIds)
591
- );
592
- }
722
+ // src/core/command/get-cave.ts
593
723
  async function getCaveListByUser(ctx, session) {
594
724
  if (!session.guildId) {
595
725
  return session.text("echo-cave.general.privateChatReminder");
@@ -652,115 +782,8 @@ async function getCave(ctx, session, cfg, id) {
652
782
  }
653
783
  await sendCaveMsg(ctx, session, caveMsg, cfg);
654
784
  }
655
- async function deleteCave(ctx, session, cfg, id) {
656
- if (!session.guildId) {
657
- return session.text("echo-cave.general.privateChatReminder");
658
- }
659
- if (!id) {
660
- return session.text(".noIdProvided");
661
- }
662
- const caves = await ctx.database.get("echo_cave", id);
663
- if (caves.length === 0) {
664
- return session.text("echo-cave.general.noMsgWithId");
665
- }
666
- const caveMsg = caves[0];
667
- const currentUserId = session.userId;
668
- const user = await ctx.database.getUser(session.platform, currentUserId);
669
- const userAuthority = user.authority;
670
- const isCurrentUserAdmin = userAuthority >= 4;
671
- if (cfg.adminMessageProtection) {
672
- const caveUser = await ctx.database.getUser(session.platform, caveMsg.userId);
673
- const isCaveUserAdmin = caveUser.authority >= 4;
674
- if (isCaveUserAdmin && !isCurrentUserAdmin) {
675
- return session.text(".adminOnly");
676
- }
677
- }
678
- if (!isCurrentUserAdmin) {
679
- if (currentUserId === caveMsg.userId) {
680
- if (!cfg.allowContributorDelete) {
681
- return session.text(".contributorDeleteDenied");
682
- }
683
- } else if (currentUserId === caveMsg.originUserId) {
684
- if (!cfg.allowSenderDelete) {
685
- return session.text(".senderDeleteDenied");
686
- }
687
- } else {
688
- return session.text(".permissionDenied");
689
- }
690
- }
691
- if (cfg.deleteMediaWhenDeletingMsg) {
692
- await deleteMediaFilesFromMessage(ctx, caveMsg.content);
693
- }
694
- await ctx.database.remove("echo_cave", id);
695
- return session.text(".msgDeleted", [id]);
696
- }
697
- async function addCave(ctx, session, cfg, userIds) {
698
- if (!session.guildId) {
699
- return session.text("echo-cave.general.privateChatReminder");
700
- }
701
- if (!session.quote) {
702
- return session.text(".noMsgQuoted");
703
- }
704
- const { userId, channelId, quote } = session;
705
- const messageId = quote.id;
706
- let parsedUserIds = [];
707
- if (userIds && userIds.length > 0) {
708
- ctx.logger.info(`Original userIds in addCave: ${JSON.stringify(userIds)}`);
709
- const result = parseUserIds(userIds);
710
- if (result.error === "invalid_all_mention") {
711
- return session.text(".invalidAllMention");
712
- }
713
- parsedUserIds = result.parsedUserIds;
714
- const isAllUsersInGroup = await checkUsersInGroup(ctx, session, parsedUserIds);
715
- if (!isAllUsersInGroup) {
716
- return session.text(".userNotInGroup");
717
- }
718
- }
719
- let content;
720
- let type;
721
- if (quote.elements[0].type === "forward") {
722
- type = "forward";
723
- const message = await reconstructForwardMsg(
724
- ctx,
725
- session,
726
- await session.onebot.getForwardMsg(messageId),
727
- cfg
728
- );
729
- content = JSON.stringify(message);
730
- } else {
731
- type = "msg";
732
- const message = (await session.onebot.getMsg(messageId)).message;
733
- let msgJson;
734
- if (typeof message === "string") {
735
- msgJson = import_koishi_plugin_adapter_onebot2.CQCode.parse(message);
736
- } else {
737
- if (message[0].type === "video" || message[0].type === "file") {
738
- type = "forward";
739
- }
740
- msgJson = message;
741
- }
742
- content = JSON.stringify(await processMessageContent(ctx, msgJson, cfg));
743
- }
744
- await ctx.database.get("echo_cave", { content }).then((existing) => {
745
- if (existing) {
746
- return session.text(".existingMsg");
747
- }
748
- });
749
- try {
750
- const result = await ctx.database.create("echo_cave", {
751
- channelId,
752
- createTime: /* @__PURE__ */ new Date(),
753
- userId,
754
- originUserId: quote.user.id,
755
- type,
756
- content,
757
- relatedUsers: parsedUserIds || []
758
- });
759
- return session.text(".msgSaved", [result.id]);
760
- } catch (error) {
761
- return session.text(".msgFailedToSave");
762
- }
763
- }
785
+
786
+ // src/core/command/misc/bind-user.ts
764
787
  async function bindUsersToCave(ctx, session, id, userIds) {
765
788
  if (!session.guildId) {
766
789
  return session.text("echo-cave.general.privateChatReminder");
@@ -768,31 +791,116 @@ async function bindUsersToCave(ctx, session, id, userIds) {
768
791
  if (!id) {
769
792
  return session.text(".noIdProvided");
770
793
  }
771
- if (!userIds || userIds.length === 0) {
794
+ if (!userIds) {
772
795
  return session.text(".noUserIdProvided");
773
796
  }
774
- let parsedUserIds = [];
775
797
  const result = parseUserIds(userIds);
776
798
  if (result.error === "invalid_all_mention") {
777
- return session.text(".invalidAllMention");
799
+ return session.text("echo-cave.user.invalidAllMention");
800
+ }
801
+ const parsedUserIds = result.parsedUserIds;
802
+ if (parsedUserIds.length === 0) {
803
+ return session.text(".noValidUserIdProvided");
778
804
  }
779
- parsedUserIds = result.parsedUserIds;
780
805
  const caves = await ctx.database.get("echo_cave", id);
781
806
  if (caves.length === 0) {
782
807
  return session.text("echo-cave.general.noMsgWithId");
783
808
  }
784
809
  const isAllUsersInGroup = await checkUsersInGroup(ctx, session, parsedUserIds);
785
810
  if (!isAllUsersInGroup) {
786
- return session.text(".userNotInGroup");
811
+ return session.text("echo-cave.user.userNotInGroup");
787
812
  }
788
813
  await ctx.database.set("echo_cave", id, {
789
814
  relatedUsers: parsedUserIds
790
815
  });
791
816
  return session.text(".userBoundSuccess", [id]);
792
817
  }
818
+
819
+ // src/core/command/search-cave.ts
820
+ var import_koishi2 = require("koishi");
821
+ async function searchCave(ctx, session, userIds) {
822
+ if (!session.guildId) {
823
+ return session.text("echo-cave.general.privateChatReminder");
824
+ }
825
+ if (!userIds) {
826
+ return session.text(".noUserIdProvided");
827
+ }
828
+ const result = parseUserIds(userIds.toString());
829
+ if (result.error === "invalid_all_mention") {
830
+ return session.text("echo-cave.user.invalidAllMention");
831
+ }
832
+ const parsedUserIds = result.parsedUserIds;
833
+ if (parsedUserIds.length === 0) {
834
+ return session.text(".noValidUserIdProvided");
835
+ }
836
+ const isUserInGroup = await checkUsersInGroup(ctx, session, parsedUserIds);
837
+ if (!isUserInGroup) {
838
+ return session.text("echo-cave.user.userNotInGroup");
839
+ }
840
+ const targetUserId = parsedUserIds[0];
841
+ const { channelId } = session;
842
+ const matchingCaves = await ctx.database.get(
843
+ "echo_cave",
844
+ (row) => import_koishi2.$.and(
845
+ import_koishi2.$.eq(row.channelId, channelId),
846
+ import_koishi2.$.or(import_koishi2.$.eq(row.originUserId, targetUserId), import_koishi2.$.in(targetUserId, row.relatedUsers))
847
+ )
848
+ );
849
+ if (matchingCaves.length === 0) {
850
+ return session.text(".noMatchingCaves", [targetUserId]);
851
+ }
852
+ const caveIds = matchingCaves.map((cave) => cave.id).join(", ");
853
+ const count = matchingCaves.length;
854
+ return session.text(".searchResult", [count, caveIds]);
855
+ }
856
+
857
+ // src/index.ts
858
+ var name = "echo-cave";
859
+ var inject = ["database"];
860
+ function apply(ctx, cfg) {
861
+ ctx.i18n.define("zh-CN", require_zh_CN());
862
+ ctx.model.extend(
863
+ "echo_cave",
864
+ {
865
+ id: "unsigned",
866
+ channelId: "string",
867
+ createTime: "timestamp",
868
+ userId: "string",
869
+ originUserId: "string",
870
+ type: "string",
871
+ content: "text",
872
+ relatedUsers: "list"
873
+ },
874
+ {
875
+ primary: "id",
876
+ autoInc: true
877
+ }
878
+ );
879
+ ctx.command("cave [id:number]").action(
880
+ async ({ session }, id) => await getCave(ctx, session, cfg, id)
881
+ );
882
+ ctx.command("cave.listen").action(async ({ session }) => await getCaveListByUser(ctx, session));
883
+ ctx.command("cave.trace").action(
884
+ async ({ session }) => await getCaveListByOriginUser(ctx, session)
885
+ );
886
+ ctx.command("cave.echo [...userIds]").action(
887
+ async ({ session }, ...userIds) => await addCave(ctx, session, cfg, userIds)
888
+ );
889
+ ctx.command("cave.drop <id:number>").action(
890
+ async ({ session }, id) => await deleteCave(ctx, session, cfg, id)
891
+ );
892
+ ctx.command("cave.purge <...ids:number>").action(
893
+ async ({ session }, ...ids) => await deleteCaves(ctx, session, cfg, ids)
894
+ );
895
+ ctx.command("cave.search <id:number>").action(
896
+ async ({ session }, id) => await searchCave(ctx, session, id)
897
+ );
898
+ ctx.command("cave.bind <id:number> <...userIds>", { authority: 4 }).action(
899
+ async ({ session }, id, ...userIds) => await bindUsersToCave(ctx, session, id, userIds)
900
+ );
901
+ }
793
902
  // Annotate the CommonJS export names for ESM import in node:
794
903
  0 && (module.exports = {
795
- Config,
796
904
  apply,
797
905
  inject,
798
906
  name
package/lib/index.d.ts CHANGED
@@ -1,20 +1,8 @@
1
1
  import '@pynickle/koishi-plugin-adapter-onebot';
2
- import { Context, Schema } from 'koishi';
2
+ import { Config } from './config/config';
3
+ import { Context } from 'koishi';
3
4
  export declare const name = "echo-cave";
4
5
  export declare const inject: string[];
5
- export interface Config {
6
- adminMessageProtection?: boolean;
7
- allowContributorDelete?: boolean;
8
- allowSenderDelete?: boolean;
9
- deleteMediaWhenDeletingMsg?: boolean;
10
- enableSizeLimit?: boolean;
11
- maxImageSize?: number;
12
- maxVideoSize?: number;
13
- maxFileSize?: number;
14
- maxRecordSize?: number;
15
- useBase64ForMedia?: boolean;
16
- }
17
- export declare const Config: Schema<Config>;
18
6
  export interface EchoCave {
19
7
  id: number;
20
8
  channelId: string;
@@ -1,4 +1,4 @@
1
- import { Config } from './index';
1
+ import { Config } from '../../config/config';
2
2
  import { Context } from 'koishi';
3
3
  export declare function saveMedia(ctx: Context, mediaElement: Record<string, any>, type: 'image' | 'video' | 'file' | 'record', cfg: Config): Promise<string>;
4
4
  export declare function processMediaElement(ctx: Context, element: any, cfg: Config): Promise<any>;
@@ -0,0 +1,6 @@
1
+ export declare function createTextMsg(content: string): {
2
+ type: string;
3
+ data: {
4
+ text: string;
5
+ };
6
+ };
@@ -0,0 +1,5 @@
1
+ export interface ParseResult {
2
+ parsedUserIds: string[];
3
+ error?: string;
4
+ }
5
+ export declare function parseUserIds(userIds: string[] | string): ParseResult;
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.16.16",
4
+ "version": "1.18.0",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",
@@ -1,11 +0,0 @@
1
- export interface ParseResult {
2
- parsedUserIds: string[];
3
- error?: string;
4
- }
5
- export declare function createTextMsg(content: string): {
6
- type: string;
7
- data: {
8
- text: string;
9
- };
10
- };
11
- export declare function parseUserIds(userIds: any[]): ParseResult;