diff --git a/src/errors/Messages.js b/src/errors/Messages.js index 80fa7e389..2d1bc3c2c 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -29,7 +29,6 @@ const Messages = { COLOR_RANGE: 'Color must be within the range 0 - 16777215 (0xFFFFFF).', COLOR_CONVERT: 'Unable to convert color to a number.', - EMBED_FIELD_COUNT: 'MessageEmbeds may not exceed 25 fields.', EMBED_FIELD_NAME: 'MessageEmbed field names may not be empty.', EMBED_FIELD_VALUE: 'MessageEmbed field values may not be empty.', diff --git a/src/structures/MessageEmbed.js b/src/structures/MessageEmbed.js index 5d2b2066f..49ddf30a7 100644 --- a/src/structures/MessageEmbed.js +++ b/src/structures/MessageEmbed.js @@ -51,12 +51,16 @@ class MessageEmbed { this.timestamp = data.timestamp ? new Date(data.timestamp).getTime() : null; /** - * The fields of this embed - * @type {Object[]} + * @typedef {Object} EmbedField * @property {string} name The name of this field * @property {string} value The value of this field * @property {boolean} inline If this field will be displayed inline */ + + /** + * The fields of this embed + * @type {EmbedField[]} + */ this.fields = data.fields ? data.fields.map(Util.cloneObject) : []; /** @@ -170,13 +174,8 @@ class MessageEmbed { * @param {boolean} [inline=false] Set the field to display inline * @returns {MessageEmbed} */ - addField(name, value, inline = false) { - if (this.fields.length >= 25) throw new RangeError('EMBED_FIELD_COUNT'); - name = Util.resolveString(name); - if (!String(name)) throw new RangeError('EMBED_FIELD_NAME'); - value = Util.resolveString(value); - if (!String(value)) throw new RangeError('EMBED_FIELD_VALUE'); - this.fields.push({ name, value, inline }); + addField(name, value, inline) { + this.fields.push(this.constructor.checkField(name, value, inline)); return this; } @@ -185,10 +184,28 @@ class MessageEmbed { * @param {boolean} [inline=false] Set the field to display inline * @returns {MessageEmbed} */ - addBlankField(inline = false) { + addBlankField(inline) { return this.addField('\u200B', '\u200B', inline); } + /** + * Removes, replaces, and inserts fields in the embed (max 25). + * @param {number} index The index to start at + * @param {number} deleteCount The number of fields to remove + * @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 {MessageEmbed} + */ + spliceField(index, deleteCount, name, value, inline) { + if (name && value) { + this.fields.splice(index, deleteCount, this.constructor.checkField(name, value, inline)); + } else { + this.fields.splice(index, deleteCount); + } + return this; + } + /** * Sets the file to upload alongside the embed. This file can be accessed via `attachment://fileName.extension` when * setting an embed image or author/footer icons. Multiple files can be attached. @@ -328,6 +345,21 @@ class MessageEmbed { } : null, }; } + + /** + * Checks for valid field input and resolves strings + * @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 {EmbedField} + */ + static checkField(name, value, inline = false) { + name = Util.resolveString(name); + if (!name) throw new RangeError('EMBED_FIELD_NAME'); + value = Util.resolveString(value); + if (!value) throw new RangeError('EMBED_FIELD_VALUE'); + return { name, value, inline }; + } } module.exports = MessageEmbed;