Change RichEmbed to take StringResolvables (#1010)

Changed RichEmbed to take StringResolvables for all title, name, value and
description arguments.

Also changed a few lines in setColor to make sure that the value
assigned to this.color is always a number.
This commit is contained in:
Alexander
2016-12-26 21:39:07 +01:00
committed by Schuyler Cebulskie
parent 72c667b307
commit b1473b1e4c

View File

@@ -67,10 +67,11 @@ class RichEmbed {
/**
* Sets the title of this embed
* @param {string} title The title
* @param {StringResolvable} title The title
* @returns {RichEmbed} This embed
*/
setTitle(title) {
title = resolveString(title);
if (title.length > 256) throw new RangeError('RichEmbed titles may not exceed 256 characters.');
this.title = title;
return this;
@@ -78,10 +79,11 @@ class RichEmbed {
/**
* Sets the description of this embed
* @param {string} description The description
* @param {StringResolvable} description The description
* @returns {RichEmbed} This embed
*/
setDescription(description) {
description = resolveString(description);
if (description.length > 2048) throw new RangeError('RichEmbed descriptions may not exceed 2048 characters.');
this.description = description;
return this;
@@ -103,14 +105,17 @@ class RichEmbed {
* @returns {RichEmbed} This embed
*/
setColor(color) {
let radix = 10;
if (color instanceof Array) {
color = (color[0] << 16) + (color[1] << 8) + color[2];
} else if (typeof color === 'string' && color.startsWith('#')) {
color = parseInt(color.replace('#', ''), 16);
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(parseInt(color))) {
} else if (color && isNaN(color)) {
throw new TypeError('Unable to convert RichEmbed color to a number.');
}
this.color = color;
@@ -119,13 +124,13 @@ class RichEmbed {
/**
* Sets the author of this embed
* @param {string} name The name of the author
* @param {StringResolvable} name The name of the author
* @param {string} [icon] The icon URL of the author
* @param {string} [url] The URL of the author
* @returns {RichEmbed} This embed
*/
setAuthor(name, icon, url) {
this.author = { name, icon_url: icon, url };
this.author = { name: resolveString(name), icon_url: icon, url };
return this;
}
@@ -141,20 +146,18 @@ class RichEmbed {
/**
* Adds a field to the embed (max 25)
* @param {string} name The name of the field
* @param {StringResolvable} name The name of the field
* @param {StringResolvable} value The value of the field
* @param {boolean} [inline=false] Set the field to display inline
* @returns {RichEmbed} This embed
*/
addField(name, value, inline = false) {
if (this.fields.length >= 25) throw new RangeError('RichEmbeds may not exceed 25 fields.');
name = resolveString(name);
if (name.length > 256) throw new RangeError('RichEmbed field names may not exceed 256 characters.');
if (typeof value !== 'undefined') {
if (value instanceof Array) value = value.join('\n');
else if (typeof value !== 'string') value = String(value);
}
value = resolveString(value);
if (value.length > 1024) throw new RangeError('RichEmbed field values may not exceed 1024 characters.');
this.fields.push({ name, value, inline });
this.fields.push({ name: String(name), value: value, inline });
return this;
}
@@ -180,11 +183,12 @@ class RichEmbed {
/**
* Sets the footer of this embed
* @param {string} text The text of the footer
* @param {StringResolvable} text The text of the footer
* @param {string} [icon] The icon URL of the footer
* @returns {RichEmbed} This embed
*/
setFooter(text, icon) {
text = resolveString(text);
if (text.length > 2048) throw new RangeError('RichEmbed footer text may not exceed 2048 characters.');
this.footer = { text, icon_url: icon };
return this;
@@ -192,3 +196,9 @@ class RichEmbed {
}
module.exports = RichEmbed;
const resolveString = (data) => {
if (typeof data === 'string') return data;
if (data instanceof Array) return data.join('\n');
return String(data);
};