mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
feat(MessageEmbed): add spliceField method (#2857)
* Add spliceField and refactor to prevent code dupe * String() was for a falsy check, fixed * requested: remove embed field count checks
This commit is contained in:
@@ -29,7 +29,6 @@ const Messages = {
|
|||||||
COLOR_RANGE: 'Color must be within the range 0 - 16777215 (0xFFFFFF).',
|
COLOR_RANGE: 'Color must be within the range 0 - 16777215 (0xFFFFFF).',
|
||||||
COLOR_CONVERT: 'Unable to convert color to a number.',
|
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_NAME: 'MessageEmbed field names may not be empty.',
|
||||||
EMBED_FIELD_VALUE: 'MessageEmbed field values may not be empty.',
|
EMBED_FIELD_VALUE: 'MessageEmbed field values may not be empty.',
|
||||||
|
|
||||||
|
|||||||
@@ -51,12 +51,16 @@ class MessageEmbed {
|
|||||||
this.timestamp = data.timestamp ? new Date(data.timestamp).getTime() : null;
|
this.timestamp = data.timestamp ? new Date(data.timestamp).getTime() : null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The fields of this embed
|
* @typedef {Object} EmbedField
|
||||||
* @type {Object[]}
|
|
||||||
* @property {string} name The name of this field
|
* @property {string} name The name of this field
|
||||||
* @property {string} value The value of this field
|
* @property {string} value The value of this field
|
||||||
* @property {boolean} inline If this field will be displayed inline
|
* @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) : [];
|
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
|
* @param {boolean} [inline=false] Set the field to display inline
|
||||||
* @returns {MessageEmbed}
|
* @returns {MessageEmbed}
|
||||||
*/
|
*/
|
||||||
addField(name, value, inline = false) {
|
addField(name, value, inline) {
|
||||||
if (this.fields.length >= 25) throw new RangeError('EMBED_FIELD_COUNT');
|
this.fields.push(this.constructor.checkField(name, value, inline));
|
||||||
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 });
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,10 +184,28 @@ class MessageEmbed {
|
|||||||
* @param {boolean} [inline=false] Set the field to display inline
|
* @param {boolean} [inline=false] Set the field to display inline
|
||||||
* @returns {MessageEmbed}
|
* @returns {MessageEmbed}
|
||||||
*/
|
*/
|
||||||
addBlankField(inline = false) {
|
addBlankField(inline) {
|
||||||
return this.addField('\u200B', '\u200B', 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
|
* 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.
|
* setting an embed image or author/footer icons. Multiple files can be attached.
|
||||||
@@ -328,6 +345,21 @@ class MessageEmbed {
|
|||||||
} : null,
|
} : 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;
|
module.exports = MessageEmbed;
|
||||||
|
|||||||
Reference in New Issue
Block a user