diff --git a/src/managers/GuildManager.js b/src/managers/GuildManager.js index 0454530f5..96ba3b96a 100644 --- a/src/managers/GuildManager.js +++ b/src/managers/GuildManager.js @@ -214,6 +214,28 @@ class GuildManager extends BaseManager { }, reject), ); } + + /** + * Obtains a guild from Discord, or the guild cache if it's already available. + * @param {Snowflake} id ID of the guild + * @param {boolean} [cache=true] Whether to cache the new guild object if it isn't already + * @param {boolean} [force=false] Whether to skip the cache check and request the API + * @returns {Promise} + * @example + * // Fetch a guild by its id + * client.guilds.fetch('222078108977594368') + * .then(guild => console.log(guild.name)) + * .catch(console.error); + */ + async fetch(id, cache = true, force = false) { + if (!force) { + const existing = this.cache.get(id); + if (existing) return existing; + } + + const data = await this.client.api.guilds(id).get({ query: { with_counts: true } }); + return this.add(data, cache); + } } module.exports = GuildManager; diff --git a/typings/index.d.ts b/typings/index.d.ts index e11f9291c..f3e347b42 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1912,6 +1912,7 @@ declare module 'discord.js' { name: string, options?: { region?: string; icon: BufferResolvable | Base64Resolvable | null }, ): Promise; + public fetch(id: Snowflake, cache?: boolean, force?: boolean): Promise; } export class GuildMemberManager extends BaseManager {