feat: fetch guilds with query (#336)

* feat: fetch guilds with query

* fix: ig

* fix: xdddd

* fix: lol
This commit is contained in:
Marcos Susaña 2025-03-12 20:02:31 -04:00 committed by GitHub
parent 5f5902b9f6
commit 2cf182f37e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 38 deletions

View File

@ -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<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'flow') {
guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache<GuildStructure<'cached'> | 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 });
}
}

View File

@ -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<M extends keyof RegisteredMiddlewares = never> ex
}
}
guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'flow') {
guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache<GuildStructure<'cached'> | 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 });
}
}

View File

@ -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<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'flow') {
guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache<GuildStructure<'cached'> | 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 });
}
}

View File

@ -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<GuildStructure<'api'>> {
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;
}

View File

@ -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<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'flow') {
guild(mode?: 'rest' | 'flow', query?: RESTGetAPIGuildQuery): Promise<GuildStructure<'cached' | 'api'> | undefined>;
guild(mode: 'cache', query?: RESTGetAPIGuildQuery): ReturnCache<GuildStructure<'cached'> | 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 });
}
}