feat: bypass cache check with forceFetch param (#4592)

This commit is contained in:
Carter
2020-08-12 13:23:04 -06:00
committed by GitHub
parent 0225851e40
commit 290938bf80
11 changed files with 84 additions and 45 deletions

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);
}