Skip to content

Commit

Permalink
fix(MessageManager): Allow caching option of an unspecified limit (#7763
Browse files Browse the repository at this point in the history
)

* refactor: merge parameters

* refactor: remove default
  • Loading branch information
Jiralite committed Apr 17, 2022
1 parent a674f64 commit 1b2d8de
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 54 deletions.
97 changes: 55 additions & 42 deletions packages/discord.js/src/managers/MessageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,79 @@ class MessageManager extends CachedManager {
}

/**
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
* `after` are mutually exclusive. All the parameters are optional.
* @typedef {Object} ChannelLogsQueryOptions
* @property {number} [limit] Number of messages to acquire
* @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
* Data that can be resolved to a Message object. This can be:
* * A Message
* * A Snowflake
* @typedef {Message|Snowflake} MessageResolvable
*/

/**
* 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
*/

/**
* Gets a message, or messages, from this channel.
* Fetches message(s) from a channel.
* <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>
* @param {Snowflake|ChannelLogsQueryOptions} [message] The id of the message to fetch, or query parameters.
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @param {MessageResolvable|FetchMessageOptions|FetchMessagesOptions} [options] Options for fetching message(s)
* @returns {Promise<Message|Collection<Snowflake, Message>>}
* @example
* // Get message
* // Fetch a message
* channel.messages.fetch('99539446449315840')
* .then(message => console.log(message.content))
* .catch(console.error);
* @example
* // Get messages
* channel.messages.fetch({ limit: 10 })
* // Fetch a maximum of 10 messages without caching
* channel.messages.fetch({ limit: 10, cache: false })
* .then(messages => console.log(`Received ${messages.size} messages`))
* .catch(console.error);
* @example
* // 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
* // Get messages and filter by user id
* // Fetch messages and filter by a user id
* channel.messages.fetch()
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
* .catch(console.error);
*/
fetch(message, { cache = true, force = false } = {}) {
return typeof message === 'string' ? this._fetchId(message, cache, force) : this._fetchMany(message, cache);
fetch(options) {
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());
}

/**
Expand All @@ -90,13 +129,6 @@ class MessageManager extends CachedManager {
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.
* @method resolve
Expand Down Expand Up @@ -212,25 +244,6 @@ class MessageManager extends CachedManager {

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;
26 changes: 14 additions & 12 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3207,11 +3207,8 @@ export class MessageManager extends CachedManager<Snowflake, Message, MessageRes
public crosspost(message: MessageResolvable): Promise<Message>;
public delete(message: MessageResolvable): Promise<void>;
public edit(message: MessageResolvable, options: string | MessagePayload | MessageEditOptions): Promise<Message>;
public fetch(message: Snowflake, options?: BaseFetchOptions): Promise<Message>;
public fetch(
options?: ChannelLogsQueryOptions,
cacheOptions?: BaseFetchOptions,
): Promise<Collection<Snowflake, Message>>;
public fetch(options: MessageResolvable | FetchMessageOptions): Promise<Message>;
public fetch(options?: FetchMessagesOptions): Promise<Collection<Snowflake, Message>>;
public fetchPinned(cache?: boolean): Promise<Collection<Snowflake, Message>>;
public react(message: MessageResolvable, emoji: EmojiIdentifierResolvable): Promise<void>;
public pin(message: MessageResolvable, reason?: string): Promise<void>;
Expand Down Expand Up @@ -3736,13 +3733,6 @@ export interface ChannelData {
videoQualityMode?: VideoQualityMode | null;
}

export interface ChannelLogsQueryOptions {
limit?: number;
before?: Snowflake;
after?: Snowflake;
around?: Snowflake;
}

export type ChannelMention = `<#${Snowflake}>`;

export interface ChannelPosition {
Expand Down Expand Up @@ -4324,6 +4314,18 @@ export interface FetchMembersOptions {
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 {
limit?: number;
after?: Snowflake;
Expand Down
17 changes: 17 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ import {
MessageActionRowComponentBuilder,
GuildBanManager,
GuildBan,
MessageManager,
} from '.';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
import { UnsafeButtonBuilder, UnsafeEmbedBuilder, UnsafeSelectMenuBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -1019,6 +1020,22 @@ declare const guildChannelManager: GuildChannelManager;
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;
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch());
expectType<Promise<Collection<Snowflake, Role>>>(roleManager.fetch(undefined, {}));
Expand Down

0 comments on commit 1b2d8de

Please sign in to comment.