Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/actions/MessageUpdate.js
Expand Up @@ -9,7 +9,7 @@ class MessageUpdateAction extends Action {
const { id, channel_id, guild_id, author, timestamp, type } = data;
const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel);
if (message) {
const old = message._update(data, true);
const old = message._update(data);
return {
old,
updated: message,
Expand Down
119 changes: 63 additions & 56 deletions src/structures/Message.js
Expand Up @@ -52,13 +52,19 @@ class Message extends Base {
if (data) this._patch(data);
}

_patch(data, partial = false) {
_patch(data) {
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
/**
* The message's id
* @type {Snowflake}
*/
this.id = data.id;

/**
* The timestamp the message was sent at
* @type {number}
*/
this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp;
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved

if ('type' in data) {
/**
* The type of the message
Expand All @@ -71,9 +77,9 @@ class Message extends Base {
* @type {?boolean}
*/
this.system = SystemMessageTypes.includes(this.type);
} else if (typeof this.type !== 'string') {
this.system = null;
this.type = null;
} else {
this.system ??= null;
this.type ??= null;
}

if ('content' in data) {
Expand All @@ -82,8 +88,8 @@ class Message extends Base {
* @type {?string}
*/
this.content = data.content;
} else if (typeof this.content !== 'string') {
this.content = null;
} else {
this.content ??= null;
}

if ('author' in data) {
Expand Down Expand Up @@ -116,37 +122,39 @@ class Message extends Base {
this.tts ??= null;
}

if (!partial) {
if ('nonce' in data) {
/**
* A random number or string used for checking message delivery
* <warn>This is only received after the message was sent successfully, and
* lost if re-fetched</warn>
* @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
* @type {MessageEmbed[]}
*/
this.embeds = data.embeds?.map(e => new Embed(e, true)) ?? [];
this.embeds = data.embeds.map(e => new Embed(e, true));
} 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
* @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 {
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
* @type {Collection<Snowflake, MessageAttachment>}
Expand All @@ -161,7 +169,7 @@ class Message extends Base {
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
* @type {Collection<Snowflake, Sticker>}
Expand All @@ -173,23 +181,18 @@ class Message extends Base {
this.stickers = new Collection(this.stickers);
}

if (!partial) {
/**
* The timestamp the message was sent at
* @type {number}
*/
this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp;
}

if ('edited_timestamp' in data || !partial) {
// Discord sends null if the message has not been edited
if (data.edited_timestamp) {
/**
* The timestamp the message was last edited at (if applicable)
* @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
* @type {ReactionManager}
Expand All @@ -200,9 +203,11 @@ class Message extends Base {
this.reactions._add(reaction);
}
}
} else {
this.reactions ??= new ReactionManager(this);
}

if (!partial) {
if (!this.mentions) {
/**
* All valid mentions that the message contains
* @type {MessageMentions}
Expand All @@ -226,52 +231,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
* @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
* @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
* @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
* @type {?MessageActivity}
*/
this.activity = data.activity
? {
partyId: data.activity.party_id,
type: data.activity.type,
}
: null;
this.activity = {
partyId: data.activity.party_id,
type: data.activity.type,
};
} else {
this.activity ??= null;
}

if ('thread' in data) {
this.client.channels._add(data.thread, this.guild);
}

if (this.member && data.member) {
this.member._patch(data.member);
} else if (data.member && this.guild && 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
* @type {Readonly<MessageFlags>}
Expand All @@ -296,18 +309,18 @@ class Message extends Base {
* @property {?Snowflake} messageId The message's id that was referenced
*/

if ('message_reference' in data || !partial) {
if ('message_reference' in data) {
/**
* Message reference data
* @type {?MessageReference}
*/
this.reference = data.message_reference
? {
channelId: data.message_reference.channel_id,
guildId: data.message_reference.guild_id,
messageId: data.message_reference.message_id,
}
: null;
this.reference = {
channelId: data.message_reference.channel_id,
guildId: data.message_reference.guild_id,
messageId: data.message_reference.message_id,
};
} else {
this.reference ??= null;
}

if (data.referenced_message) {
Expand Down Expand Up @@ -339,12 +352,6 @@ class Message extends Base {
}
}

_update(data, partial = false) {
SpaceEEC marked this conversation as resolved.
Show resolved Hide resolved
const clone = this._clone();
this._patch(data, partial);
return clone;
}

/**
* The channel that the message was sent in
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}
Expand Down
5 changes: 1 addition & 4 deletions typings/index.d.ts
Expand Up @@ -1186,10 +1186,7 @@ type AwaitMessageCollectorOptionsParams<T extends MessageComponentType | Message

export class Message extends Base {
public constructor(client: Client, data: RawMessageData);
private _patch(data: RawPartialMessageData, partial: true): void;
private _patch(data: RawMessageData, partial?: boolean): void;
private _update(data: RawPartialMessageData, partial: true): Message;
private _update(data: RawMessageData, partial?: boolean): Message;
private _patch(data: RawPartialMessageData | RawMessageData): void;

public activity: MessageActivity | null;
public applicationId: Snowflake | null;
Expand Down