From c412cd75213c4b683cad702a92e7386ef5b94ff7 Mon Sep 17 00:00:00 2001 From: "Matt (IPv4) Cowley" Date: Sat, 17 Oct 2020 14:34:49 +0100 Subject: [PATCH] feat(Message): add messageEditHistoryMaxSize to limit stored msg edits (#4867) --- src/client/Client.js | 7 +++++++ src/client/actions/MessageUpdate.js | 4 ++-- src/structures/Message.js | 11 +++++++++-- src/util/Constants.js | 3 +++ typings/index.d.ts | 3 ++- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index 44d187017..fd02197cd 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -432,6 +432,13 @@ class Client extends BaseClient { if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) { throw new TypeError('CLIENT_INVALID_OPTION', 'messageSweepInterval', 'a number'); } + if ( + typeof options.messageEditHistoryMaxSize !== 'number' || + isNaN(options.messageEditHistoryMaxSize) || + options.messageEditHistoryMaxSize < -1 + ) { + throw new TypeError('CLIENT_INVALID_OPTION', 'messageEditHistoryMaxSize', 'a number greater than or equal to -1'); + } if (typeof options.fetchAllMembers !== 'boolean') { throw new TypeError('CLIENT_INVALID_OPTION', 'fetchAllMembers', 'a boolean'); } diff --git a/src/client/actions/MessageUpdate.js b/src/client/actions/MessageUpdate.js index 07e2aacb3..7667dea4b 100644 --- a/src/client/actions/MessageUpdate.js +++ b/src/client/actions/MessageUpdate.js @@ -9,9 +9,9 @@ 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) { - message.patch(data); + const old = message.patch(data); return { - old: message._edits[0], + old, updated: message, }; } diff --git a/src/structures/Message.js b/src/structures/Message.js index 439eb4aaa..1b4302df0 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -235,13 +235,18 @@ class Message extends Base { } /** - * Updates the message. + * Updates the message and returns the old message. * @param {Object} data Raw Discord message update data + * @returns {Message} * @private */ patch(data) { const clone = this._clone(); - this._edits.unshift(clone); + const { messageEditHistoryMaxSize } = this.client.options; + if (messageEditHistoryMaxSize !== 0) { + const editsLimit = messageEditHistoryMaxSize === -1 ? Infinity : messageEditHistoryMaxSize; + if (this._edits.unshift(clone) > editsLimit) this._edits.pop(); + } if ('edited_timestamp' in data) this.editedTimestamp = new Date(data.edited_timestamp).getTime(); if ('content' in data) this.content = data.content; @@ -268,6 +273,8 @@ class Message extends Base { ); this.flags = new MessageFlags('flags' in data ? data.flags : 0).freeze(); + + return clone; } /** diff --git a/src/util/Constants.js b/src/util/Constants.js index 251267b9b..2d9ab42db 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -19,6 +19,8 @@ const browser = (exports.browser = typeof window !== 'undefined'); * sweepable (in seconds, 0 for forever) * @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than * the message cache lifetime (in seconds, 0 for never) + * @property {number} [messageEditHistoryMaxSize=-1] Maximum number of previous versions to hold for an edited message + * (-1 or Infinity for unlimited - don't do this without sweeping, otherwise memory usage may climb indefinitely.) * @property {boolean} [fetchAllMembers=false] Whether to cache all guild members and users upon startup, as well as * upon joining a guild (should be avoided whenever possible) * @property {DisableMentionType} [disableMentions='none'] Default value for {@link MessageOptions#disableMentions} @@ -43,6 +45,7 @@ exports.DefaultOptions = { messageCacheMaxSize: 200, messageCacheLifetime: 0, messageSweepInterval: 0, + messageEditHistoryMaxSize: -1, fetchAllMembers: false, disableMentions: 'none', partials: [], diff --git a/typings/index.d.ts b/typings/index.d.ts index f9dadb217..fd21dc033 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -978,7 +978,7 @@ declare module 'discord.js' { export class Message extends Base { constructor(client: Client, data: object, channel: TextChannel | DMChannel | NewsChannel); private _edits: Message[]; - private patch(data: object): void; + private patch(data: object): Message; public activity: MessageActivity | null; public application: ClientApplication | null; @@ -2317,6 +2317,7 @@ declare module 'discord.js' { messageCacheMaxSize?: number; messageCacheLifetime?: number; messageSweepInterval?: number; + messageEditHistoryMaxSize?: number; fetchAllMembers?: boolean; disableMentions?: 'none' | 'all' | 'everyone'; allowedMentions?: MessageMentionOptions;