mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
feat(Message): add messageEditHistoryMaxSize to limit stored msg edits (#4867)
This commit is contained in:
committed by
GitHub
parent
4a6fb9a7d4
commit
c412cd7521
@@ -432,6 +432,13 @@ class Client extends BaseClient {
|
|||||||
if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) {
|
if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'messageSweepInterval', 'a number');
|
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') {
|
if (typeof options.fetchAllMembers !== 'boolean') {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'fetchAllMembers', 'a boolean');
|
throw new TypeError('CLIENT_INVALID_OPTION', 'fetchAllMembers', 'a boolean');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ 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) {
|
||||||
message.patch(data);
|
const old = message.patch(data);
|
||||||
return {
|
return {
|
||||||
old: message._edits[0],
|
old,
|
||||||
updated: message,
|
updated: message,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
* @param {Object} data Raw Discord message update data
|
||||||
|
* @returns {Message}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
patch(data) {
|
patch(data) {
|
||||||
const clone = this._clone();
|
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 ('edited_timestamp' in data) this.editedTimestamp = new Date(data.edited_timestamp).getTime();
|
||||||
if ('content' in data) this.content = data.content;
|
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();
|
this.flags = new MessageFlags('flags' in data ? data.flags : 0).freeze();
|
||||||
|
|
||||||
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ const browser = (exports.browser = typeof window !== 'undefined');
|
|||||||
* sweepable (in seconds, 0 for forever)
|
* sweepable (in seconds, 0 for forever)
|
||||||
* @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than
|
* @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)
|
* 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
|
* @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)
|
* upon joining a guild (should be avoided whenever possible)
|
||||||
* @property {DisableMentionType} [disableMentions='none'] Default value for {@link MessageOptions#disableMentions}
|
* @property {DisableMentionType} [disableMentions='none'] Default value for {@link MessageOptions#disableMentions}
|
||||||
@@ -43,6 +45,7 @@ exports.DefaultOptions = {
|
|||||||
messageCacheMaxSize: 200,
|
messageCacheMaxSize: 200,
|
||||||
messageCacheLifetime: 0,
|
messageCacheLifetime: 0,
|
||||||
messageSweepInterval: 0,
|
messageSweepInterval: 0,
|
||||||
|
messageEditHistoryMaxSize: -1,
|
||||||
fetchAllMembers: false,
|
fetchAllMembers: false,
|
||||||
disableMentions: 'none',
|
disableMentions: 'none',
|
||||||
partials: [],
|
partials: [],
|
||||||
|
|||||||
3
typings/index.d.ts
vendored
3
typings/index.d.ts
vendored
@@ -978,7 +978,7 @@ declare module 'discord.js' {
|
|||||||
export class Message extends Base {
|
export class Message extends Base {
|
||||||
constructor(client: Client, data: object, channel: TextChannel | DMChannel | NewsChannel);
|
constructor(client: Client, data: object, channel: TextChannel | DMChannel | NewsChannel);
|
||||||
private _edits: Message[];
|
private _edits: Message[];
|
||||||
private patch(data: object): void;
|
private patch(data: object): Message;
|
||||||
|
|
||||||
public activity: MessageActivity | null;
|
public activity: MessageActivity | null;
|
||||||
public application: ClientApplication | null;
|
public application: ClientApplication | null;
|
||||||
@@ -2317,6 +2317,7 @@ declare module 'discord.js' {
|
|||||||
messageCacheMaxSize?: number;
|
messageCacheMaxSize?: number;
|
||||||
messageCacheLifetime?: number;
|
messageCacheLifetime?: number;
|
||||||
messageSweepInterval?: number;
|
messageSweepInterval?: number;
|
||||||
|
messageEditHistoryMaxSize?: number;
|
||||||
fetchAllMembers?: boolean;
|
fetchAllMembers?: boolean;
|
||||||
disableMentions?: 'none' | 'all' | 'everyone';
|
disableMentions?: 'none' | 'all' | 'everyone';
|
||||||
allowedMentions?: MessageMentionOptions;
|
allowedMentions?: MessageMentionOptions;
|
||||||
|
|||||||
Reference in New Issue
Block a user