koishi-plugin-echo-cave 1.20.2 → 1.21.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.
@@ -11,5 +11,6 @@ export interface Config {
11
11
  maxRecordSize?: number;
12
12
  useBase64ForMedia?: boolean;
13
13
  sendAllAsForwardMsg?: boolean;
14
+ rankingTopCount?: number;
14
15
  }
15
16
  export declare const Config: Schema<Config>;
@@ -0,0 +1,5 @@
1
+ import { Config } from '../../config/config';
2
+ import { Context, Session } from 'koishi';
3
+ export type Period = 'day' | 'week' | 'month' | 'all';
4
+ export declare const SUPPORTED_PERIODS: Period[];
5
+ export declare function getRanking(ctx: Context, session: Session, cfg: Config, period?: string): Promise<void>;
package/lib/index.cjs CHANGED
@@ -43,7 +43,8 @@ var require_zh_CN = __commonJS({
43
43
  maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)",
44
44
  maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)",
45
45
  useBase64ForMedia: "\u662F\u5426\u4F7F\u7528 Base64 \u7F16\u7801\u53D1\u9001\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u8BFB\u53D6 base64 \u7F16\u7801\u53D1\u9001\u800C\u4E0D\u662F\u4F7F\u7528 file uri",
46
- sendAllAsForwardMsg: "\u662F\u5426\u5C06\u6240\u6709\u6D88\u606F\u4EE5\u8F6C\u53D1\u6D88\u606F\u5F62\u5F0F\u53D1\u9001\uFF0C\u5F00\u542F\u540E\u666E\u901A\u6D88\u606F\u4E5F\u4F1A\u8F6C\u6362\u4E3A\u8F6C\u53D1\u6D88\u606F\u683C\u5F0F"
46
+ sendAllAsForwardMsg: "\u662F\u5426\u5C06\u6240\u6709\u6D88\u606F\u4EE5\u8F6C\u53D1\u6D88\u606F\u5F62\u5F0F\u53D1\u9001\uFF0C\u5F00\u542F\u540E\u666E\u901A\u6D88\u606F\u4E5F\u4F1A\u8F6C\u6362\u4E3A\u8F6C\u53D1\u6D88\u606F\u683C\u5F0F",
47
+ rankingTopCount: "\u6392\u884C\u699C\u663E\u793A\u7684\u7528\u6237\u6570\u91CF"
47
48
  };
48
49
  }
49
50
  });
@@ -173,6 +174,21 @@ var require_zh_CN2 = __commonJS({
173
174
  msgNotFound: "\u{1F50D} \u672A\u627E\u5230\u8BE5 ID \u7684\u56DE\u58F0\u6D1E\u6D88\u606F",
174
175
  userBoundSuccess: "\u2705 \u5DF2\u6210\u529F\u5C06\u7528\u6237\u7ED1\u5B9A\u5230\u56DE\u58F0\u6D1E #{0}\uFF01"
175
176
  }
177
+ },
178
+ "cave.ranking": {
179
+ description: "\u67E5\u770B\u56DE\u58F0\u6D1E\u6392\u884C\u699C",
180
+ messages: {
181
+ invalidPeriod: "\u65E0\u6548\u7684\u65F6\u95F4\u6BB5\u53C2\u6570\u3002\u652F\u6301\u7684\u65F6\u95F4\u6BB5\uFF1A{0}",
182
+ rankingTitle: "\u{1F4CA} \u56DE\u58F0\u6D1E\u6392\u884C\u699C ({0})",
183
+ noData: "\u6682\u65E0\u6570\u636E",
184
+ rankFormat: "{0} {1}\uFF1A{2} \u4E2A\u56DE\u58F0\u6D1E",
185
+ period: {
186
+ day: "24 \u5C0F\u65F6",
187
+ week: "7 \u5929",
188
+ month: "1 \u4E2A\u6708",
189
+ all: "\u6240\u6709\u65F6\u95F4"
190
+ }
191
+ }
176
192
  }
177
193
  }
178
194
  };
@@ -680,6 +696,16 @@ function createTextMsg(content) {
680
696
  }
681
697
  };
682
698
  }
699
+ function createTextMsgNode(userId, nickname, content) {
700
+ return {
701
+ type: "node",
702
+ data: {
703
+ user_id: userId,
704
+ nickname,
705
+ content
706
+ }
707
+ };
708
+ }
683
709
 
684
710
  // src/core/formatter/msg-formatter.ts
685
711
  async function sendCaveMsg(ctx, session, caveMsg, cfg) {
@@ -868,6 +894,100 @@ async function bindUsersToCave(ctx, session, id, userIds) {
868
894
  return session.text(".userBoundSuccess", [id]);
869
895
  }
870
896
 
897
+ // src/core/command/ranking.ts
898
+ var SUPPORTED_PERIODS = ["day", "week", "month", "all"];
899
+ function getStartTime(period) {
900
+ const now = /* @__PURE__ */ new Date();
901
+ const startTime = /* @__PURE__ */ new Date();
902
+ switch (period) {
903
+ case "day":
904
+ startTime.setDate(now.getDate() - 1);
905
+ break;
906
+ case "week":
907
+ startTime.setDate(now.getDate() - 7);
908
+ break;
909
+ case "month":
910
+ startTime.setMonth(now.getMonth() - 1);
911
+ break;
912
+ case "all":
913
+ startTime.setTime(0);
914
+ break;
915
+ }
916
+ return startTime;
917
+ }
918
+ function countUserOccurrences(caves) {
919
+ const countMap = /* @__PURE__ */ new Map();
920
+ caves.forEach((cave) => {
921
+ cave.relatedUsers.forEach((userId) => {
922
+ countMap.set(userId, (countMap.get(userId) || 0) + 1);
923
+ });
924
+ countMap.set(cave.originUserId, (countMap.get(cave.originUserId) || 0) + 1);
925
+ });
926
+ return countMap;
927
+ }
928
+ async function generateRankingText(ctx, session, countMap, topCount) {
929
+ const sortedUsers = Array.from(countMap.entries()).sort(([, a], [, b]) => b - a).slice(0, topCount);
930
+ let text = "";
931
+ if (sortedUsers.length === 0) {
932
+ text += session.text(".noData");
933
+ } else {
934
+ for (let i = 0; i < sortedUsers.length; i++) {
935
+ const [userId, count] = sortedUsers[i];
936
+ const userName = await getUserName(ctx, session, userId);
937
+ const rank = i + 1;
938
+ let rankEmoji = "";
939
+ switch (rank) {
940
+ case 1:
941
+ rankEmoji = "\u{1F947}";
942
+ break;
943
+ case 2:
944
+ rankEmoji = "\u{1F948}";
945
+ break;
946
+ case 3:
947
+ rankEmoji = "\u{1F949}";
948
+ break;
949
+ default:
950
+ rankEmoji = `${rank}.`;
951
+ }
952
+ if (i === sortedUsers.length - 1) {
953
+ text += session.text(".rankFormat", [rankEmoji, userName, count]);
954
+ } else {
955
+ text += session.text(".rankFormat", [rankEmoji, userName, count]) + "\n";
956
+ }
957
+ }
958
+ }
959
+ return text;
960
+ }
961
+ async function getRanking(ctx, session, cfg, period = "all") {
962
+ if (!session.guildId) {
963
+ await session.send(session.text("echo-cave.general.privateChatReminder"));
964
+ return;
965
+ }
966
+ const normalizedPeriod = period.toLowerCase();
967
+ if (!SUPPORTED_PERIODS.includes(normalizedPeriod)) {
968
+ await session.send(session.text(".invalidPeriod", [SUPPORTED_PERIODS.join(", ")]));
969
+ return;
970
+ }
971
+ const { channelId } = session;
972
+ const startTime = getStartTime(normalizedPeriod);
973
+ const topCount = cfg.rankingTopCount || 10;
974
+ const caves = await ctx.database.get("echo_cave_v2", {
975
+ channelId,
976
+ createTime: {
977
+ $gte: startTime
978
+ }
979
+ });
980
+ const countMap = countUserOccurrences(caves);
981
+ const rankingText = await generateRankingText(ctx, session, countMap, topCount);
982
+ const botName = await getUserName(this.ctx, session, session.bot?.userId) || "Bot";
983
+ const periodText = session.text(`.period.${period}`);
984
+ let title = session.text(".rankingTitle", [periodText]);
985
+ await session.onebot.sendGroupForwardMsg(channelId, [
986
+ createTextMsgNode(session.bot?.userId || session.userId, botName, title),
987
+ createTextMsgNode(session.bot?.userId || session.userId, botName, rankingText)
988
+ ]);
989
+ }
990
+
871
991
  // src/core/command/search-cave.ts
872
992
  async function searchCave(ctx, session, userIds) {
873
993
  if (!session.guildId) {
@@ -917,7 +1037,8 @@ var Config = import_koishi2.Schema.object({
917
1037
  maxFileSize: import_koishi2.Schema.number().default(512),
918
1038
  maxRecordSize: import_koishi2.Schema.number().default(512),
919
1039
  useBase64ForMedia: import_koishi2.Schema.boolean().default(false),
920
- sendAllAsForwardMsg: import_koishi2.Schema.boolean().default(false)
1040
+ sendAllAsForwardMsg: import_koishi2.Schema.boolean().default(false),
1041
+ rankingTopCount: import_koishi2.Schema.number().default(10)
921
1042
  }).i18n({
922
1043
  "zh-CN": require_zh_CN()
923
1044
  });
@@ -993,6 +1114,9 @@ function apply(ctx, cfg) {
993
1114
  ctx.command("cave.bind <id:number> <...userIds>", { authority: 4 }).action(
994
1115
  async ({ session }, id, ...userIds) => await bindUsersToCave(ctx, session, id, userIds)
995
1116
  );
1117
+ ctx.command("cave.ranking [period:string]").action(
1118
+ async ({ session }, period) => await getRanking(ctx, session, cfg, period)
1119
+ );
996
1120
  }
997
1121
  // Annotate the CommonJS export names for ESM import in node:
998
1122
  0 && (module.exports = {
@@ -4,3 +4,11 @@ export declare function createTextMsg(content: string): {
4
4
  text: string;
5
5
  };
6
6
  };
7
+ export declare function createTextMsgNode(userId: string, nickname: string, content: string): {
8
+ type: string;
9
+ data: {
10
+ user_id: string;
11
+ nickname: string;
12
+ content: string;
13
+ };
14
+ };
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.20.2",
4
+ "version": "1.21.1",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",