mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
feat: bypass cache check with forceFetch param (#4592)
This commit is contained in:
@@ -74,6 +74,7 @@ class ChannelManager extends BaseManager {
|
||||
* Obtains a channel from Discord, or the channel cache if it's already available.
|
||||
* @param {Snowflake} id ID of the channel
|
||||
* @param {boolean} [cache=true] Whether to cache the new channel object if it isn't already
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<Channel>}
|
||||
* @example
|
||||
* // Fetch a channel by its id
|
||||
@@ -81,9 +82,11 @@ class ChannelManager extends BaseManager {
|
||||
* .then(channel => console.log(channel.name))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async fetch(id, cache = true) {
|
||||
const existing = this.cache.get(id);
|
||||
if (existing && !existing.partial) return existing;
|
||||
async fetch(id, cache = true, force = false) {
|
||||
if (!force) {
|
||||
const existing = this.cache.get(id);
|
||||
if (existing && !existing.partial) return existing;
|
||||
}
|
||||
|
||||
const data = await this.client.api.channels(id).get();
|
||||
return this.add(data, null, cache);
|
||||
|
||||
@@ -67,6 +67,7 @@ class GuildMemberManager extends BaseManager {
|
||||
* @typedef {Object} FetchMemberOptions
|
||||
* @property {UserResolvable} user The user to fetch
|
||||
* @property {boolean} [cache=true] Whether or not to cache the fetched member
|
||||
* @property {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -78,6 +79,7 @@ class GuildMemberManager extends BaseManager {
|
||||
* @property {boolean} [withPresences=false] Whether or not to include the presences
|
||||
* @property {number} [time=120e3] Timeout for receipt of members
|
||||
* @property {?string} nonce Nonce for this request (32 characters max - default to base 16 now timestamp)
|
||||
* @property {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -97,6 +99,11 @@ class GuildMemberManager extends BaseManager {
|
||||
* .then(console.log)
|
||||
* .catch(console.error);
|
||||
* @example
|
||||
* // Fetch a single member without checking cache
|
||||
* guild.members.fetch({ user, force: true })
|
||||
* .then(console.log)
|
||||
* .catch(console.error)
|
||||
* @example
|
||||
* // Fetch a single member without caching
|
||||
* guild.members.fetch({ user, cache: false })
|
||||
* .then(console.log)
|
||||
@@ -215,9 +222,12 @@ class GuildMemberManager extends BaseManager {
|
||||
.then(() => this.client.users.resolve(user));
|
||||
}
|
||||
|
||||
_fetchSingle({ user, cache }) {
|
||||
const existing = this.cache.get(user);
|
||||
if (existing && !existing.partial) return Promise.resolve(existing);
|
||||
_fetchSingle({ user, cache, force = false }) {
|
||||
if (!force) {
|
||||
const existing = this.cache.get(user);
|
||||
if (existing && !existing.partial) return Promise.resolve(existing);
|
||||
}
|
||||
|
||||
return this.client.api
|
||||
.guilds(this.guild.id)
|
||||
.members(user)
|
||||
@@ -232,9 +242,10 @@ class GuildMemberManager extends BaseManager {
|
||||
query,
|
||||
time = 120e3,
|
||||
nonce = Date.now().toString(16),
|
||||
force = false,
|
||||
} = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.guild.memberCount === this.cache.size && !query && !limit && !presences && !user_ids) {
|
||||
if (this.guild.memberCount === this.cache.size && !query && !limit && !presences && !user_ids && !force) {
|
||||
resolve(this.cache);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ class MessageManager extends BaseManager {
|
||||
* 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 {boolean} [cache=true] Whether to cache the message(s)
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>}
|
||||
* @example
|
||||
* // Get message
|
||||
@@ -62,8 +63,8 @@ class MessageManager extends BaseManager {
|
||||
* .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetch(message, cache = true) {
|
||||
return typeof message === 'string' ? this._fetchId(message, cache) : this._fetchMany(message, cache);
|
||||
fetch(message, cache = true, force = false) {
|
||||
return typeof message === 'string' ? this._fetchId(message, cache, force) : this._fetchMany(message, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,9 +128,12 @@ class MessageManager extends BaseManager {
|
||||
}
|
||||
}
|
||||
|
||||
async _fetchId(messageID, cache) {
|
||||
const existing = this.cache.get(messageID);
|
||||
if (existing && !existing.partial) return existing;
|
||||
async _fetchId(messageID, cache, force) {
|
||||
if (!force) {
|
||||
const existing = this.cache.get(messageID);
|
||||
if (existing && !existing.partial) return existing;
|
||||
}
|
||||
|
||||
const data = await this.client.api.channels[this.channel.id].messages[messageID].get();
|
||||
return this.add(data, cache);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class RoleManager extends BaseManager {
|
||||
* Obtains one or more roles from Discord, or the role cache if they're already available.
|
||||
* @param {Snowflake} [id] ID or IDs of the role(s)
|
||||
* @param {boolean} [cache=true] Whether to cache the new roles objects if it weren't already
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<Role|RoleManager>}
|
||||
* @example
|
||||
* // Fetch all roles from the guild
|
||||
@@ -45,8 +46,8 @@ class RoleManager extends BaseManager {
|
||||
* .then(role => console.log(`The role color is: ${role.color}`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async fetch(id, cache = true) {
|
||||
if (id) {
|
||||
async fetch(id, cache = true, force = false) {
|
||||
if (id && !force) {
|
||||
const existing = this.cache.get(id);
|
||||
if (existing) return existing;
|
||||
}
|
||||
|
||||
@@ -55,11 +55,15 @@ class UserManager extends BaseManager {
|
||||
* Obtains a user from Discord, or the user cache if it's already available.
|
||||
* @param {Snowflake} id ID of the user
|
||||
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
async fetch(id, cache = true) {
|
||||
const existing = this.cache.get(id);
|
||||
if (existing && !existing.partial) return existing;
|
||||
async fetch(id, cache = true, force = false) {
|
||||
if (!force) {
|
||||
const existing = this.cache.get(id);
|
||||
if (existing && !existing.partial) return existing;
|
||||
}
|
||||
|
||||
const data = await this.client.api.users(id).get();
|
||||
return this.add(data, cache);
|
||||
}
|
||||
|
||||
@@ -90,10 +90,11 @@ class Channel extends Base {
|
||||
|
||||
/**
|
||||
* Fetches this channel.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<Channel>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.client.channels.fetch(this.id, true);
|
||||
fetch(force = false) {
|
||||
return this.client.channels.fetch(this.id, true, force);
|
||||
}
|
||||
|
||||
static create(client, data, guild) {
|
||||
|
||||
@@ -61,10 +61,11 @@ class DMChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Fetch this DMChannel.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<DMChannel>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.recipient.createDM();
|
||||
fetch(force = false) {
|
||||
return this.recipient.createDM(force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -373,10 +373,11 @@ class GuildMember extends Base {
|
||||
|
||||
/**
|
||||
* Fetches this GuildMember.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.guild.members.fetch(this.id, true);
|
||||
fetch(force = false) {
|
||||
return this.guild.members.fetch({ user: this.id, cache: true, force });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -547,10 +547,11 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Fetch this message.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.channel.messages.fetch(this.id, true);
|
||||
fetch(force = false) {
|
||||
return this.channel.messages.fetch(this.id, true, force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -222,11 +222,15 @@ class User extends Base {
|
||||
|
||||
/**
|
||||
* Creates a DM channel between the client and the user.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the API
|
||||
* @returns {Promise<DMChannel>}
|
||||
*/
|
||||
async createDM() {
|
||||
const { dmChannel } = this;
|
||||
if (dmChannel && !dmChannel.partial) return dmChannel;
|
||||
async createDM(force = false) {
|
||||
if (!force) {
|
||||
const { dmChannel } = this;
|
||||
if (dmChannel && !dmChannel.partial) return dmChannel;
|
||||
}
|
||||
|
||||
const data = await this.client.api.users(this.client.user.id).channels.post({
|
||||
data: {
|
||||
recipient_id: this.id,
|
||||
@@ -265,10 +269,11 @@ class User extends Base {
|
||||
|
||||
/**
|
||||
* Fetches this user's flags.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the AP
|
||||
* @returns {Promise<UserFlags>}
|
||||
*/
|
||||
async fetchFlags() {
|
||||
if (this.flags) return this.flags;
|
||||
async fetchFlags(force = false) {
|
||||
if (this.flags && !force) return this.flags;
|
||||
const data = await this.client.api.users(this.id).get();
|
||||
this._patch(data);
|
||||
return this.flags;
|
||||
@@ -276,10 +281,11 @@ class User extends Base {
|
||||
|
||||
/**
|
||||
* Fetches this user.
|
||||
* @param {boolean} [force=false] Whether to skip the cache check and request the AP
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.client.users.fetch(this.id, true);
|
||||
fetch(force = false) {
|
||||
return this.client.users.fetch(this.id, true, force);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user