koishi-plugin-echo-cave 1.24.0 → 1.24.2
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 +23 -18
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -908,24 +908,22 @@ var PREDEFINED_PERIODS = [
|
|
|
908
908
|
"all"
|
|
909
909
|
];
|
|
910
910
|
var TIME_UNITS = {
|
|
911
|
-
m: 60 * 1e3,
|
|
912
|
-
//
|
|
913
|
-
h: 60 * 60 * 1e3,
|
|
914
|
-
// hours
|
|
915
|
-
d: 24 * 60 * 60 * 1e3,
|
|
916
|
-
// days
|
|
911
|
+
m: 30 * 24 * 60 * 60 * 1e3,
|
|
912
|
+
// 月(按约 30 天算)
|
|
917
913
|
w: 7 * 24 * 60 * 60 * 1e3,
|
|
918
|
-
//
|
|
919
|
-
|
|
920
|
-
//
|
|
914
|
+
// 周
|
|
915
|
+
d: 24 * 60 * 60 * 1e3,
|
|
916
|
+
// 天
|
|
917
|
+
h: 60 * 60 * 1e3
|
|
918
|
+
// 小时
|
|
921
919
|
};
|
|
922
920
|
function parseCustomTime(timeStr) {
|
|
923
|
-
const regex = /(\d+)([
|
|
921
|
+
const regex = /(\d+)([mwdh])/g;
|
|
924
922
|
let match;
|
|
925
923
|
let totalMs = 0;
|
|
926
924
|
let lastUnit = "";
|
|
927
|
-
const unitOrder = ["
|
|
928
|
-
if (
|
|
925
|
+
const unitOrder = ["m", "w", "d", "h"];
|
|
926
|
+
if (!/^(\d+[mwdh])+$/i.test(timeStr)) {
|
|
929
927
|
return null;
|
|
930
928
|
}
|
|
931
929
|
while ((match = regex.exec(timeStr)) !== null) {
|
|
@@ -933,6 +931,7 @@ function parseCustomTime(timeStr) {
|
|
|
933
931
|
const num = parseInt(value, 10);
|
|
934
932
|
const currentIndex = unitOrder.indexOf(unit);
|
|
935
933
|
const lastIndex = unitOrder.indexOf(lastUnit);
|
|
934
|
+
if (lastUnit && currentIndex < 0) return null;
|
|
936
935
|
if (lastUnit && currentIndex > lastIndex) {
|
|
937
936
|
return null;
|
|
938
937
|
}
|
|
@@ -941,10 +940,10 @@ function parseCustomTime(timeStr) {
|
|
|
941
940
|
}
|
|
942
941
|
return totalMs;
|
|
943
942
|
}
|
|
944
|
-
function getStartTime(period) {
|
|
943
|
+
function getStartTime(period, customTimeMs) {
|
|
945
944
|
const now = /* @__PURE__ */ new Date();
|
|
946
945
|
const startTime = /* @__PURE__ */ new Date();
|
|
947
|
-
const customTime = parseCustomTime(period);
|
|
946
|
+
const customTime = customTimeMs !== void 0 ? customTimeMs : parseCustomTime(period);
|
|
948
947
|
if (customTime !== null) {
|
|
949
948
|
startTime.setTime(now.getTime() - customTime);
|
|
950
949
|
return startTime;
|
|
@@ -1038,12 +1037,13 @@ async function getRanking(ctx, session, cfg, period = "all") {
|
|
|
1038
1037
|
return;
|
|
1039
1038
|
}
|
|
1040
1039
|
const normalizedPeriod = period.toLowerCase();
|
|
1041
|
-
|
|
1040
|
+
const customTimeMs = parseCustomTime(normalizedPeriod);
|
|
1041
|
+
if (!PREDEFINED_PERIODS.includes(normalizedPeriod) && customTimeMs === null) {
|
|
1042
1042
|
await session.send(session.text(".invalidPeriod", [PREDEFINED_PERIODS.join(", ")]));
|
|
1043
1043
|
return;
|
|
1044
1044
|
}
|
|
1045
1045
|
const { channelId } = session;
|
|
1046
|
-
const startTime = getStartTime(normalizedPeriod);
|
|
1046
|
+
const startTime = getStartTime(normalizedPeriod, customTimeMs);
|
|
1047
1047
|
const topCount = cfg.rankingTopCount || 10;
|
|
1048
1048
|
const caves = await ctx.database.get("echo_cave_v2", {
|
|
1049
1049
|
channelId,
|
|
@@ -1053,8 +1053,13 @@ async function getRanking(ctx, session, cfg, period = "all") {
|
|
|
1053
1053
|
});
|
|
1054
1054
|
const countMap = countUserOccurrences(caves);
|
|
1055
1055
|
const rankingText = await generateRankingText(ctx, session, countMap, topCount);
|
|
1056
|
-
const botName = await getUserName(
|
|
1057
|
-
|
|
1056
|
+
const botName = await getUserName(ctx, session, session.bot?.userId) || "Bot";
|
|
1057
|
+
let periodText;
|
|
1058
|
+
if (customTimeMs !== null) {
|
|
1059
|
+
periodText = normalizedPeriod;
|
|
1060
|
+
} else {
|
|
1061
|
+
periodText = session.text(`.period.${normalizedPeriod}`);
|
|
1062
|
+
}
|
|
1058
1063
|
let title = session.text(".rankingTitle", [periodText]);
|
|
1059
1064
|
await session.onebot.sendGroupForwardMsg(channelId, [
|
|
1060
1065
|
createTextMsgNode(session.bot?.userId || session.userId, botName, title),
|