mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
fix(MessageManager): Allow caching option of an unspecified limit (#7763)
* refactor: merge parameters * refactor: remove default
This commit is contained in:
@@ -35,40 +35,79 @@ class MessageManager extends CachedManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
|
* Data that can be resolved to a Message object. This can be:
|
||||||
* `after` are mutually exclusive. All the parameters are optional.
|
* * A Message
|
||||||
* @typedef {Object} ChannelLogsQueryOptions
|
* * A Snowflake
|
||||||
* @property {number} [limit] Number of messages to acquire
|
* @typedef {Message|Snowflake} MessageResolvable
|
||||||
* @property {Snowflake} [before] The message's id to get the messages that were posted before it
|
|
||||||
* @property {Snowflake} [after] The message's id to get the messages that were posted after it
|
|
||||||
* @property {Snowflake} [around] The message's id to get the messages that were posted around it
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a message, or messages, from this channel.
|
* Options used to fetch a message.
|
||||||
|
* @typedef {BaseFetchOptions} FetchMessageOptions
|
||||||
|
* @property {MessageResolvable} [message] The message to fetch
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options used to fetch multiple messages.
|
||||||
|
* @typedef {Object} FetchMessagesOptions
|
||||||
|
* @property {number} [limit] The maximum number of messages to return
|
||||||
|
* @property {Snowflake} [before] Consider only messages before this id
|
||||||
|
* @property {Snowflake} [after] Consider only messages after this id
|
||||||
|
* @property {Snowflake} [around] Consider only messages around this id
|
||||||
|
* @property {boolean} [cache] Whether to cache the fetched messages
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches message(s) from a channel.
|
||||||
* <info>The returned Collection does not contain reaction users of the messages if they were not cached.
|
* <info>The returned Collection does not contain reaction users of the messages if they were not cached.
|
||||||
* Those need to be fetched separately in such a case.</info>
|
* Those need to be fetched separately in such a case.</info>
|
||||||
* @param {Snowflake|ChannelLogsQueryOptions} [message] The id of the message to fetch, or query parameters.
|
* @param {MessageResolvable|FetchMessageOptions|FetchMessagesOptions} [options] Options for fetching message(s)
|
||||||
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
||||||
* @returns {Promise<Message|Collection<Snowflake, Message>>}
|
* @returns {Promise<Message|Collection<Snowflake, Message>>}
|
||||||
* @example
|
* @example
|
||||||
* // Get message
|
* // Fetch a message
|
||||||
* channel.messages.fetch('99539446449315840')
|
* channel.messages.fetch('99539446449315840')
|
||||||
* .then(message => console.log(message.content))
|
* .then(message => console.log(message.content))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
* @example
|
* @example
|
||||||
* // Get messages
|
* // Fetch a maximum of 10 messages without caching
|
||||||
* channel.messages.fetch({ limit: 10 })
|
* channel.messages.fetch({ limit: 10, cache: false })
|
||||||
* .then(messages => console.log(`Received ${messages.size} messages`))
|
* .then(messages => console.log(`Received ${messages.size} messages`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
* @example
|
* @example
|
||||||
* // Get messages and filter by user id
|
* // Fetch a maximum of 10 messages without caching around a message id
|
||||||
|
* channel.messages.fetch({ limit: 10, cache: false, around: '99539446449315840' })
|
||||||
|
* .then(messages => console.log(`Received ${messages.size} messages`))
|
||||||
|
* .catch(console.error);
|
||||||
|
* @example
|
||||||
|
* // Fetch messages and filter by a user id
|
||||||
* channel.messages.fetch()
|
* channel.messages.fetch()
|
||||||
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
|
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
fetch(message, { cache = true, force = false } = {}) {
|
fetch(options) {
|
||||||
return typeof message === 'string' ? this._fetchId(message, cache, force) : this._fetchMany(message, cache);
|
if (!options) return this._fetchMany();
|
||||||
|
const { message, cache, force } = options;
|
||||||
|
const resolvedMessage = this.resolveId(message ?? options);
|
||||||
|
if (resolvedMessage) return this._fetchSingle({ message: resolvedMessage, cache, force });
|
||||||
|
return this._fetchMany(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
async _fetchSingle({ message, cache, force = false }) {
|
||||||
|
if (!force) {
|
||||||
|
const existing = this.cache.get(message);
|
||||||
|
if (existing && !existing.partial) return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await this.client.rest.get(Routes.channelMessage(this.channel.id, message));
|
||||||
|
return this._add(data, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
async _fetchMany(options = {}) {
|
||||||
|
const data = await this.client.rest.get(Routes.channelMessages(this.channel.id), {
|
||||||
|
query: makeURLSearchParams(options),
|
||||||
|
});
|
||||||
|
|
||||||
|
return data.reduce((_data, message) => _data.set(message.id, this._add(message, options.cache)), new Collection());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,13 +129,6 @@ class MessageManager extends CachedManager {
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Data that can be resolved to a Message object. This can be:
|
|
||||||
* * A Message
|
|
||||||
* * A Snowflake
|
|
||||||
* @typedef {Message|Snowflake} MessageResolvable
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves a {@link MessageResolvable} to a {@link Message} object.
|
* Resolves a {@link MessageResolvable} to a {@link Message} object.
|
||||||
* @method resolve
|
* @method resolve
|
||||||
@@ -212,25 +244,6 @@ class MessageManager extends CachedManager {
|
|||||||
|
|
||||||
await this.client.rest.delete(Routes.channelMessage(this.channel.id, message));
|
await this.client.rest.delete(Routes.channelMessage(this.channel.id, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
async _fetchId(messageId, cache, force) {
|
|
||||||
if (!force) {
|
|
||||||
const existing = this.cache.get(messageId);
|
|
||||||
if (existing && !existing.partial) return existing;
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await this.client.rest.get(Routes.channelMessage(this.channel.id, messageId));
|
|
||||||
return this._add(data, cache);
|
|
||||||
}
|
|
||||||
|
|
||||||
async _fetchMany(options = {}, cache) {
|
|
||||||
const data = await this.client.rest.get(Routes.channelMessages(this.channel.id), {
|
|
||||||
query: makeURLSearchParams(options),
|
|
||||||
});
|
|
||||||
const messages = new Collection();
|
|
||||||
for (const message of data) messages.set(message.id, this._add(message, cache));
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MessageManager;
|
module.exports = MessageManager;
|
||||||
|
|||||||
26
packages/discord.js/typings/index.d.ts
vendored
26
packages/discord.js/typings/index.d.ts
vendored
@@ -3207,11 +3207,8 @@ export class MessageManager extends CachedManager<Snowflake, Message, MessageRes
|
|||||||
public crosspost(message: MessageResolvable): Promise<Message>;
|
public crosspost(message: MessageResolvable): Promise<Message>;
|
||||||
public delete(message: MessageResolvable): Promise<void>;
|
public delete(message: MessageResolvable): Promise<void>;
|
||||||
public edit(message: MessageResolvable, options: string | MessagePayload | MessageEditOptions): Promise<Message>;
|
public edit(message: MessageResolvable, options: string | MessagePayload | MessageEditOptions): Promise<Message>;
|
||||||
public fetch(message: Snowflake, options?: BaseFetchOptions): Promise<Message>;
|
public fetch(options: MessageResolvable | FetchMessageOptions): Promise<Message>;
|
||||||
public fetch(
|
public fetch(options?: FetchMessagesOptions): Promise<Collection<Snowflake, Message>>;
|
||||||
options?: ChannelLogsQueryOptions,
|
|
||||||
cacheOptions?: BaseFetchOptions,
|
|
||||||
): Promise<Collection<Snowflake, Message>>;
|
|
||||||
public fetchPinned(cache?: boolean): Promise<Collection<Snowflake, Message>>;
|
public fetchPinned(cache?: boolean): Promise<Collection<Snowflake, Message>>;
|
||||||
public react(message: MessageResolvable, emoji: EmojiIdentifierResolvable): Promise<void>;
|
public react(message: MessageResolvable, emoji: EmojiIdentifierResolvable): Promise<void>;
|
||||||
public pin(message: MessageResolvable, reason?: string): Promise<void>;
|
public pin(message: MessageResolvable, reason?: string): Promise<void>;
|
||||||
@@ -3736,13 +3733,6 @@ export interface ChannelData {
|
|||||||
videoQualityMode?: VideoQualityMode | null;
|
videoQualityMode?: VideoQualityMode | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChannelLogsQueryOptions {
|
|
||||||
limit?: number;
|
|
||||||
before?: Snowflake;
|
|
||||||
after?: Snowflake;
|
|
||||||
around?: Snowflake;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ChannelMention = `<#${Snowflake}>`;
|
export type ChannelMention = `<#${Snowflake}>`;
|
||||||
|
|
||||||
export interface ChannelPosition {
|
export interface ChannelPosition {
|
||||||
@@ -4324,6 +4314,18 @@ export interface FetchMembersOptions {
|
|||||||
force?: boolean;
|
force?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FetchMessageOptions extends BaseFetchOptions {
|
||||||
|
message: MessageResolvable;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FetchMessagesOptions {
|
||||||
|
limit?: number;
|
||||||
|
before?: Snowflake;
|
||||||
|
after?: Snowflake;
|
||||||
|
around?: Snowflake;
|
||||||
|
cache?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FetchReactionUsersOptions {
|
export interface FetchReactionUsersOptions {
|
||||||
limit?: number;
|
limit?: number;
|
||||||
after?: Snowflake;
|
after?: Snowflake;
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ import {
|
|||||||
MessageActionRowComponentBuilder,
|
MessageActionRowComponentBuilder,
|
||||||
GuildBanManager,
|
GuildBanManager,
|
||||||
GuildBan,
|
GuildBan,
|
||||||
|
MessageManager,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||||
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
|
||||||
@@ -1019,6 +1020,22 @@ declare const guildChannelManager: GuildChannelManager;
|
|||||||
expectType<Promise<AnyChannel | null>>(guildChannelManager.fetch('0'));
|
expectType<Promise<AnyChannel | null>>(guildChannelManager.fetch('0'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare const messageManager: MessageManager;
|
||||||
|
{
|
||||||
|
expectType<Promise<Message>>(messageManager.fetch('1234567890'));
|
||||||
|
expectType<Promise<Message>>(messageManager.fetch({ message: '1234567890' }));
|
||||||
|
expectType<Promise<Message>>(messageManager.fetch({ message: '1234567890', cache: true, force: false }));
|
||||||
|
expectType<Promise<Collection<Snowflake, Message>>>(messageManager.fetch());
|
||||||
|
expectType<Promise<Collection<Snowflake, Message>>>(messageManager.fetch({}));
|
||||||
|
expectType<Promise<Collection<Snowflake, Message>>>(
|
||||||
|
messageManager.fetch({ limit: 100, before: '1234567890', cache: false }),
|
||||||
|
);
|
||||||
|
// @ts-expect-error
|
||||||
|
messageManager.fetch({ cache: true, force: false });
|
||||||
|
// @ts-expect-error
|
||||||
|
messageManager.fetch({ message: '1234567890', after: '1234567890', cache: true, force: false });
|
||||||
|
}
|
||||||
|
|
||||||
declare const roleManager: RoleManager;
|
declare const roleManager: RoleManager;
|
||||||
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch());
|
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch());
|
||||||
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch(undefined, {}));
|
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch(undefined, {}));
|
||||||
|
|||||||
Reference in New Issue
Block a user