Skip to content

Commit

Permalink
refactor(embed): allow hex strings in setColor() (#7593)
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Mar 6, 2022
1 parent 8907390 commit 79d6c04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/discord.js/src/structures/Embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

const { Embed: BuildersEmbed } = require('@discordjs/builders');
const Transformers = require('../util/Transformers');
const { Util } = require('../util/Util');

/**
* Represents an embed object
*/
class Embed extends BuildersEmbed {
constructor(data) {
super(Transformers.toSnakeCase(data));
}

/**
* Sets the color of this embed
* @param {ColorResolvable} color The color of the embed
* @returns {Embed}
*/
setColor(color) {
if (color === null) {
return super.setColor(null);
}
return super.setColor(Util.resolveColor(color));
}
}

module.exports = Embed;
3 changes: 2 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ export interface EmbedProviderData {

export class Embed extends BuildersEmbed {
public constructor(data?: EmbedData | APIEmbed);
public override setColor(color: ColorResolvable | null): this;
}

export interface MappedChannelCategoryTypes {
Expand Down Expand Up @@ -3779,7 +3780,7 @@ export type ColorResolvable =
| 'DarkButNotBlack'
| 'NotQuiteBlack'
| 'Random'
| readonly [number, number, number]
| readonly [red: number, green: number, blue: number]
| number
| HexColorString;

Expand Down
7 changes: 6 additions & 1 deletion packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ import {
CategoryChannelChildManager,
ActionRowData,
MessageActionRowComponentData,
Embed,
} from '.';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { Embed } from '@discordjs/builders';

// Test type transformation:
declare const serialize: <T>(value: T) => Serialized<T>;
Expand Down Expand Up @@ -1342,6 +1342,11 @@ new ButtonComponent({
style: ButtonStyle.Danger,
});

// @ts-expect-error
new Embed().setColor('abc');

new Embed().setColor('#ffffff');

expectNotAssignable<ActionRowData<MessageActionRowComponentData>>({
type: ComponentType.ActionRow,
components: [
Expand Down

0 comments on commit 79d6c04

Please sign in to comment.