fix(discord): guard timeout expiry dates

This commit is contained in:
Peter Steinberger
2026-05-30 08:29:10 -04:00
parent dd5b70bcc4
commit 3c41e1722f
2 changed files with 18 additions and 1 deletions

View File

@@ -121,6 +121,7 @@ beforeEach(() => {
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
afterAll(() => {
@@ -322,6 +323,18 @@ describe("sendMessageDiscord", () => {
).toBeTypeOf("string");
});
it("rejects timeout durations outside Date range", async () => {
const { rest, patchMock } = makeDiscordRest();
await expect(
timeoutMemberDiscord(
{ guildId: "g1", userId: "u1", durationMinutes: 8_640_000_000_000_001 },
discordClientOpts(rest),
),
).rejects.toThrow("Discord timeout duration is outside the supported Date range");
expect(patchMock).not.toHaveBeenCalled();
});
it("adds and removes roles", async () => {
const { rest, putMock, deleteMock } = makeDiscordRest();
putMock.mockResolvedValue({});

View File

@@ -6,6 +6,7 @@ import type {
APIVoiceState,
RESTPostAPIGuildScheduledEventJSONBody,
} from "discord-api-types/v10";
import { timestampMsToIsoString } from "openclaw/plugin-sdk/number-runtime";
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/string-coerce-runtime";
import { loadWebMediaRaw } from "openclaw/plugin-sdk/web-media";
import {
@@ -139,7 +140,10 @@ export async function timeoutMemberDiscord(
let until = payload.until;
if (!until && payload.durationMinutes) {
const ms = payload.durationMinutes * 60 * 1000;
until = new Date(Date.now() + ms).toISOString();
until = timestampMsToIsoString(Date.now() + ms);
if (!until) {
throw new Error("Discord timeout duration is outside the supported Date range");
}
}
return await timeoutGuildMember(rest, payload.guildId, payload.userId, {
body: { communication_disabled_until: until ?? null },