fix(discord): omit invalid component accent colors (#105387)

* fix(discord): omit invalid component accent colors

* test(discord): cover accent color boundaries

* docs: keep release notes in PR context

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
qingminlong
2026-07-13 09:25:49 +08:00
committed by GitHub
parent a63aec8bf8
commit 60ca7565a7
2 changed files with 27 additions and 1 deletions

View File

@@ -115,6 +115,32 @@ describe("discord components", () => {
expect(result.modals[0]?.allowedUsers).toEqual(["discord:user-1"]);
});
it.each([
{ label: "minimum", accentColor: 0, expected: 0 },
{ label: "maximum", accentColor: 0xffffff, expected: 0xffffff },
{ label: "negative", accentColor: -1, expected: undefined },
{ label: "fractional", accentColor: 1.5, expected: undefined },
{ label: "above maximum", accentColor: 0x1000000, expected: undefined },
])("serializes $label numeric container accent colors safely", ({ accentColor, expected }) => {
const spec = readDiscordComponentSpec({
text: "Status",
container: { accentColor },
});
if (!spec) {
throw new Error("Expected component spec to be parsed");
}
const serialized = buildDiscordComponentMessage({ spec }).components[0]?.serialize() as
| { accent_color?: unknown }
| undefined;
if (expected === undefined) {
expect(serialized).not.toHaveProperty("accent_color");
} else {
expect(serialized?.accent_color).toBe(expected);
}
});
it("serializes disabled link buttons", () => {
const spec = readDiscordComponentSpec({
blocks: [

View File

@@ -41,7 +41,7 @@ export function clean<T extends Record<string, unknown>>(value: T): T {
export function colorToNumber(value: string | number | undefined): number | undefined {
if (typeof value === "number") {
return value;
return Number.isInteger(value) && value >= 0 && value <= 0xffffff ? value : undefined;
}
if (typeof value === "string" && /^#?[0-9a-f]{6}$/i.test(value)) {
return Number.parseInt(value.replace(/^#/, ""), 16);