koishi-plugin-echo-cave 1.22.2 → 1.24.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.
@@ -1,5 +1,5 @@
1
1
  import { Config } from '../../config/config';
2
2
  import { Context, Session } from 'koishi';
3
- export type Period = 'day' | 'week' | 'month' | 'all';
4
- export declare const SUPPORTED_PERIODS: Period[];
3
+ export type Period = 'lday' | 'lweek' | 'lmonth' | 'day' | 'week' | 'month' | 'all' | string;
4
+ export declare const PREDEFINED_PERIODS: Array<Period>;
5
5
  export declare function getRanking(ctx: Context, session: Session, cfg: Config, period?: string): Promise<void>;
package/lib/index.cjs CHANGED
@@ -178,14 +178,17 @@ var require_zh_CN2 = __commonJS({
178
178
  "cave.rank": {
179
179
  description: "\u67E5\u770B\u56DE\u58F0\u6D1E\u6392\u884C\u699C",
180
180
  messages: {
181
- invalidPeriod: "\u65E0\u6548\u7684\u65F6\u95F4\u6BB5\u53C2\u6570\u3002\u652F\u6301\u7684\u65F6\u95F4\u6BB5\uFF1A{0}",
181
+ invalidPeriod: "\u65E0\u6548\u7684\u65F6\u95F4\u6BB5\u53C2\u6570\u3002\u652F\u6301\u7684\u65F6\u95F4\u6BB5\uFF1A{0}\uFF0C\u6216\u81EA\u5B9A\u4E49\u65F6\u95F4\u683C\u5F0F(\u5982: 1d, 2d5h, 30m)",
182
182
  rankingTitle: "\u{1F4CA} \u56DE\u58F0\u6D1E\u6392\u884C\u699C ({0})",
183
183
  noData: "\u6682\u65E0\u6570\u636E",
184
184
  rankFormat: "{rankEmoji} {userName}\uFF1A{count} \u4E2A\u56DE\u58F0\u6D1E",
185
185
  period: {
186
- day: "24 \u5C0F\u65F6",
187
- week: "7 \u5929",
188
- month: "1 \u4E2A\u6708",
186
+ lday: "\u8FC7\u53BB 24 \u5C0F\u65F6",
187
+ lweek: "\u8FC7\u53BB 7 \u5929",
188
+ lmonth: "\u8FC7\u53BB 30 \u5929",
189
+ day: "\u4ECA\u5929",
190
+ week: "\u672C\u5468",
191
+ month: "\u672C\u6708",
189
192
  all: "\u6240\u6709\u65F6\u95F4"
190
193
  }
191
194
  }
@@ -895,23 +898,90 @@ async function bindUsersToCave(ctx, session, id, userIds) {
895
898
  }
896
899
 
897
900
  // src/core/command/rank.ts
898
- var SUPPORTED_PERIODS = ["day", "week", "month", "all"];
901
+ var PREDEFINED_PERIODS = [
902
+ "lday",
903
+ "lweek",
904
+ "lmonth",
905
+ "day",
906
+ "week",
907
+ "month",
908
+ "all"
909
+ ];
910
+ var TIME_UNITS = {
911
+ m: 60 * 1e3,
912
+ // minutes
913
+ h: 60 * 60 * 1e3,
914
+ // hours
915
+ d: 24 * 60 * 60 * 1e3,
916
+ // days
917
+ w: 7 * 24 * 60 * 60 * 1e3,
918
+ // weeks
919
+ M: 30 * 24 * 60 * 60 * 1e3
920
+ // months (approximate)
921
+ };
922
+ function parseCustomTime(timeStr) {
923
+ const regex = /(\d+)([mhdwM])/g;
924
+ let match;
925
+ let totalMs = 0;
926
+ let lastUnit = "";
927
+ const unitOrder = ["M", "w", "d", "h", "m"];
928
+ if (!/^\d+([mhdwM]\d*)*$/.test(timeStr)) {
929
+ return null;
930
+ }
931
+ while ((match = regex.exec(timeStr)) !== null) {
932
+ const [, value, unit] = match;
933
+ const num = parseInt(value, 10);
934
+ const currentIndex = unitOrder.indexOf(unit);
935
+ const lastIndex = unitOrder.indexOf(lastUnit);
936
+ if (lastUnit && currentIndex > lastIndex) {
937
+ return null;
938
+ }
939
+ totalMs += num * TIME_UNITS[unit];
940
+ lastUnit = unit;
941
+ }
942
+ return totalMs;
943
+ }
899
944
  function getStartTime(period) {
900
945
  const now = /* @__PURE__ */ new Date();
901
946
  const startTime = /* @__PURE__ */ new Date();
947
+ const customTime = parseCustomTime(period);
948
+ if (customTime !== null) {
949
+ startTime.setTime(now.getTime() - customTime);
950
+ return startTime;
951
+ }
902
952
  switch (period) {
903
- case "day":
953
+ // Last day/week/month (previous 24h, 7d, 30d)
954
+ case "lday":
904
955
  startTime.setDate(now.getDate() - 1);
905
956
  break;
906
- case "week":
957
+ case "lweek":
907
958
  startTime.setDate(now.getDate() - 7);
908
959
  break;
960
+ case "lmonth":
961
+ startTime.setDate(now.getDate() - 30);
962
+ break;
963
+ // This day/week/month (from start of period to now)
964
+ case "day":
965
+ startTime.setHours(0, 0, 0, 0);
966
+ break;
967
+ case "week":
968
+ const dayOfWeek = now.getDay();
969
+ const daysToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1;
970
+ startTime.setDate(now.getDate() - daysToMonday);
971
+ startTime.setHours(0, 0, 0, 0);
972
+ break;
909
973
  case "month":
910
- startTime.setMonth(now.getMonth() - 1);
974
+ startTime.setDate(1);
975
+ startTime.setHours(0, 0, 0, 0);
911
976
  break;
977
+ // All time
912
978
  case "all":
913
979
  startTime.setTime(0);
914
980
  break;
981
+ // Default case (should not happen due to validation)
982
+ default:
983
+ startTime.setTime(0);
984
+ break;
915
985
  }
916
986
  return startTime;
917
987
  }
@@ -968,8 +1038,8 @@ async function getRanking(ctx, session, cfg, period = "all") {
968
1038
  return;
969
1039
  }
970
1040
  const normalizedPeriod = period.toLowerCase();
971
- if (!SUPPORTED_PERIODS.includes(normalizedPeriod)) {
972
- await session.send(session.text(".invalidPeriod", [SUPPORTED_PERIODS.join(", ")]));
1041
+ if (!PREDEFINED_PERIODS.includes(normalizedPeriod) && parseCustomTime(normalizedPeriod) === null) {
1042
+ await session.send(session.text(".invalidPeriod", [PREDEFINED_PERIODS.join(", ")]));
973
1043
  return;
974
1044
  }
975
1045
  const { channelId } = session;
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.22.2",
4
+ "version": "1.24.0",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",