diff --git a/src/commands/applications/chatcontext.ts b/src/commands/applications/chatcontext.ts index e451913..82be4c4 100644 --- a/src/commands/applications/chatcontext.ts +++ b/src/commands/applications/chatcontext.ts @@ -16,7 +16,7 @@ import type { MessageWebhookCreateBodyRequest, } from '../../common/types/write'; import { type AllChannels, ChatInputCommandInteraction, type Message } from '../../structures'; -import { MessageFlags } from '../../types'; +import { MessageFlags, type RESTGetAPIGuildQuery } from '../../types'; import { BaseContext } from '../basecontext'; import type { RegisteredMiddlewares } from '../decorators'; import type { Command, ContextOptions, OptionsRecord, SubCommand } from './chat'; @@ -182,9 +182,9 @@ export class CommandContext< } } - guild(mode?: 'rest' | 'flow'): Promise | undefined>; - guild(mode: 'cache'): ReturnCache | undefined>; - guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise | undefined>; + guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow', query?: RESTGetAPIGuildQuery) { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { @@ -194,7 +194,7 @@ export class CommandContext< (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) ); default: - return this.client.guilds.fetch(this.guildId, mode === 'rest'); + return this.client.guilds.fetch(this.guildId, { force: mode === 'rest', query }); } } diff --git a/src/commands/applications/entrycontext.ts b/src/commands/applications/entrycontext.ts index 3ee9ee5..d1c59b2 100644 --- a/src/commands/applications/entrycontext.ts +++ b/src/commands/applications/entrycontext.ts @@ -16,7 +16,7 @@ import type { When, } from '../../common'; import type { AllChannels, EntryPointInteraction } from '../../structures'; -import { MessageFlags } from '../../types'; +import { MessageFlags, type RESTGetAPIGuildQuery } from '../../types'; import { BaseContext } from '../basecontext'; import type { RegisteredMiddlewares } from '../decorators'; import type { EntryPointCommand } from './entryPoint'; @@ -110,18 +110,19 @@ export class EntryPointContext ex } } - guild(mode?: 'rest' | 'flow'): Promise | undefined>; - guild(mode: 'cache'): ReturnCache | undefined>; - guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise | undefined>; + guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow', query?: RESTGetAPIGuildQuery) { if (!this.guildId) - return ( - mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve() - ) as any; + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': - return this.client.cache.guilds?.get(this.guildId); + return ( + this.client.cache.guilds?.get(this.guildId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); default: - return this.client.guilds.fetch(this.guildId, mode === 'rest'); + return this.client.guilds.fetch(this.guildId, { force: mode === 'rest', query }); } } diff --git a/src/commands/applications/menucontext.ts b/src/commands/applications/menucontext.ts index 2ecf08d..ee47bd1 100644 --- a/src/commands/applications/menucontext.ts +++ b/src/commands/applications/menucontext.ts @@ -18,7 +18,7 @@ import { toSnakeCase, } from '../../common'; import type { AllChannels, MessageCommandInteraction, UserCommandInteraction } from '../../structures'; -import { type APIMessage, ApplicationCommandType, MessageFlags } from '../../types'; +import { type APIMessage, ApplicationCommandType, MessageFlags, type RESTGetAPIGuildQuery } from '../../types'; import { BaseContext } from '../basecontext'; import type { RegisteredMiddlewares } from '../decorators'; import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from './shared'; @@ -131,18 +131,19 @@ export class MenuCommandContext< } } - guild(mode?: 'rest' | 'flow'): Promise | undefined>; - guild(mode: 'cache'): ReturnCache | undefined>; - guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise | undefined>; + guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow', query?: RESTGetAPIGuildQuery) { if (!this.guildId) - return ( - mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve() - ) as any; + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': - return this.client.cache.guilds?.get(this.guildId); + return ( + this.client.cache.guilds?.get(this.guildId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); default: - return this.client.guilds.fetch(this.guildId, mode === 'rest'); + return this.client.guilds.fetch(this.guildId, { force: mode === 'rest', query }); } } diff --git a/src/common/shorters/guilds.ts b/src/common/shorters/guilds.ts index 4a32425..16eed81 100644 --- a/src/common/shorters/guilds.ts +++ b/src/common/shorters/guilds.ts @@ -23,6 +23,7 @@ import type { APISticker, GuildWidgetStyle, RESTGetAPICurrentUserGuildsQuery, + RESTGetAPIGuildQuery, RESTPatchAPIAutoModerationRuleJSONBody, RESTPatchAPIChannelJSONBody, RESTPatchAPIGuildChannelPositionsJSONBody, @@ -49,20 +50,30 @@ export class GuildShorter extends BaseShorter { /** * Fetches a guild by its ID. * @param id The ID of the guild to fetch. - * @param force Whether to force fetching the guild from the API even if it exists in the cache. + * @param options The options for fetching the guild. + * @param options.query The query parameters for fetching the guild. + * @param options.force Whether to force fetching the guild from the API even if it exists in the cache. * @returns A Promise that resolves to the fetched guild. */ - async fetch(id: string, force = false): Promise> { - return Transformers.Guild<'api'>(this.client, await this.raw(id, force)); + async fetch(id: string, options: GuildFetchOptions | boolean = false) { + return Transformers.Guild<'api'>(this.client, await this.raw(id, options)); } - async raw(id: string, force = false) { - if (!force) { + /** + * Fetches a guild by its ID. + * @param id The ID of the guild to fetch. + * @param options The options for fetching the guild. + * @param options.query The query parameters for fetching the guild. + * @param options.force Whether to force fetching the guild from the API even if it exists in the cache. + * @returns A Promise that resolves to the fetched guild. + */ + async raw(id: string, options: GuildFetchOptions | boolean = false) { + if (!(typeof options === 'boolean' ? options : options.force)) { const guild = await this.client.cache.guilds?.raw(id); if (guild) return guild; } - const data = await this.client.proxy.guilds(id).get(); + const data = await this.client.proxy.guilds(id).get({ query: (options as GuildFetchOptions).query }); await this.client.cache.guilds?.patch(CacheFrom.Rest, id, data); return (await this.client.cache.guilds?.raw(id)) ?? data; } @@ -384,3 +395,8 @@ export class GuildShorter extends BaseShorter { }, }; } + +export interface GuildFetchOptions { + query?: RESTGetAPIGuildQuery; + force?: boolean; +} diff --git a/src/components/componentcontext.ts b/src/components/componentcontext.ts index 545456f..8303f9d 100644 --- a/src/components/componentcontext.ts +++ b/src/components/componentcontext.ts @@ -28,7 +28,7 @@ import type { UnionToTuple, When, } from '../common'; -import { ComponentType, MessageFlags } from '../types'; +import { ComponentType, MessageFlags, type RESTGetAPIGuildQuery } from '../types'; export interface ComponentContext< Type extends keyof ContextComponentCommandInteractionMap = keyof ContextComponentCommandInteractionMap, @@ -190,18 +190,19 @@ export class ComponentContext< * @param mode - The mode to fetch the guild. * @returns A promise that resolves to the guild. */ - guild(mode?: 'rest' | 'flow'): Promise | undefined>; - guild(mode: 'cache'): ReturnCache | undefined>; - guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise | undefined>; + guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow', query?: RESTGetAPIGuildQuery) { if (!this.guildId) - return ( - mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve() - ) as any; + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': - return this.client.cache.guilds?.get(this.guildId); + return ( + this.client.cache.guilds?.get(this.guildId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); default: - return this.client.guilds.fetch(this.guildId, mode === 'rest'); + return this.client.guilds.fetch(this.guildId, { force: mode === 'rest', query }); } }