feat(Message): add messageEditHistoryMaxSize to limit stored msg edits (#4867)

This commit is contained in:
Matt (IPv4) Cowley
2020-10-17 14:34:49 +01:00
committed by GitHub
parent 4a6fb9a7d4
commit c412cd7521
5 changed files with 23 additions and 5 deletions

View File

@@ -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');
}

View File

@@ -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,
};
}

View File

@@ -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;
}
/**

View File

@@ -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: [],

3
typings/index.d.ts vendored
View File

@@ -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;