Skip to content

Commit

Permalink
refactor(MessageEmbed): Deprecate strings for setAuthor() (complete…
Browse files Browse the repository at this point in the history
…ly) and `setFooter()` (#7153)
  • Loading branch information
Jiralite committed Dec 29, 2021
1 parent baacd6b commit 3496516
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
47 changes: 36 additions & 11 deletions src/structures/MessageEmbed.js
Expand Up @@ -5,6 +5,9 @@ const { RangeError } = require('../errors');
const Util = require('../util/Util');

let deprecationEmittedForSetAuthor = false;
let deprecationEmittedForSetFooter = false;

// TODO: Remove the deprecated code for `setAuthor()` and `setFooter()`.

/**
* Represents an embed in a message (image/video preview, rich embed, etc.)
Expand Down Expand Up @@ -356,11 +359,9 @@ class MessageEmbed {
* @property {string} [iconURL] The icon URL of this author.
*/

// TODO: Remove the deprecated code in the following method and typings.
/**
* Sets the author of this embed.
* @param {string|EmbedAuthorData|null} options The options to provide for the author.
* A string may simply be provided if only the author name is desirable.
* Provide `null` to remove the author data.
* @param {string} [deprecatedIconURL] The icon URL of this author.
* <warn>This parameter is **deprecated**. Use the `options` parameter instead.</warn>
Expand All @@ -375,13 +376,9 @@ class MessageEmbed {
}

if (typeof options === 'string') {
if (
!deprecationEmittedForSetAuthor &&
(typeof deprecatedIconURL !== 'undefined' || typeof deprecatedURL !== 'undefined')
) {
if (!deprecationEmittedForSetAuthor) {
process.emitWarning(
// eslint-disable-next-line max-len
"Passing strings for the URL or the icon's URL for MessageEmbed#setAuthor is deprecated. Pass a sole object instead.",
'Passing strings for MessageEmbed#setAuthor is deprecated. Pass a sole object instead.',
'DeprecationWarning',
);

Expand Down Expand Up @@ -416,13 +413,41 @@ class MessageEmbed {
return this;
}

/**
* The options to provide for setting a footer for a {@link MessageEmbed}.
* @typedef {Object} EmbedFooterData
* @property {string} text The text of the footer.
* @property {string} [iconURL] The icon URL of the footer.
*/

/**
* Sets the footer of this embed.
* @param {string} text The text of the footer
* @param {string} [iconURL] The icon URL of the footer
* @param {string|EmbedFooterData|null} options The options to provide for the footer.
* Provide `null` to remove the footer data.
* @param {string} [deprecatedIconURL] The icon URL of this footer.
* <warn>This parameter is **deprecated**. Use the `options` parameter instead.</warn>
* @returns {MessageEmbed}
*/
setFooter(text, iconURL) {
setFooter(options, deprecatedIconURL) {
if (options === null) {
this.footer = {};
return this;
}

if (typeof options === 'string') {
if (!deprecationEmittedForSetFooter) {
process.emitWarning(
'Passing strings for MessageEmbed#setFooter is deprecated. Pass a sole object instead.',
'DeprecationWarning',
);

deprecationEmittedForSetFooter = true;
}

options = { text: options, iconURL: deprecatedIconURL };
}

const { text, iconURL } = options;
this.footer = { text: Util.verifyString(text, RangeError, 'EMBED_FOOTER_TEXT'), iconURL };
return this;
}
Expand Down
11 changes: 9 additions & 2 deletions typings/index.d.ts
Expand Up @@ -1694,11 +1694,13 @@ export class MessageEmbed {
public addField(name: string, value: string, inline?: boolean): this;
public addFields(...fields: EmbedFieldData[] | EmbedFieldData[][]): this;
public setFields(...fields: EmbedFieldData[] | EmbedFieldData[][]): this;
public setAuthor(options: string | EmbedAuthorData | null): this;
/** @deprecated Supply a lone object of interface {@link EmbedAuthorData} instead of more parameters. */
public setAuthor(options: EmbedAuthorData | null): this;
/** @deprecated Supply a lone object of interface {@link EmbedAuthorData} instead. */
public setAuthor(name: string, iconURL?: string, url?: string): this;
public setColor(color: ColorResolvable): this;
public setDescription(description: string): this;
public setFooter(options: EmbedFooterData | null): this;
/** @deprecated Supply a lone object of interface {@link EmbedFooterData} instead. */
public setFooter(text: string, iconURL?: string): this;
public setImage(url: string): this;
public setThumbnail(url: string): this;
Expand Down Expand Up @@ -4318,6 +4320,11 @@ export interface EmbedFieldData {
inline?: boolean;
}

export interface EmbedFooterData {
text: string;
iconURL?: string;
}

export type EmojiIdentifierResolvable = string | EmojiResolvable;

export type EmojiResolvable = Snowflake | GuildEmoji | ReactionEmoji;
Expand Down

0 comments on commit 3496516

Please sign in to comment.