koishi-plugin-echo-cave 1.15.0 → 1.16.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.cjs CHANGED
@@ -96,7 +96,8 @@ var require_zh_CN = __commonJS({
96
96
  noMsgQuoted: "\u{1F4A1} \u8BF7\u5F15\u7528\u4E00\u6761\u6D88\u606F\u540E\u518D\u4F7F\u7528\u6B64\u547D\u4EE4\uFF01",
97
97
  existingMsg: "\u267B\uFE0F \u8BE5\u6D88\u606F\u5DF2\u5B58\u5728\u4E8E\u56DE\u58F0\u6D1E\u7A74\u4E2D\uFF01",
98
98
  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"
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"
100
101
  }
101
102
  },
102
103
  "cave.wipe": {
@@ -125,6 +126,17 @@ var require_zh_CN = __commonJS({
125
126
  msgListHeader: "\u{1F4DC} \u60A8\u5728\u672C\u9891\u9053\u53D1\u8A00\u7684\u56DE\u58F0\u6D1E\u6D88\u606F\u5217\u8868\uFF1A\n",
126
127
  msgListItem: "ID: {0} | \u521B\u5EFA\u65F6\u95F4: {1}\n"
127
128
  }
129
+ },
130
+ "cave.bind": {
131
+ description: "\u5C06\u7528\u6237\u7ED1\u5B9A\u5230\u7279\u5B9A\u56DE\u58F0\u6D1E",
132
+ messages: {
133
+ noIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u7ED1\u5B9A\u7528\u6237\u7684\u56DE\u58F0\u6D1E\u6D88\u606F ID\uFF01",
134
+ noUserIdProvided: "\u274C \u8BF7\u63D0\u4F9B\u8981\u7ED1\u5B9A\u7684\u7528\u6237 ID\uFF01",
135
+ msgNotFound: "\u{1F50D} \u672A\u627E\u5230\u8BE5 ID \u7684\u56DE\u58F0\u6D1E\u6D88\u606F",
136
+ userBoundSuccess: "\u2705 \u5DF2\u6210\u529F\u5C06\u7528\u6237\u7ED1\u5B9A\u5230\u56DE\u58F0\u6D1E #{0}\uFF01",
137
+ userAlreadyBound: "\u2139\uFE0F \u8BE5\u7528\u6237\u5DF2\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
+ }
128
140
  }
129
141
  }
130
142
  };
@@ -360,6 +372,16 @@ async function getUserName(ctx, session, userId) {
360
372
  return userId;
361
373
  }
362
374
  }
375
+ async function checkUsersInGroup(ctx, session, userIds) {
376
+ try {
377
+ const groupMembers = await session.onebot.getGroupMemberList(session.channelId);
378
+ const memberIds = groupMembers.map((member) => member.user_id.toString());
379
+ return userIds.every((userId) => memberIds.includes(userId));
380
+ } catch (error) {
381
+ ctx.logger.warn(`\u83B7\u53D6\u7FA4\u6210\u5458\u5217\u8868\u5931\u8D25\uFF1A`, error);
382
+ return false;
383
+ }
384
+ }
363
385
 
364
386
  // src/cave-helper.ts
365
387
  async function sendCaveMsg(ctx, session, caveMsg, cfg) {
@@ -373,11 +395,19 @@ async function sendCaveMsg(ctx, session, caveMsg, cfg) {
373
395
  const date = formatDate(caveMsg.createTime);
374
396
  const originName = await getUserName(ctx, session, caveMsg.originUserId);
375
397
  const userName = await getUserName(ctx, session, caveMsg.userId);
398
+ let relatedUsersFormatted = "";
399
+ if (caveMsg.relatedUsers && caveMsg.relatedUsers.length > 0) {
400
+ const relatedUserNames = await Promise.all(
401
+ caveMsg.relatedUsers.map(async (userId) => await getUserName(ctx, session, userId))
402
+ );
403
+ relatedUsersFormatted = relatedUserNames.join(", ");
404
+ }
376
405
  const templateData = {
377
406
  id: caveMsg.id.toString(),
378
407
  date,
379
408
  originName,
380
409
  userName,
410
+ relatedUsers: relatedUsersFormatted,
381
411
  nl: "\n"
382
412
  };
383
413
  const TEMPLATE_COUNT = 5;
@@ -509,7 +539,8 @@ function apply(ctx, cfg) {
509
539
  userId: "string",
510
540
  originUserId: "string",
511
541
  type: "string",
512
- content: "text"
542
+ content: "text",
543
+ relatedUsers: "list"
513
544
  },
514
545
  {
515
546
  primary: "id",
@@ -519,7 +550,9 @@ function apply(ctx, cfg) {
519
550
  ctx.command("cave [id:number]").action(
520
551
  async ({ session }, id) => await getCave(ctx, session, cfg, id)
521
552
  );
522
- ctx.command("cave.echo").action(async ({ session }) => await addCave(ctx, session, cfg));
553
+ ctx.command("cave.echo [...userIds:string]").action(
554
+ async ({ session }, ...userIds) => await addCave(ctx, session, cfg, userIds)
555
+ );
523
556
  ctx.command("cave.wipe <id:number>").action(
524
557
  async ({ session }, id) => await deleteCave(ctx, session, cfg, id)
525
558
  );
@@ -527,6 +560,9 @@ function apply(ctx, cfg) {
527
560
  ctx.command("cave.trace").action(
528
561
  async ({ session }) => await getCaveListByOriginUser(ctx, session)
529
562
  );
563
+ ctx.command("cave.bind <id:number> <...userIds:string>", { authority: 4 }).action(
564
+ async ({ session }, id, ...userIds) => await bindUsersToCave(ctx, session, id, userIds)
565
+ );
530
566
  }
531
567
  async function getCaveListByUser(ctx, session) {
532
568
  if (!session.guildId) {
@@ -632,7 +668,7 @@ async function deleteCave(ctx, session, cfg, id) {
632
668
  await ctx.database.remove("echo_cave", id);
633
669
  return session.text(".msgDeleted", [id]);
634
670
  }
635
- async function addCave(ctx, session, cfg) {
671
+ async function addCave(ctx, session, cfg, userIds) {
636
672
  if (!session.guildId) {
637
673
  return session.text("echo-cave.general.privateChatReminder");
638
674
  }
@@ -671,6 +707,12 @@ async function addCave(ctx, session, cfg) {
671
707
  return session.text(".existingMsg");
672
708
  }
673
709
  });
710
+ if (userIds && userIds.length > 0) {
711
+ const isAllUsersInGroup = await checkUsersInGroup(ctx, session, userIds);
712
+ if (!isAllUsersInGroup) {
713
+ return session.text(".userNotInGroup");
714
+ }
715
+ }
674
716
  try {
675
717
  const result = await ctx.database.create("echo_cave", {
676
718
  channelId,
@@ -678,13 +720,37 @@ async function addCave(ctx, session, cfg) {
678
720
  userId,
679
721
  originUserId: quote.user.id,
680
722
  type,
681
- content
723
+ content,
724
+ relatedUsers: userIds || []
682
725
  });
683
726
  return session.text(".msgSaved", [result.id]);
684
727
  } catch (error) {
685
728
  return session.text(".msgFailedToSave");
686
729
  }
687
730
  }
731
+ async function bindUsersToCave(ctx, session, id, userIds) {
732
+ if (!session.guildId) {
733
+ return session.text("echo-cave.general.privateChatReminder");
734
+ }
735
+ if (!id) {
736
+ return session.text(".noIdProvided");
737
+ }
738
+ if (!userIds || userIds.length === 0) {
739
+ return session.text(".noUserIdProvided");
740
+ }
741
+ const caves = await ctx.database.get("echo_cave", id);
742
+ if (caves.length === 0) {
743
+ return session.text("echo-cave.general.noMsgWithId");
744
+ }
745
+ const isAllUsersInGroup = await checkUsersInGroup(ctx, session, userIds);
746
+ if (!isAllUsersInGroup) {
747
+ return session.text(".userNotInGroup");
748
+ }
749
+ await ctx.database.set("echo_cave", id, {
750
+ relatedUsers: userIds
751
+ });
752
+ return session.text(".userBoundSuccess", [id]);
753
+ }
688
754
  // Annotate the CommonJS export names for ESM import in node:
689
755
  0 && (module.exports = {
690
756
  Config,
package/lib/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export interface EchoCave {
23
23
  originUserId: string;
24
24
  type: 'forward' | 'msg';
25
25
  content: string;
26
+ relatedUsers: string[];
26
27
  }
27
28
  declare module 'koishi' {
28
29
  interface Tables {
@@ -1,2 +1,6 @@
1
1
  import { Context, Session } from 'koishi';
2
2
  export declare function getUserName(ctx: Context, session: Session, userId: string): Promise<string>;
3
+ /**
4
+ * 检查用户是否属于指定群组
5
+ */
6
+ export declare function checkUsersInGroup(ctx: Context, session: Session, userIds: string[]): Promise<boolean>;
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.15.0",
4
+ "version": "1.16.0",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",