feat: bring embed builder field manipulation in line with underlying array functionality (#3761)

* feat: splice multiple fields

* remove MessageEmbed#spliceField
* add MessageEmbed#spliceFields
* to behave more like Array#splice
* and allow multiple fields to be replaced/inserted
* update typings accordingly

* refactor: rename check to normalize

* check suggests boolean return type

* feat: allow spread args or array as field input

* rewrite: replace addField in favor of addFields

* typings: account for changes

* chore: bump min node to 11.0.0

* for Array#flat

* fix: bump min-node in package engines field

* remove addBlankField
This commit is contained in:
Souji
2020-02-23 20:41:48 +01:00
committed by GitHub
parent ecd8cccddf
commit b727f6c1b9
6 changed files with 25 additions and 33 deletions

View File

@@ -38,7 +38,7 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to
- 100% coverage of the Discord API - 100% coverage of the Discord API
## Installation ## Installation
**Node.js 10.2.0 or newer is required.** **Node.js 11.0.0 or newer is required.**
Ignore any warnings about unmet peer dependencies, as they're all optional. Ignore any warnings about unmet peer dependencies, as they're all optional.
Without voice support: `npm install discordjs/discord.js` Without voice support: `npm install discordjs/discord.js`

View File

@@ -3,7 +3,7 @@ These questions are some of the most frequently asked.
## No matter what, I get `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`‽ ## No matter what, I get `SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode`‽
Update to Node.js 10.0.0 or newer. Update to Node.js 11.0.0 or newer.
## How do I get voice working? ## How do I get voice working?
- Install FFMPEG. - Install FFMPEG.

View File

@@ -33,7 +33,7 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to
- 100% coverage of the Discord API - 100% coverage of the Discord API
## Installation ## Installation
**Node.js 10.0.0 or newer is required.** **Node.js 11.0.0 or newer is required.**
Ignore any warnings about unmet peer dependencies, as they're all optional. Ignore any warnings about unmet peer dependencies, as they're all optional.
Without voice support: `npm install discordjs/discord.js` Without voice support: `npm install discordjs/discord.js`

View File

@@ -87,7 +87,7 @@
"webpack-cli": "^3.2.3" "webpack-cli": "^3.2.3"
}, },
"engines": { "engines": {
"node": ">=10.2.0" "node": ">=11.0.0"
}, },
"browser": { "browser": {
"@discordjs/opus": false, "@discordjs/opus": false,

View File

@@ -188,41 +188,24 @@ class MessageEmbed {
} }
/** /**
* Adds a field to the embed (max 25). * Adds a fields to the embed (max 25).
* @param {StringResolvable} name The name of the field * @param {...EmbedField|EmbedField[]} fields The fields to add
* @param {StringResolvable} value The value of the field
* @param {boolean} [inline=false] Set the field to display inline
* @returns {MessageEmbed} * @returns {MessageEmbed}
*/ */
addField(name, value, inline) { addFields(...fields) {
this.fields.push(this.constructor.checkField(name, value, inline)); this.fields.push(...this.constructor.normalizeFields(fields));
return this; return this;
} }
/**
* Convenience function for `<MessageEmbed>.addField('\u200B', '\u200B', inline)`.
* @param {boolean} [inline=false] Set the field to display inline
* @returns {MessageEmbed}
*/
addBlankField(inline) {
return this.addField('\u200B', '\u200B', inline);
}
/** /**
* Removes, replaces, and inserts fields in the embed (max 25). * Removes, replaces, and inserts fields in the embed (max 25).
* @param {number} index The index to start at * @param {number} index The index to start at
* @param {number} deleteCount The number of fields to remove * @param {number} deleteCount The number of fields to remove
* @param {StringResolvable} [name] The name of the field * @param {...EmbedField|EmbedField[]} [fields] The replacing field objects
* @param {StringResolvable} [value] The value of the field
* @param {boolean} [inline=false] Set the field to display inline
* @returns {MessageEmbed} * @returns {MessageEmbed}
*/ */
spliceField(index, deleteCount, name, value, inline) { spliceFields(index, deleteCount, ...fields) {
if (name && value) { this.fields.splice(index, deleteCount, ...this.constructor.normalizeFields(...fields));
this.fields.splice(index, deleteCount, this.constructor.checkField(name, value, inline));
} else {
this.fields.splice(index, deleteCount);
}
return this; return this;
} }
@@ -373,13 +356,22 @@ class MessageEmbed {
* @param {boolean} [inline=false] Set the field to display inline * @param {boolean} [inline=false] Set the field to display inline
* @returns {EmbedField} * @returns {EmbedField}
*/ */
static checkField(name, value, inline = false) { static normalizeField(name, value, inline = false) {
name = Util.resolveString(name); name = Util.resolveString(name);
if (!name) throw new RangeError('EMBED_FIELD_NAME'); if (!name) throw new RangeError('EMBED_FIELD_NAME');
value = Util.resolveString(value); value = Util.resolveString(value);
if (!value) throw new RangeError('EMBED_FIELD_VALUE'); if (!value) throw new RangeError('EMBED_FIELD_VALUE');
return { name, value, inline }; return { name, value, inline };
} }
/**
* Check for valid field input and resolves strings
* @param {...EmbedField|EmbedField[]} fields Fields to normalize
* @returns {EmbedField[]}
*/
static normalizeFields(...fields) {
return fields.flat(2).map(({ name, value, inline }) => this.normalizeField(name, value, inline));
}
} }
module.exports = MessageEmbed; module.exports = MessageEmbed;

8
typings/index.d.ts vendored
View File

@@ -1070,8 +1070,7 @@ declare module 'discord.js' {
public type: string; public type: string;
public url: string; public url: string;
public readonly video: { url?: string; proxyURL?: string; height?: number; width?: number } | null; public readonly video: { url?: string; proxyURL?: string; height?: number; width?: number } | null;
public addBlankField(inline?: boolean): this; public addFields(...fields: EmbedField[] | EmbedField[][]): this;
public addField(name: StringResolvable, value: StringResolvable, inline?: boolean): this;
public attachFiles(file: (MessageAttachment | FileOptions | string)[]): this; public attachFiles(file: (MessageAttachment | FileOptions | string)[]): this;
public setAuthor(name: StringResolvable, iconURL?: string, url?: string): this; public setAuthor(name: StringResolvable, iconURL?: string, url?: string): this;
public setColor(color: ColorResolvable): this; public setColor(color: ColorResolvable): this;
@@ -1082,10 +1081,11 @@ declare module 'discord.js' {
public setTimestamp(timestamp?: Date | number): this; public setTimestamp(timestamp?: Date | number): this;
public setTitle(title: StringResolvable): this; public setTitle(title: StringResolvable): this;
public setURL(url: string): this; public setURL(url: string): this;
public spliceField(index: number, deleteCount: number, name?: StringResolvable, value?: StringResolvable, inline?: boolean): this; public spliceFields(index: number, deleteCount: number, ...fields: EmbedField[] | EmbedField[][]): this;
public toJSON(): object; public toJSON(): object;
public static checkField(name: StringResolvable, value: StringResolvable, inline?: boolean): Required<EmbedField>; public static normalizeField(name: StringResolvable, value: StringResolvable, inline?: boolean): Required<EmbedField>;
public static normalizeFields(...fields: EmbedField[] | EmbedField[][]): Required<EmbedField>[];
} }
export class MessageMentions { export class MessageMentions {