add color resolvable, and color constants from the client (#1080)

* add color resolvable, and color constants from the client

* fix up docs

* Update ClientDataResolver.js

* add easter eggs

* Update ClientDataResolver.js

* Update RESTMethods.js
This commit is contained in:
Gus Caplan
2017-01-12 12:43:22 -06:00
committed by Amish Shah
parent a3091f5262
commit 0d4eab8d24
5 changed files with 122 additions and 47 deletions

View File

@@ -163,33 +163,33 @@ class ClientDataResolver {
* Possible strings: * Possible strings:
* ```js * ```js
* [ * [
* "CREATE_INSTANT_INVITE", * 'CREATE_INSTANT_INVITE',
* "KICK_MEMBERS", * 'KICK_MEMBERS',
* "BAN_MEMBERS", * 'BAN_MEMBERS',
* "ADMINISTRATOR", * 'ADMINISTRATOR',
* "MANAGE_CHANNELS", * 'MANAGE_CHANNELS',
* "MANAGE_GUILD", * 'MANAGE_GUILD',
* "ADD_REACTIONS", // add reactions to messages * 'ADD_REACTIONS', // add reactions to messages
* "READ_MESSAGES", * 'READ_MESSAGES',
* "SEND_MESSAGES", * 'SEND_MESSAGES',
* "SEND_TTS_MESSAGES", * 'SEND_TTS_MESSAGES',
* "MANAGE_MESSAGES", * 'MANAGE_MESSAGES',
* "EMBED_LINKS", * 'EMBED_LINKS',
* "ATTACH_FILES", * 'ATTACH_FILES',
* "READ_MESSAGE_HISTORY", * 'READ_MESSAGE_HISTORY',
* "MENTION_EVERYONE", * 'MENTION_EVERYONE',
* "EXTERNAL_EMOJIS", // use external emojis * 'EXTERNAL_EMOJIS', // use external emojis
* "CONNECT", // connect to voice * 'CONNECT', // connect to voice
* "SPEAK", // speak on voice * 'SPEAK', // speak on voice
* "MUTE_MEMBERS", // globally mute members on voice * 'MUTE_MEMBERS', // globally mute members on voice
* "DEAFEN_MEMBERS", // globally deafen members on voice * 'DEAFEN_MEMBERS', // globally deafen members on voice
* "MOVE_MEMBERS", // move member's voice channels * 'MOVE_MEMBERS', // move member's voice channels
* "USE_VAD", // use voice activity detection * 'USE_VAD', // use voice activity detection
* "CHANGE_NICKNAME", * 'CHANGE_NICKNAME',
* "MANAGE_NICKNAMES", // change nicknames of others * 'MANAGE_NICKNAMES', // change nicknames of others
* "MANAGE_ROLES_OR_PERMISSIONS", * 'MANAGE_ROLES_OR_PERMISSIONS',
* "MANAGE_WEBHOOKS", * 'MANAGE_WEBHOOKS',
* "MANAGE_EMOJIS" * 'MANAGE_EMOJIS'
* ] * ]
* ``` * ```
* @typedef {string|number} PermissionResolvable * @typedef {string|number} PermissionResolvable
@@ -317,6 +317,67 @@ class ClientDataResolver {
} }
return null; return null;
} }
/**
* Can be a Hex Literal, Hex String, Number, RGB Array, or one of the following
* ```
* [
* 'DEFAULT',
* 'AQUA',
* 'GREEN',
* 'BLUE',
* 'PURPLE',
* 'GOLD',
* 'ORANGE',
* 'RED',
* 'GREY',
* 'DARKER_GREY',
* 'NAVY',
* 'DARK_AQUA',
* 'DARK_GREEN',
* 'DARK_BLUE',
* 'DARK_PURPLE',
* 'DARK_GOLD',
* 'DARK_ORANGE',
* 'DARK_RED',
* 'DARK_GREY',
* 'LIGHT_GREY',
* 'DARK_NAVY',
* ]
* ```
* or something like
* ```
* [255, 0, 255]
* ```
* for purple
* @typedef {String|number|Array} ColorResolvable
*/
/**
* @param {ColorResolvable} color Color to resolve
* @returns {number} A color
*/
static resolveColor(color) {
if (typeof color === 'string') {
color = Constants.Colors[color] || parseInt(color.replace('#', ''), 16);
} else if (color instanceof Array) {
color = (color[0] << 16) + (color[1] << 8) + color[2];
}
if (color < 0 || color > 0xFFFFFF) {
throw new RangeError('Color must be within the range 0 - 16777215 (0xFFFFFF).');
} else if (color && isNaN(color)) {
throw new TypeError('Unable to convert color to a number.');
}
return color;
}
/**
* @param {ColorResolvable} color Color to resolve
* @returns {number} A color
*/
resolveColor(color) {
return ClientDataResolver.resolveColor(color);
}
} }
module.exports = ClientDataResolver; module.exports = ClientDataResolver;

View File

@@ -459,10 +459,7 @@ class RESTMethods {
const data = {}; const data = {};
data.name = _data.name || role.name; data.name = _data.name || role.name;
data.position = typeof _data.position !== 'undefined' ? _data.position : role.position; data.position = typeof _data.position !== 'undefined' ? _data.position : role.position;
data.color = _data.color || role.color; data.color = this.client.resolver.resolveColor(_data.color || role.color);
if (typeof data.color === 'string' && data.color.startsWith('#')) {
data.color = parseInt(data.color.replace('#', ''), 16);
}
data.hoist = typeof _data.hoist !== 'undefined' ? _data.hoist : role.hoist; data.hoist = typeof _data.hoist !== 'undefined' ? _data.hoist : role.hoist;
data.mentionable = typeof _data.mentionable !== 'undefined' ? _data.mentionable : role.mentionable; data.mentionable = typeof _data.mentionable !== 'undefined' ? _data.mentionable : role.mentionable;

View File

@@ -1,3 +1,5 @@
const ClientDataResolver = require('../client/ClientDataResolver');
/** /**
* A rich embed to be sent with a message with a fluent interface for creation * A rich embed to be sent with a message with a fluent interface for creation
* @param {Object} [data] Data to set in the rich embed * @param {Object} [data] Data to set in the rich embed
@@ -101,24 +103,11 @@ class RichEmbed {
/** /**
* Sets the color of this embed * Sets the color of this embed
* @param {string|number|number[]} color The color to set * @param {ColorResolvable} color The color to set
* @returns {RichEmbed} This embed * @returns {RichEmbed} This embed
*/ */
setColor(color) { setColor(color) {
let radix = 10; this.color = ClientDataResolver.resolveColor(color);
if (color instanceof Array) {
color = (color[0] << 16) + (color[1] << 8) + color[2];
} else if (typeof color === 'string' && color.startsWith('#')) {
radix = 16;
color = color.replace('#', '');
}
color = parseInt(color, radix);
if (color < 0 || color > 0xFFFFFF) {
throw new RangeError('RichEmbed color must be within the range 0 - 16777215 (0xFFFFFF).');
} else if (color && isNaN(color)) {
throw new TypeError('Unable to convert RichEmbed color to a number.');
}
this.color = color;
return this; return this;
} }

View File

@@ -180,7 +180,7 @@ class Role {
* The data for a role * The data for a role
* @typedef {Object} RoleData * @typedef {Object} RoleData
* @property {string} [name] The name of the role * @property {string} [name] The name of the role
* @property {number|string} [color] The color of the role, either a hex string or a base 10 number * @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
* @property {boolean} [hoist] Whether or not the role should be hoisted * @property {boolean} [hoist] Whether or not the role should be hoisted
* @property {number} [position] The position of the role * @property {number} [position] The position of the role
* @property {string[]} [permissions] The permissions of the role * @property {string[]} [permissions] The permissions of the role

View File

@@ -368,6 +368,34 @@ const PermissionFlags = exports.PermissionFlags = {
MANAGE_EMOJIS: 1 << 30, MANAGE_EMOJIS: 1 << 30,
}; };
exports.Colors = {
DEFAULT: 0x000000,
AQUA: 0x1ABC9C,
GREEN: 0x2ECC71,
BLUE: 0x3498DB,
PURPLE: 0x9B59B6,
GOLD: 0xF1C40F,
ORANGE: 0xE67E22,
RED: 0xE74C3C,
GREY: 0x95A5A6,
NAVY: 0x34495E,
DARK_AQUA: 0x11806A,
DARK_GREEN: 0x1F8B4C,
DARK_BLUE: 0x206694,
DARK_PURPLE: 0x71368A,
DARK_GOLD: 0xC27C0E,
DARK_ORANGE: 0xA84300,
DARK_RED: 0x992D22,
DARK_GREY: 0x979C9F,
DARKER_GREY: 0x7F8C8D,
LIGHT_GREY: 0xBCC0C0,
DARK_NAVY: 0x2C3E50,
BLURPLE: 0x7289DA,
GREYPLE: 0x99AAB5,
DARK_BUT_NOT_BLACK: 0x2C2F33,
NOT_QUITE_BLACK: 0x23272A,
};
let _ALL_PERMISSIONS = 0; let _ALL_PERMISSIONS = 0;
for (const key in PermissionFlags) _ALL_PERMISSIONS |= PermissionFlags[key]; for (const key in PermissionFlags) _ALL_PERMISSIONS |= PermissionFlags[key];
exports.ALL_PERMISSIONS = _ALL_PERMISSIONS; exports.ALL_PERMISSIONS = _ALL_PERMISSIONS;