Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions backend/src/plugins/Reminders/commands/RemindCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes.js";
import { registerUpcomingReminder } from "../../../data/loops/upcomingRemindersLoop.js";
import { humanizeDuration } from "../../../humanizeDuration.js";
import { convertDelayStringToMS, messageLink } from "../../../utils.js";
import { parseDiscordTimestampToMS } from "../../../utils/parseDiscordTimestampToMS.js";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin.js";
import { remindersCmd } from "../types.js";

Expand Down Expand Up @@ -34,14 +35,19 @@ export const RemindCmd = remindersCmd({
// Date and time in YYYY-MM-DD[T]HH:mm format
reminderTime = moment.tz(args.time, "YYYY-M-D[T]HH:mm", tz).second(0);
} else {
// "Delay string" i.e. e.g. "2h30m"
const ms = convertDelayStringToMS(args.time);
if (ms === null) {
void pluginData.state.common.sendErrorMessage(msg, "Invalid reminder time");
return;
}
const timestampMs = parseDiscordTimestampToMS(args.time);
if (timestampMs !== null) {
reminderTime = moment.utc(timestampMs);
} else {
// "Delay string" i.e. e.g. "2h30m"
const ms = convertDelayStringToMS(args.time);
if (ms === null) {
void pluginData.state.common.sendErrorMessage(msg, "Invalid reminder time");
return;
}

reminderTime = moment.utc().add(ms, "millisecond");
reminderTime = moment.utc().add(ms, "millisecond");
}
}

if (!reminderTime.isValid() || reminderTime.isBefore(now)) {
Expand Down
15 changes: 15 additions & 0 deletions backend/src/utils/parseDiscordTimestampToMS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from "ava";
import { parseDiscordTimestampToMS } from "./parseDiscordTimestampToMS.js";

test("parseDiscordTimestampToMS: accepts timestamp markup", (t) => {
t.is(parseDiscordTimestampToMS("<t:1767200400>"), 1_767_200_400_000);
t.is(parseDiscordTimestampToMS("<t:1767200400:F>"), 1_767_200_400_000);
t.is(parseDiscordTimestampToMS("<t:1767200400:s>"), 1_767_200_400_000);
t.is(parseDiscordTimestampToMS("<t:1767200400:S>"), 1_767_200_400_000);
});

test("parseDiscordTimestampToMS: rejects invalid timestamp markup", (t) => {
t.is(parseDiscordTimestampToMS("1767200400"), null);
t.is(parseDiscordTimestampToMS("<t:1767200400:invalid>"), null);
t.is(parseDiscordTimestampToMS("<t:-1767200400>"), null);
});
13 changes: 13 additions & 0 deletions backend/src/utils/parseDiscordTimestampToMS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function parseDiscordTimestampToMS(str: string): number | null {
const match = str.match(/^<t:(\d+)(?::[tTdDfFsSR])?>$/);
if (!match) {
return null;
}

const timestamp = Number(match[1]);
if (!Number.isSafeInteger(timestamp)) {
return null;
}

return timestamp * 1000;
}
Loading