fix(Message): avoid overwriting properties in _patch (#6738)

This commit is contained in:
SpaceEEC
2021-10-03 15:27:00 +02:00
committed by GitHub
parent 6e5c768379
commit a8c21cd754
3 changed files with 62 additions and 58 deletions

View File

@@ -9,7 +9,7 @@ class MessageUpdateAction extends Action {
const { id, channel_id, guild_id, author, timestamp, type } = data; const { id, channel_id, guild_id, author, timestamp, type } = data;
const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel); const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel);
if (message) { if (message) {
const old = message._update(data, true); const old = message._update(data);
return { return {
old, old,
updated: message, updated: message,

View File

@@ -48,13 +48,19 @@ class Message extends Base {
if (data) this._patch(data); if (data) this._patch(data);
} }
_patch(data, partial = false) { _patch(data) {
/** /**
* The message's id * The message's id
* @type {Snowflake} * @type {Snowflake}
*/ */
this.id = data.id; this.id = data.id;
/**
* The timestamp the message was sent at
* @type {number}
*/
this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp;
if ('type' in data) { if ('type' in data) {
/** /**
* The type of the message * The type of the message
@@ -78,8 +84,8 @@ class Message extends Base {
* @type {?string} * @type {?string}
*/ */
this.content = data.content; this.content = data.content;
} else if (typeof this.content !== 'string') { } else {
this.content = null; this.content ??= null;
} }
if ('author' in data) { if ('author' in data) {
@@ -112,37 +118,39 @@ class Message extends Base {
this.tts ??= null; this.tts ??= null;
} }
if (!partial) { if ('nonce' in data) {
/** /**
* A random number or string used for checking message delivery * A random number or string used for checking message delivery
* <warn>This is only received after the message was sent successfully, and * <warn>This is only received after the message was sent successfully, and
* lost if re-fetched</warn> * lost if re-fetched</warn>
* @type {?string} * @type {?string}
*/ */
this.nonce = 'nonce' in data ? data.nonce : null; this.nonce = data.nonce;
} else {
this.nonce ??= null;
} }
if ('embeds' in data || !partial) { if ('embeds' in data) {
/** /**
* A list of embeds in the message - e.g. YouTube Player * A list of embeds in the message - e.g. YouTube Player
* @type {MessageEmbed[]} * @type {MessageEmbed[]}
*/ */
this.embeds = data.embeds?.map(e => new Embed(e, true)) ?? []; this.embeds = data.embeds.map(e => new Embed(e, true));
} else { } else {
this.embeds = this.embeds.slice(); this.embeds = this.embeds?.slice() ?? [];
} }
if ('components' in data || !partial) { if ('components' in data) {
/** /**
* A list of MessageActionRows in the message * A list of MessageActionRows in the message
* @type {MessageActionRow[]} * @type {MessageActionRow[]}
*/ */
this.components = data.components?.map(c => BaseMessageComponent.create(c, this.client)) ?? []; this.components = data.components.map(c => BaseMessageComponent.create(c, this.client));
} else { } else {
this.components = this.components.slice(); this.components = this.components?.slice() ?? [];
} }
if ('attachments' in data || !partial) { if ('attachments' in data) {
/** /**
* A collection of attachments in the message - e.g. Pictures - mapped by their ids * A collection of attachments in the message - e.g. Pictures - mapped by their ids
* @type {Collection<Snowflake, MessageAttachment>} * @type {Collection<Snowflake, MessageAttachment>}
@@ -157,7 +165,7 @@ class Message extends Base {
this.attachments = new Collection(this.attachments); this.attachments = new Collection(this.attachments);
} }
if ('sticker_items' in data || 'stickers' in data || !partial) { if ('sticker_items' in data || 'stickers' in data) {
/** /**
* A collection of stickers in the message * A collection of stickers in the message
* @type {Collection<Snowflake, Sticker>} * @type {Collection<Snowflake, Sticker>}
@@ -169,23 +177,18 @@ class Message extends Base {
this.stickers = new Collection(this.stickers); this.stickers = new Collection(this.stickers);
} }
if (!partial) { // Discord sends null if the message has not been edited
/** if (data.edited_timestamp) {
* The timestamp the message was sent at
* @type {number}
*/
this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp;
}
if ('edited_timestamp' in data || !partial) {
/** /**
* The timestamp the message was last edited at (if applicable) * The timestamp the message was last edited at (if applicable)
* @type {?number} * @type {?number}
*/ */
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null; this.editedTimestamp = new Date(data.edited_timestamp).getTime();
} else {
this.editedTimestamp ??= null;
} }
if ('reactions' in data || !partial) { if ('reactions' in data) {
/** /**
* A manager of the reactions belonging to this message * A manager of the reactions belonging to this message
* @type {ReactionManager} * @type {ReactionManager}
@@ -196,9 +199,11 @@ class Message extends Base {
this.reactions._add(reaction); this.reactions._add(reaction);
} }
} }
} else {
this.reactions ??= new ReactionManager(this);
} }
if (!partial) { if (!this.mentions) {
/** /**
* All valid mentions that the message contains * All valid mentions that the message contains
* @type {MessageMentions} * @type {MessageMentions}
@@ -222,52 +227,60 @@ class Message extends Base {
); );
} }
if ('webhook_id' in data || !partial) { if ('webhook_id' in data) {
/** /**
* The id of the webhook that sent the message, if applicable * The id of the webhook that sent the message, if applicable
* @type {?Snowflake} * @type {?Snowflake}
*/ */
this.webhookId = data.webhook_id ?? null; this.webhookId = data.webhook_id;
} else {
this.webhookId ??= null;
} }
if ('application' in data || !partial) { if ('application' in data) {
/** /**
* Supplemental application information for group activities * Supplemental application information for group activities
* @type {?ClientApplication} * @type {?ClientApplication}
*/ */
this.groupActivityApplication = data.application ? new ClientApplication(this.client, data.application) : null; this.groupActivityApplication = new ClientApplication(this.client, data.application);
} else {
this.groupActivityApplication ??= null;
} }
if ('application_id' in data || !partial) { if ('application_id' in data) {
/** /**
* The id of the application of the interaction that sent this message, if any * The id of the application of the interaction that sent this message, if any
* @type {?Snowflake} * @type {?Snowflake}
*/ */
this.applicationId = data.application_id ?? null; this.applicationId = data.application_id;
} else {
this.applicationId ??= null;
} }
if ('activity' in data || !partial) { if ('activity' in data) {
/** /**
* Group activity * Group activity
* @type {?MessageActivity} * @type {?MessageActivity}
*/ */
this.activity = data.activity this.activity = {
? { partyId: data.activity.party_id,
partyId: data.activity.party_id, type: data.activity.type,
type: data.activity.type, };
} } else {
: null; this.activity ??= null;
} }
if ('thread' in data) { if ('thread' in data) {
this.client.channels._add(data.thread, this.guild); this.client.channels._add(data.thread, this.guild);
} }
if (this.member && data.member) { if (this.member && data.member) {
this.member._patch(data.member); this.member._patch(data.member);
} else if (data.member && this.guild && this.author) { } else if (data.member && this.guild && this.author) {
this.guild.members._add(Object.assign(data.member, { user: this.author })); this.guild.members._add(Object.assign(data.member, { user: this.author }));
} }
if ('flags' in data || !partial) { if ('flags' in data) {
/** /**
* Flags that are applied to the message * Flags that are applied to the message
* @type {Readonly<MessageFlags>} * @type {Readonly<MessageFlags>}
@@ -292,18 +305,18 @@ class Message extends Base {
* @property {?Snowflake} messageId The message's id that was referenced * @property {?Snowflake} messageId The message's id that was referenced
*/ */
if ('message_reference' in data || !partial) { if ('message_reference' in data) {
/** /**
* Message reference data * Message reference data
* @type {?MessageReference} * @type {?MessageReference}
*/ */
this.reference = data.message_reference this.reference = {
? { channelId: data.message_reference.channel_id,
channelId: data.message_reference.channel_id, guildId: data.message_reference.guild_id,
guildId: data.message_reference.guild_id, messageId: data.message_reference.message_id,
messageId: data.message_reference.message_id, };
} } else {
: null; this.reference ??= null;
} }
if (data.referenced_message) { if (data.referenced_message) {
@@ -335,12 +348,6 @@ class Message extends Base {
} }
} }
_update(data, partial = false) {
const clone = this._clone();
this._patch(data, partial);
return clone;
}
/** /**
* The channel that the message was sent in * The channel that the message was sent in
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel} * @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}

5
typings/index.d.ts vendored
View File

@@ -1211,10 +1211,7 @@ type AwaitMessageCollectorOptionsParams<T extends MessageComponentType | Message
export class Message extends Base { export class Message extends Base {
private constructor(client: Client, data: RawMessageData); private constructor(client: Client, data: RawMessageData);
private _patch(data: RawPartialMessageData, partial: true): void; private _patch(data: RawPartialMessageData | RawMessageData): void;
private _patch(data: RawMessageData, partial?: boolean): void;
private _update(data: RawPartialMessageData, partial: true): Message;
private _update(data: RawMessageData, partial?: boolean): Message;
public activity: MessageActivity | null; public activity: MessageActivity | null;
public applicationId: Snowflake | null; public applicationId: Snowflake | null;