Skip to content

Commit

Permalink
feat(Message): add messageEditHistoryMaxSize to limit stored msg edits (
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Oct 17, 2020
1 parent 4a6fb9a commit c412cd7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/actions/MessageUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
11 changes: 9 additions & 2 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -268,6 +273,8 @@ class Message extends Base {
);

this.flags = new MessageFlags('flags' in data ? data.flags : 0).freeze();

return clone;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -43,6 +45,7 @@ exports.DefaultOptions = {
messageCacheMaxSize: 200,
messageCacheLifetime: 0,
messageSweepInterval: 0,
messageEditHistoryMaxSize: -1,
fetchAllMembers: false,
disableMentions: 'none',
partials: [],
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2317,6 +2317,7 @@ declare module 'discord.js' {
messageCacheMaxSize?: number;
messageCacheLifetime?: number;
messageSweepInterval?: number;
messageEditHistoryMaxSize?: number;
fetchAllMembers?: boolean;
disableMentions?: 'none' | 'all' | 'everyone';
allowedMentions?: MessageMentionOptions;
Expand Down

0 comments on commit c412cd7

Please sign in to comment.