Skip to content

Commit 0942567

Browse files
committed
feat: role gradient colors
1 parent e6d59ea commit 0942567

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

packages/discord.js/src/managers/RoleManager.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,23 @@ class RoleManager extends CachedManager {
109109
* @returns {?Snowflake}
110110
*/
111111

112+
/**
113+
* @typedef {Object} RoleColors
114+
* @property {number} primaryColor The primary color of the role
115+
* @property {?number} secondaryColor The secondary color of the role.
116+
* This will make the role a gradient between the other provided colors
117+
* @property {?number} tertiaryColor The tertiary color of the role.
118+
* When sending `tertiaryColor` the API enforces the role color to be a holographic style with values of `primaryColor = 11127295`, `secondaryColor = 16759788`, and `tertiaryColor = 16761760`
119+
*/
120+
112121
/**
113122
* Options used to create a new role.
114123
*
115124
* @typedef {Object} RoleCreateOptions
116125
* @property {string} [name] The name of the new role
117-
* @property {ColorResolvable} [color] The data to create the role with
126+
* @property {ColorResolvable} [color] The color to create the role with.
127+
* This will still be returned by the API, but using the `colors` field is recommended when doing requests
128+
* @property {RoleColors} [colors] The colors to create the role with
118129
* @property {boolean} [hoist] Whether or not the new role should be hoisted
119130
* @property {PermissionResolvable} [permissions] The permissions for the new role
120131
* @property {number} [position] The position of the new role
@@ -158,10 +169,20 @@ class RoleManager extends CachedManager {
158169
if (typeof icon !== 'string') icon = undefined;
159170
}
160171

172+
let colors;
173+
if (options.colors) {
174+
colors = {
175+
primary_color: resolveColor(options.colors.primaryColor),
176+
secondary_color: options.colors.secondaryColor && resolveColor(options.colors.secondaryColor),
177+
tertiary_color: options.colors.tertiaryColor && resolveColor(options.colors.tertiaryColor),
178+
};
179+
}
180+
161181
const data = await this.client.rest.post(Routes.guildRoles(this.guild.id), {
162182
body: {
163183
name,
164184
color,
185+
colors,
165186
hoist,
166187
permissions,
167188
mentionable,
@@ -212,9 +233,19 @@ class RoleManager extends CachedManager {
212233
if (typeof icon !== 'string') icon = undefined;
213234
}
214235

236+
let colors;
237+
if (options.colors) {
238+
colors = {
239+
primary_color: resolveColor(options.colors.primaryColor),
240+
secondary_color: options.colors.secondaryColor && resolveColor(options.colors.secondaryColor),
241+
tertiary_color: options.colors.tertiaryColor && resolveColor(options.colors.tertiaryColor),
242+
};
243+
}
244+
215245
const body = {
216246
name: options.name,
217247
color: options.color === undefined ? undefined : resolveColor(options.color),
248+
colors,
218249
hoist: options.hoist,
219250
permissions: options.permissions === undefined ? undefined : new PermissionsBitField(options.permissions),
220251
mentionable: options.mentionable,

packages/discord.js/src/structures/Role.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ class Role extends Base {
6666
this.color = data.color;
6767
}
6868

69+
if ('colors' in data) {
70+
/**
71+
* The colors of the role
72+
*
73+
* @type {RoleColors}
74+
*/
75+
this.colors = {
76+
primaryColor: data.colors.primary_color,
77+
secondaryColor: data.colors.secondary_color,
78+
tertiaryColor: data.colors.tertiary_color,
79+
};
80+
}
81+
6982
if ('hoist' in data) {
7083
/**
7184
* If true, users that are part of this role will appear in a separate category in the users list
@@ -257,7 +270,9 @@ class Role extends Base {
257270
*
258271
* @typedef {Object} RoleData
259272
* @property {string} [name] The name of the role
260-
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
273+
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number.
274+
* This will still be returned by the API, but using the `colors` field is recommended when doing requests
275+
* @property {RoleColors} [colors] The colors of the role
261276
* @property {boolean} [hoist] Whether or not the role should be hoisted
262277
* @property {number} [position] The position of the role
263278
* @property {PermissionResolvable} [permissions] The permissions of the role
@@ -330,6 +345,22 @@ class Role extends Base {
330345
return this.edit({ color, reason });
331346
}
332347

348+
/**
349+
* Sets new colors for the role.
350+
*
351+
* @param {RoleColors} colors The colors of the role
352+
* @param {string} [reason] Reason for changing the role's colors
353+
* @returns {Promise<Role>}
354+
* @example
355+
* // Set the colors of a role
356+
* role.setColors({ primaryColor: '#FF0000', secondaryColor: '#00FF00', tertiaryColor: '#0000FF' })
357+
* .then(updated => console.log(`Set colors of role to ${updated.colors}`))
358+
* .catch(console.error);
359+
*/
360+
async setColors(colors, reason) {
361+
return this.edit({ colors, reason });
362+
}
363+
333364
/**
334365
* Sets whether or not the role should be hoisted.
335366
*
@@ -476,6 +507,9 @@ class Role extends Base {
476507
this.id === role.id &&
477508
this.name === role.name &&
478509
this.color === role.color &&
510+
this.colors.primaryColor === role.colors.primaryColor &&
511+
this.colors.secondaryColor === role.colors.secondaryColor &&
512+
this.colors.tertiaryColor === role.colors.tertiaryColor &&
479513
this.hoist === role.hoist &&
480514
this.position === role.position &&
481515
this.permissions.bitfield === role.permissions.bitfield &&

packages/discord.js/typings/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,9 +2834,16 @@ export class RichPresenceAssets {
28342834
public smallImageURL(options?: ImageURLOptions): string | null;
28352835
}
28362836

2837+
export interface RoleColors {
2838+
primaryColor: number;
2839+
secondaryColor: number | null;
2840+
tertiaryColor: number | null;
2841+
}
2842+
28372843
export class Role extends Base {
28382844
private constructor(client: Client<true>, data: APIRole, guild: Guild);
28392845
public color: number;
2846+
public colors: RoleColors;
28402847
public get createdAt(): Date;
28412848
public get createdTimestamp(): number;
28422849
public get editable(): boolean;
@@ -2865,6 +2872,7 @@ export class Role extends Base {
28652872
checkAdmin?: boolean,
28662873
): Readonly<PermissionsBitField>;
28672874
public setColor(color: ColorResolvable, reason?: string): Promise<Role>;
2875+
public setColors(colors: RoleColors, reason?: string): Promise<Role>;
28682876
public setHoist(hoist?: boolean, reason?: string): Promise<Role>;
28692877
public setMentionable(mentionable?: boolean, reason?: string): Promise<Role>;
28702878
public setName(name: string, reason?: string): Promise<Role>;
@@ -6798,6 +6806,7 @@ export interface ResolvedOverwriteOptions {
67986806

67996807
export interface RoleData {
68006808
color?: ColorResolvable;
6809+
colors?: RoleColors;
68016810
hoist?: boolean;
68026811
icon?: Base64Resolvable | BufferResolvable | EmojiResolvable | null;
68036812
mentionable?: boolean;

0 commit comments

Comments
 (0)