Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing v13 component methods #7466

Merged
merged 10 commits into from
Feb 18, 2022
12 changes: 8 additions & 4 deletions packages/builders/__tests__/messages/embed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ describe('Embed', () => {
});

test('GIVEN an embed using Embed#setColor THEN returns valid toJSON data', () => {
const embed = new Embed();
embed.setColor(0xff0000);

expect(embed.toJSON()).toStrictEqual({ color: 0xff0000 });
expect(new Embed().setColor(0xff0000).toJSON()).toStrictEqual({ color: 0xff0000 });
expect(new Embed().setColor([242, 66, 245]).toJSON()).toStrictEqual({ color: 0xf242f5 });
});

test('GIVEN an embed with a pre-defined color THEN unset color THEN return valid toJSON data', () => {
Expand All @@ -134,6 +132,12 @@ describe('Embed', () => {
// @ts-expect-error
expect(() => embed.setColor('RED')).toThrowError();
});

test('GIVEN an embed with a valid color THEN does not throw error', () => {
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
const embed = new Embed();
expect(() => embed.setColor([42, 36, 100])).not.toThrowError();
expect(() => embed.setColor(0xffffff)).not.toThrowError();
});
});

describe('Embed Timestamp', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/builders/src/messages/embed/Assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export const authorNamePredicate = fieldNamePredicate.nullable();

export const urlPredicate = z.string().url().nullish();

export const colorPredicate = z.number().gte(0).lte(0xffffff).nullable();
export const colorPredicate = z
.number()
.gte(0)
.lte(0xffffff)
.nullable()
.or(z.tuple([z.number(), z.number(), z.number()]));
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved

export const descriptionPredicate = z.string().min(1).max(4096).nullable();

Expand Down
4 changes: 2 additions & 2 deletions packages/builders/src/messages/embed/Embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
urlPredicate,
validateFieldLength,
} from './Assertions';
import { EmbedAuthorOptions, EmbedFooterOptions, UnsafeEmbed } from './UnsafeEmbed';
import { EmbedAuthorOptions, EmbedFooterOptions, RGBTuple, UnsafeEmbed } from './UnsafeEmbed';

/**
* Represents a validated embed in a message (image/video preview, rich embed, etc.)
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Embed extends UnsafeEmbed {
return super.setAuthor(options);
}

public override setColor(color: number | null): this {
public override setColor(color: number | RGBTuple | null): this {
// Data assertions
return super.setColor(colorPredicate.parse(color));
}
Expand Down