feat(utils): add EmbedsBuilder.addFields() (#4077)

* feat(embed-builder): add addFields function

* feat(embed-builder): add tests for addField and addFields function

* Update packages/utils/src/builders/embeds.ts

Co-authored-by: Fleny <Fleny113@outlook.com>

* feat(embed-builder): add undefined check to addFields

* fix: make inline field optional in push

* revert: EmbedBuilder tsdoc rename

* Update packages/utils/src/builders/embeds.ts

Co-authored-by: Fleny <Fleny113@outlook.com>

* fix(embed-builder): expect inline to be undefined

* revert: outdated changes

* fix(embed-builder): fix embed unit test

* feat(embed-builder): add stickz jsdoc suggestions

---------

Co-authored-by: Fleny <Fleny113@outlook.com>
This commit is contained in:
Dominik Koch
2024-12-30 16:06:51 -08:00
committed by GitHub
co-authored by Fleny
parent a03ae35790
commit 21e81c087b
2 changed files with 42 additions and 1 deletions
+17 -1
View File
@@ -22,7 +22,7 @@ export class EmbedsBuilder extends Array<DiscordEmbed> {
#currentEmbedIndex: number = 0
/**
* Adds a new field to the embed fields array.
* Adds a new field to the current embed.
*
* @param {string} name - Field name
* @param {string} value - Field value
@@ -43,6 +43,22 @@ export class EmbedsBuilder extends Array<DiscordEmbed> {
return this
}
/**
* Adds multiple new fields to the current embed.
*
* @param {DiscordEmbedField[]} fields - The fields to add
* @returns {EmbedsBuilder}
*/
addFields(fields: DiscordEmbedField[]): this {
if (this.#currentEmbed.fields === undefined) {
this.#currentEmbed.fields = []
}
this.#currentEmbed.fields.push(...fields)
return this
}
/**
* Creates a blank embed.
*
+25
View File
@@ -22,6 +22,14 @@ describe('builders/embeds.ts', () => {
expect(new EmbedsBuilder().setDescription('My Description')).to.eql([{ description: 'My Description' }])
})
it('should add a field in the embed JSON', () => {
expect(new EmbedsBuilder().addField('firstname', 'firstvalue')).to.eql([
{
fields: [{ name: 'firstname', value: 'firstvalue', inline: undefined }],
},
])
})
it('should set the fields in the embed JSON', () => {
expect(
new EmbedsBuilder().setFields([
@@ -38,6 +46,23 @@ describe('builders/embeds.ts', () => {
])
})
it('should add the fields in the embed JSON', () => {
expect(
new EmbedsBuilder().addField('firstname', 'firstvalue').addFields([
{ name: 'secondname', value: 'secondvalue' },
{ name: 'thirdname', value: 'thirdvalue', inline: true },
]),
).to.eql([
{
fields: [
{ name: 'firstname', value: 'firstvalue', inline: undefined },
{ name: 'secondname', value: 'secondvalue' },
{ name: 'thirdname', value: 'thirdvalue', inline: true },
],
},
])
})
it('should set the footer text in the embed JSON', () => {
expect(new EmbedsBuilder().setFooter('footer text')).to.eql([{ footer: { text: 'footer text' } }])
})