From cb7dacc7a768e43bb70c7a3d4b0790006ac2d75d Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Sun, 19 Jan 2025 14:44:33 -0400 Subject: [PATCH] feat: change `force` to `rest,flow,cache` --- src/commands/applications/chatcontext.ts | 10 ++-- src/commands/applications/entrycontext.ts | 9 ++-- src/commands/applications/menucontext.ts | 6 +-- src/components/componentcontext.ts | 6 +-- src/components/modalcontext.ts | 6 +-- src/structures/AutoModerationRule.ts | 15 +++++- src/structures/GuildBan.ts | 15 +++++- src/structures/GuildEmoji.ts | 16 +++++-- src/structures/GuildMember.ts | 15 +++++- src/structures/GuildRole.ts | 16 +++++-- src/structures/GuildTemplate.ts | 15 +++++- src/structures/Interaction.ts | 17 ++++++- src/structures/Message.ts | 19 ++++++-- src/structures/Sticker.ts | 18 ++++++-- src/structures/VoiceState.ts | 34 ++++++++++++-- src/structures/Webhook.ts | 33 ++++++++++--- src/structures/channels.ts | 56 +++++++++++++++++------ src/structures/extra/BaseGuild.ts | 17 +++++-- 18 files changed, 254 insertions(+), 69 deletions(-) diff --git a/src/commands/applications/chatcontext.ts b/src/commands/applications/chatcontext.ts index 67a37b3..5ed3ed7 100644 --- a/src/commands/applications/chatcontext.ts +++ b/src/commands/applications/chatcontext.ts @@ -150,7 +150,7 @@ export class CommandContext< return (this.messageResponse = (await this.messageResponse!.fetch()) as never); } - channel(mode?: 'rest' | 'flow'): Promise>; + channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache>; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { if (this.interaction && mode === 'cache') @@ -166,7 +166,7 @@ export class CommandContext< } } - me(mode?: 'rest' | 'flow'): Promise; + me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; me(mode: 'cache' | 'rest' | 'flow' = 'flow') { if (!this.guildId) @@ -186,11 +186,7 @@ export class CommandContext< guild(mode: 'cache'): ReturnCache | undefined>; guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { if (!this.guildId) - return (mode === 'cache' - ? this.client.cache.adapter.isAsync - ? Promise.resolve() - : undefined - : Promise.resolve()) as unknown as undefined; + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': return ( diff --git a/src/commands/applications/entrycontext.ts b/src/commands/applications/entrycontext.ts index ad6425b..368e01e 100644 --- a/src/commands/applications/entrycontext.ts +++ b/src/commands/applications/entrycontext.ts @@ -89,19 +89,22 @@ export class EntryPointContext ex channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { - if (this.interaction.channel && mode === 'cache') + if (mode === 'cache') return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } - me(mode?: 'rest' | 'flow'): Promise; + me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; me(mode: 'cache' | 'rest' | 'flow' = 'flow') { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': - return this.client.cache.members?.get(this.client.botId, this.guildId); + return ( + this.client.cache.members?.get(this.client.botId, this.guildId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); default: return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest'); } diff --git a/src/commands/applications/menucontext.ts b/src/commands/applications/menucontext.ts index d3c52b4..2ecf08d 100644 --- a/src/commands/applications/menucontext.ts +++ b/src/commands/applications/menucontext.ts @@ -113,14 +113,14 @@ export class MenuCommandContext< channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { - if (this.interaction.channel && mode === 'cache') + if (mode === 'cache') return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } - me(mode?: 'rest' | 'flow'): Promise; + me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; - me(mode: 'cache' | 'rest' | 'flow' = 'flow') { + me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { diff --git a/src/components/componentcontext.ts b/src/components/componentcontext.ts index a311796..a950ea0 100644 --- a/src/components/componentcontext.ts +++ b/src/components/componentcontext.ts @@ -162,7 +162,7 @@ export class ComponentContext< channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { - if (this.interaction.channel && mode === 'cache') + if (mode === 'cache') return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } @@ -172,9 +172,9 @@ export class ComponentContext< * @param mode - The mode to fetch the member. * @returns A promise that resolves to the bot member. */ - me(mode?: 'rest' | 'flow'): Promise; + me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; - me(mode: 'cache' | 'rest' | 'flow' = 'flow') { + me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { diff --git a/src/components/modalcontext.ts b/src/components/modalcontext.ts index 476ed7a..1ecfe3f 100644 --- a/src/components/modalcontext.ts +++ b/src/components/modalcontext.ts @@ -132,7 +132,7 @@ export class ModalContext extends channel(mode?: 'rest' | 'flow'): Promise; channel(mode: 'cache'): ReturnCache; channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { - if (this.interaction.channel && mode === 'cache') + if (mode === 'cache') return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } @@ -142,9 +142,9 @@ export class ModalContext extends * @param mode - The mode to fetch the member. * @returns A promise that resolves to the bot member. */ - me(mode?: 'rest' | 'flow'): Promise; + me(mode?: 'rest' | 'flow'): Promise; me(mode: 'cache'): ReturnCache; - me(mode: 'cache' | 'rest' | 'flow' = 'flow') { + me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any { if (!this.guildId) return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { diff --git a/src/structures/AutoModerationRule.ts b/src/structures/AutoModerationRule.ts index a964740..e05f119 100644 --- a/src/structures/AutoModerationRule.ts +++ b/src/structures/AutoModerationRule.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import type { AutoModerationRuleStructure, GuildMemberStructure, GuildStructure } from '../client'; import type { UsingClient } from '../commands'; import type { MethodContext, ObjectToLower } from '../common'; @@ -19,8 +20,18 @@ export class AutoModerationRule extends DiscordBase { return this.client.members.fetch(this.guildId, this.creatorId, force); } - guild(force = false): Promise> { - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow'): any { + switch (mode) { + case 'cache': + 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'); + } } fetch(): Promise { diff --git a/src/structures/GuildBan.ts b/src/structures/GuildBan.ts index cc4f0a6..1c6ad7e 100644 --- a/src/structures/GuildBan.ts +++ b/src/structures/GuildBan.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import type { GuildBanStructure, GuildStructure } from '../client'; import type { UsingClient } from '../commands'; import { Formatter, type MethodContext, type ObjectToLower } from '../common'; @@ -24,8 +25,18 @@ export class GuildBan extends DiscordBase { return this.client.bans.remove(this.guildId, this.id, reason); } - guild(force = false): Promise> { - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + 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'); + } } fetch(force = false): Promise { diff --git a/src/structures/GuildEmoji.ts b/src/structures/GuildEmoji.ts index fbb35e8..4d6921b 100644 --- a/src/structures/GuildEmoji.ts +++ b/src/structures/GuildEmoji.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import type { BaseCDNUrlOptions } from '../api'; import type { GuildEmojiStructure, GuildStructure } from '../client'; import type { UsingClient } from '../commands'; @@ -16,9 +17,18 @@ export class GuildEmoji extends DiscordBase { super(client, { ...data, id: data.id! }); } - async guild(force = false): Promise | undefined> { - if (!this.guildId) return; - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow'): unknown { + switch (mode) { + case 'cache': + 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'); + } } edit(body: RESTPatchAPIChannelJSONBody, reason?: string): Promise { diff --git a/src/structures/GuildMember.ts b/src/structures/GuildMember.ts index 0cc238d..8b8f638 100644 --- a/src/structures/GuildMember.ts +++ b/src/structures/GuildMember.ts @@ -7,6 +7,7 @@ export type GuildMemberData = | GatewayGuildMemberAddDispatchData | APIInteractionDataResolvedGuildMember; +import type { ReturnCache } from '../../src'; import { type DMChannelStructure, type GuildMemberStructure, @@ -59,8 +60,18 @@ export class BaseGuildMember extends DiscordBase { this.patch(data); } - guild(force = false): Promise> { - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + 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'); + } } fetch(force = false): Promise { diff --git a/src/structures/GuildRole.ts b/src/structures/GuildRole.ts index b781fb6..b33ede2 100644 --- a/src/structures/GuildRole.ts +++ b/src/structures/GuildRole.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import type { GuildRoleStructure, GuildStructure } from '../client'; import type { UsingClient } from '../commands'; import { Formatter, type MethodContext, type ObjectToLower } from '../common'; @@ -23,9 +24,18 @@ export class GuildRole extends DiscordBase { this.permissions = new PermissionsBitField(BigInt(data.permissions)); } - async guild(force = false): Promise | undefined> { - if (!this.guildId) return; - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + 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'); + } } fetch(force = false): Promise { diff --git a/src/structures/GuildTemplate.ts b/src/structures/GuildTemplate.ts index 1ce102c..8db3393 100644 --- a/src/structures/GuildTemplate.ts +++ b/src/structures/GuildTemplate.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import type { GuildStructure, GuildTemplateStructure } from '../client'; import type { UsingClient } from '../commands'; import type { MethodContext, ObjectToLower } from '../common'; @@ -12,8 +13,18 @@ export class GuildTemplate extends Base { this.__patchThis(data); } - guild(force = false): Promise> { - return this.client.guilds.fetch(this.sourceGuildId, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + return ( + this.client.cache.guilds?.get(this.sourceGuildId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.guilds.fetch(this.sourceGuildId, mode === 'rest'); + } } fetch(): Promise { diff --git a/src/structures/Interaction.ts b/src/structures/Interaction.ts index b291eb6..866e6b8 100644 --- a/src/structures/Interaction.ts +++ b/src/structures/Interaction.ts @@ -42,6 +42,7 @@ import { type RESTPostAPIInteractionCallbackResult, } from '../types'; +import type { ReturnCache } from '../../src'; import type { RawFile } from '../api'; import { ActionRow, Embed, Modal, PollBuilder, resolveAttachment, resolveFiles } from '../builders'; import { @@ -364,8 +365,20 @@ export class BaseInteraction< } } - async fetchGuild(force = false): Promise | undefined> { - return this.guildId ? this.client.guilds.fetch(this.guildId, force) : undefined; + fetchGuild(mode?: 'rest' | 'flow'): Promise | undefined>; + fetchGuild(mode: 'cache'): ReturnCache | undefined>; + fetchGuild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + if (!this.guildId) + 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) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.guilds.fetch(this.guildId, mode === 'rest'); + } } } diff --git a/src/structures/Message.ts b/src/structures/Message.ts index 11aee20..1fcf107 100644 --- a/src/structures/Message.ts +++ b/src/structures/Message.ts @@ -81,14 +81,27 @@ export class BaseMessage extends DiscordBase { ) as any; 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'); } } - channel(force = false): Promise { - return this.client.channels.fetch(this.channelId, force); + channel(mode?: 'rest' | 'flow'): Promise; + channel(mode: 'cache'): ReturnCache; + channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + return ( + this.client.cache.channels?.get(this.channelId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.channels.fetch(this.channelId, mode === 'rest'); + } } react(emoji: EmojiResolvable) { diff --git a/src/structures/Sticker.ts b/src/structures/Sticker.ts index 96463c8..ebea926 100644 --- a/src/structures/Sticker.ts +++ b/src/structures/Sticker.ts @@ -1,4 +1,5 @@ import type { GuildStructure, RawFile, StickerStructure, UsingClient } from '..'; +import type { ReturnCache } from '../../src'; import type { Attachment, AttachmentBuilder } from '../builders'; import { Transformers, type UserStructure } from '../client/transformers'; import type { MethodContext, ObjectToLower } from '../common'; @@ -16,9 +17,20 @@ export class Sticker extends DiscordBase { } } - async guild(force = false): Promise | undefined> { - if (!this.guildId) return; - return this.client.guilds.fetch(this.guildId, force); + guild(mode?: 'rest' | 'flow'): Promise | undefined>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + if (!this.guildId) + 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) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.guilds.fetch(this.guildId, mode === 'rest'); + } } async edit(body: RESTPatchAPIGuildStickerJSONBody, reason?: string): Promise { diff --git a/src/structures/VoiceState.ts b/src/structures/VoiceState.ts index 297a4e0..75333df 100644 --- a/src/structures/VoiceState.ts +++ b/src/structures/VoiceState.ts @@ -1,9 +1,10 @@ import type { UserStructure, UsingClient, VoiceStateStructure } from '../'; +import type { GuildStructure, ReturnCache } from '../../src'; import type { VoiceStateResource } from '../cache/resources/voice-states'; import { type GuildMemberStructure, Transformers } from '../client/transformers'; import type { ObjectToLower } from '../common'; import type { APIVoiceState } from '../types'; -import type { AllGuildVoiceChannels } from './channels'; +import type { AllChannels } from './channels'; import { Base } from './extra/Base'; export interface VoiceState extends Base, ObjectToLower> {} @@ -30,9 +31,20 @@ export class VoiceState extends Base { return this.client.users.fetch(this.userId, force); } - async channel(force?: boolean): Promise { - if (!this.channelId) return; - return this.client.channels.fetch(this.channelId, force) as Promise; + channel(mode?: 'rest' | 'flow'): Promise; + channel(mode: 'cache'): ReturnCache; + channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { + if (!this.channelId) + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); + switch (mode) { + case 'cache': + return ( + this.client.cache.channels?.get(this.channelId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.channels.fetch(this.channelId, mode === 'rest'); + } } async setMute(mute = !this.mute, reason?: string): Promise { @@ -74,4 +86,18 @@ export class VoiceState extends Base { this.channelId = channel_id; return member; } + + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + 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'); + } + } } diff --git a/src/structures/Webhook.ts b/src/structures/Webhook.ts index a896446..097db4d 100644 --- a/src/structures/Webhook.ts +++ b/src/structures/Webhook.ts @@ -1,3 +1,4 @@ +import type { ReturnCache } from '../../src'; import { type AnonymousGuildStructure, type GuildStructure, @@ -66,9 +67,20 @@ export class Webhook extends DiscordBase { * @param force Whether to force fetching the guild even if it's already cached. * @returns A promise that resolves to the guild associated with the webhook, or undefined if not applicable. */ - async guild(force = false): Promise | undefined> { - if (!this.sourceGuild?.id) return; - return this.client.guilds.fetch(this.sourceGuild.id, force); + guild(mode?: 'rest' | 'flow'): Promise | undefined>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + if (!this.guildId) + 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) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.guilds.fetch(this.guildId, mode === 'rest'); + } } /** @@ -76,9 +88,18 @@ export class Webhook extends DiscordBase { * @param force Whether to force fetching the channel even if it's already cached. * @returns A promise that resolves to the channel associated with the webhook, or undefined if not applicable. */ - async channel(force = false): Promise { - if (!this.sourceChannel?.id) return; - return this.client.channels.fetch(this.sourceChannel.id, force); + channel(mode?: 'rest' | 'flow'): Promise; + channel(mode: 'cache'): ReturnCache; + channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + return ( + this.client.cache.channels?.get(this.channelId) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.channels.fetch(this.channelId, mode === 'rest'); + } } /** diff --git a/src/structures/channels.ts b/src/structures/channels.ts index dc5e974..7830b7d 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -84,8 +84,18 @@ export class BaseNoEditableChannel extends DiscordBase { - return this.client.channels.fetch(this.id, force); + fetch(mode?: 'rest' | 'flow'): Promise; + fetch(mode: 'cache'): ReturnCache; + fetch(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + return ( + this.client.cache.channels?.get(this.id) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.channels.fetch(this.id, mode === 'rest'); + } } delete(reason?: string): Promise { @@ -195,6 +205,7 @@ interface IChannelTypes { export interface BaseGuildChannel extends ObjectToLower, 'permission_overwrites'>> {} export class BaseGuildChannel extends BaseChannel { + declare guildId: string; constructor(client: UsingClient, data: APIGuildChannel) { const { permission_overwrites, ...rest } = data; super(client, rest); @@ -225,8 +236,18 @@ export class BaseGuildChannel extends BaseChannel { return this.client.channels.overwritesFor(this.id, member); } - guild(force = false): Promise> { - return this.client.guilds.fetch(this.guildId!, force); + guild(mode?: 'rest' | 'flow'): Promise>; + guild(mode: 'cache'): ReturnCache | undefined>; + guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + 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'); + } } get url() { @@ -341,7 +362,7 @@ export class MessagesMethods extends DiscordBase { } export interface TextBaseGuildChannel - extends ObjectToLower>, + extends ObjectToLower>, MessagesMethods {} @mix(MessagesMethods) export class TextBaseGuildChannel extends BaseGuildChannel {} @@ -491,7 +512,7 @@ export class WebhookChannelMethods extends DiscordBase { } export interface TextGuildChannel - extends ObjectToLower>, + extends ObjectToLower>, BaseGuildChannel, TextBaseGuildChannel, WebhookChannelMethods {} @@ -506,26 +527,30 @@ export class DMChannel extends BaseNoEditableChannel { declare type: ChannelType.DM; } export interface VoiceChannel - extends ObjectToLower>, + extends ObjectToLower>, Omit, VoiceChannelMethods, - WebhookChannelMethods {} + WebhookChannelMethods { + guildId: string; +} @mix(TextGuildChannel, VoiceChannelMethods) export class VoiceChannel extends BaseGuildChannel { declare type: ChannelType.GuildVoice; } export interface StageChannel - extends ObjectToLower>, + extends ObjectToLower>, TopicableGuildChannel, - VoiceChannelMethods {} + VoiceChannelMethods { + guildId: string; +} @mix(TopicableGuildChannel, VoiceChannelMethods) export class StageChannel extends BaseGuildChannel { declare type: ChannelType.GuildStageVoice; } export interface MediaChannel - extends ObjectToLower>, + extends ObjectToLower>, ThreadOnlyMethods {} @mix(ThreadOnlyMethods) export class MediaChannel extends BaseGuildChannel { @@ -533,7 +558,7 @@ export class MediaChannel extends BaseGuildChannel { } export interface ForumChannel - extends ObjectToLower>, + extends ObjectToLower>, Omit, WebhookChannelMethods {} @mix(ThreadOnlyMethods, WebhookChannelMethods) @@ -542,7 +567,7 @@ export class ForumChannel extends BaseGuildChannel { } export interface ThreadChannel - extends ObjectToLower>, + extends ObjectToLower>, Omit {} @mix(TextBaseGuildChannel) export class ThreadChannel extends BaseChannel< @@ -602,7 +627,8 @@ export class ThreadChannel extends BaseChannel< } } -export interface CategoryChannel extends ObjectToLower> {} +export interface CategoryChannel + extends ObjectToLower> {} export class CategoryChannel extends (BaseGuildChannel as unknown as ToClass< Omit, @@ -612,7 +638,7 @@ export class CategoryChannel extends (BaseGuildChannel as unknown as ToClass< } export interface NewsChannel - extends ObjectToLower>, + extends ObjectToLower>, WebhookChannelMethods, Omit {} @mix(TextGuildChannel, WebhookChannelMethods) diff --git a/src/structures/extra/BaseGuild.ts b/src/structures/extra/BaseGuild.ts index 0fab57e..6584878 100644 --- a/src/structures/extra/BaseGuild.ts +++ b/src/structures/extra/BaseGuild.ts @@ -1,4 +1,6 @@ import type { WorkerClient } from '../..'; +import type { ReturnCache } from '../../../src'; +import type { GuildStructure } from '../../client'; import { type ObjectToLower, calculateShardId } from '../../common'; import type { ImageOptions } from '../../common/types/options'; import { type APIPartialGuild, GuildFeature } from '../../types'; @@ -31,9 +33,18 @@ export class BaseGuild extends DiscordBase { /** * Fetch guild on API */ - async fetch() { - const data = await this.api.guilds(this.id).get(); - return new BaseGuild(this.client, data); + fetch(mode?: 'rest' | 'flow'): Promise>; + fetch(mode: 'cache'): ReturnCache | undefined>; + fetch(mode: 'cache' | 'rest' | 'flow' = 'flow') { + switch (mode) { + case 'cache': + return ( + this.client.cache.guilds?.get(this.id) || + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) + ); + default: + return this.client.guilds.fetch(this.id, mode === 'rest'); + } } /**