mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
feat: add missing v13 component methods (#7466)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com> Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
@@ -25,7 +25,14 @@ export const authorNamePredicate = fieldNamePredicate.nullable();
|
||||
|
||||
export const urlPredicate = z.string().url().nullish();
|
||||
|
||||
export const colorPredicate = z.number().gte(0).lte(0xffffff).nullable();
|
||||
export const RGBPredicate = z.number().int().gte(0).lte(255);
|
||||
export const colorPredicate = z
|
||||
.number()
|
||||
.int()
|
||||
.gte(0)
|
||||
.lte(0xffffff)
|
||||
.nullable()
|
||||
.or(z.tuple([RGBPredicate, RGBPredicate, RGBPredicate]));
|
||||
|
||||
export const descriptionPredicate = z.string().min(1).max(4096).nullable();
|
||||
|
||||
|
||||
@@ -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.)
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ import type {
|
||||
APIEmbedImage,
|
||||
APIEmbedVideo,
|
||||
} from 'discord-api-types/v9';
|
||||
import type { Equatable } from '../../util/equatable';
|
||||
import isEqual from 'fast-deep-equal';
|
||||
|
||||
export type RGBTuple = [red: number, green: number, blue: number];
|
||||
|
||||
export interface IconData {
|
||||
/**
|
||||
@@ -36,7 +40,7 @@ export interface EmbedImageData extends Omit<APIEmbedImage, 'proxy_url'> {
|
||||
/**
|
||||
* Represents a non-validated embed in a message (image/video preview, rich embed, etc.)
|
||||
*/
|
||||
export class UnsafeEmbed {
|
||||
export class UnsafeEmbed implements Equatable<APIEmbed | UnsafeEmbed> {
|
||||
protected data: APIEmbed;
|
||||
|
||||
public constructor(data: APIEmbed = {}) {
|
||||
@@ -164,6 +168,13 @@ export class UnsafeEmbed {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The hex color of the current color of the embed
|
||||
*/
|
||||
public get hexColor() {
|
||||
return typeof this.data.color === 'number' ? `#${this.data.color.toString(16).padStart(6, '0')}` : this.data.color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a field to the embed (max 25)
|
||||
*
|
||||
@@ -228,7 +239,12 @@ export class UnsafeEmbed {
|
||||
*
|
||||
* @param color The color of the embed
|
||||
*/
|
||||
public setColor(color: number | null): this {
|
||||
public setColor(color: number | RGBTuple | null): this {
|
||||
if (Array.isArray(color)) {
|
||||
const [red, green, blue] = color;
|
||||
this.data.color = (red << 16) + (green << 8) + blue;
|
||||
return this;
|
||||
}
|
||||
this.data.color = color ?? undefined;
|
||||
return this;
|
||||
}
|
||||
@@ -315,6 +331,13 @@ export class UnsafeEmbed {
|
||||
return { ...this.data };
|
||||
}
|
||||
|
||||
public equals(other: UnsafeEmbed | APIEmbed) {
|
||||
const { image: thisImage, thumbnail: thisThumbnail, ...thisData } = this.data;
|
||||
const data = other instanceof UnsafeEmbed ? other.data : other;
|
||||
const { image, thumbnail, ...otherData } = data;
|
||||
return isEqual(otherData, thisData) && image?.url === thisImage?.url && thumbnail?.url === thisThumbnail?.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes field input and resolves strings
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user