feat: change force to rest,flow,cache

This commit is contained in:
MARCROCK22 2025-01-19 14:44:33 -04:00
parent c3ba598804
commit cb7dacc7a7
18 changed files with 254 additions and 69 deletions

View File

@ -150,7 +150,7 @@ export class CommandContext<
return (this.messageResponse = (await this.messageResponse!.fetch()) as never); return (this.messageResponse = (await this.messageResponse!.fetch()) as never);
} }
channel(mode?: 'rest' | 'flow'): Promise<If<InferWithPrefix, AllChannels | undefined, AllChannels>>; channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode: 'cache'): ReturnCache<If<InferWithPrefix, AllChannels | undefined, AllChannels>>; channel(mode: 'cache'): ReturnCache<If<InferWithPrefix, AllChannels | undefined, AllChannels>>;
channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { channel(mode: 'cache' | 'rest' | 'flow' = 'flow') {
if (this.interaction && mode === 'cache') if (this.interaction && mode === 'cache')
@ -166,7 +166,7 @@ export class CommandContext<
} }
} }
me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure>; me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure | undefined>;
me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>; me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'flow') { me(mode: 'cache' | 'rest' | 'flow' = 'flow') {
if (!this.guildId) if (!this.guildId)
@ -186,11 +186,7 @@ export class CommandContext<
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>; guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'flow') { guild(mode: 'cache' | 'rest' | 'flow' = 'flow') {
if (!this.guildId) if (!this.guildId)
return (mode === 'cache' return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
? this.client.cache.adapter.isAsync
? Promise.resolve()
: undefined
: Promise.resolve()) as unknown as undefined;
switch (mode) { switch (mode) {
case 'cache': case 'cache':
return ( return (

View File

@ -89,19 +89,22 @@ export class EntryPointContext<M extends keyof RegisteredMiddlewares = never> ex
channel(mode?: 'rest' | 'flow'): Promise<AllChannels>; channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode: 'cache'): ReturnCache<AllChannels>; channel(mode: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { 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.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
return this.client.channels.fetch(this.channelId, mode === 'rest'); return this.client.channels.fetch(this.channelId, mode === 'rest');
} }
me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure>; me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure | undefined>;
me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>; me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'flow') { me(mode: 'cache' | 'rest' | 'flow' = 'flow') {
if (!this.guildId) if (!this.guildId)
return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
switch (mode) { switch (mode) {
case 'cache': 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: default:
return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest'); return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest');
} }

View File

@ -113,14 +113,14 @@ export class MenuCommandContext<
channel(mode?: 'rest' | 'flow'): Promise<AllChannels>; channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode: 'cache'): ReturnCache<AllChannels>; channel(mode: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { 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.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
return this.client.channels.fetch(this.channelId, mode === 'rest'); return this.client.channels.fetch(this.channelId, mode === 'rest');
} }
me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure>; me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure | undefined>;
me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>; me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'flow') { me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any {
if (!this.guildId) if (!this.guildId)
return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
switch (mode) { switch (mode) {

View File

@ -162,7 +162,7 @@ export class ComponentContext<
channel(mode?: 'rest' | 'flow'): Promise<AllChannels>; channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode: 'cache'): ReturnCache<AllChannels>; channel(mode: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { 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.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
return this.client.channels.fetch(this.channelId, mode === 'rest'); return this.client.channels.fetch(this.channelId, mode === 'rest');
} }
@ -172,9 +172,9 @@ export class ComponentContext<
* @param mode - The mode to fetch the member. * @param mode - The mode to fetch the member.
* @returns A promise that resolves to the bot member. * @returns A promise that resolves to the bot member.
*/ */
me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure>; me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure | undefined>;
me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>; me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'flow') { me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any {
if (!this.guildId) if (!this.guildId)
return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
switch (mode) { switch (mode) {

View File

@ -132,7 +132,7 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
channel(mode?: 'rest' | 'flow'): Promise<AllChannels>; channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
channel(mode: 'cache'): ReturnCache<AllChannels>; channel(mode: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'flow') { 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.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
return this.client.channels.fetch(this.channelId, mode === 'rest'); return this.client.channels.fetch(this.channelId, mode === 'rest');
} }
@ -142,9 +142,9 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
* @param mode - The mode to fetch the member. * @param mode - The mode to fetch the member.
* @returns A promise that resolves to the bot member. * @returns A promise that resolves to the bot member.
*/ */
me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure>; me(mode?: 'rest' | 'flow'): Promise<GuildMemberStructure | undefined>;
me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>; me(mode: 'cache'): ReturnCache<GuildMemberStructure | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'flow') { me(mode: 'cache' | 'rest' | 'flow' = 'flow'): any {
if (!this.guildId) if (!this.guildId)
return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
switch (mode) { switch (mode) {

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import type { AutoModerationRuleStructure, GuildMemberStructure, GuildStructure } from '../client'; import type { AutoModerationRuleStructure, GuildMemberStructure, GuildStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import type { MethodContext, ObjectToLower } from '../common'; import type { MethodContext, ObjectToLower } from '../common';
@ -19,8 +20,18 @@ export class AutoModerationRule extends DiscordBase<APIAutoModerationRule> {
return this.client.members.fetch(this.guildId, this.creatorId, force); return this.client.members.fetch(this.guildId, this.creatorId, force);
} }
guild(force = false): Promise<GuildStructure<'api'>> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
return this.client.guilds.fetch(this.guildId, force); guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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<AutoModerationRuleStructure> { fetch(): Promise<AutoModerationRuleStructure> {

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import type { GuildBanStructure, GuildStructure } from '../client'; import type { GuildBanStructure, GuildStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import { Formatter, type MethodContext, type ObjectToLower } from '../common'; 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); return this.client.bans.remove(this.guildId, this.id, reason);
} }
guild(force = false): Promise<GuildStructure<'api'>> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
return this.client.guilds.fetch(this.guildId, force); guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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<GuildBanStructure> { fetch(force = false): Promise<GuildBanStructure> {

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import type { BaseCDNUrlOptions } from '../api'; import type { BaseCDNUrlOptions } from '../api';
import type { GuildEmojiStructure, GuildStructure } from '../client'; import type { GuildEmojiStructure, GuildStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
@ -16,9 +17,18 @@ export class GuildEmoji extends DiscordBase {
super(client, { ...data, id: data.id! }); super(client, { ...data, id: data.id! });
} }
async guild(force = false): Promise<GuildStructure<'api'> | undefined> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
if (!this.guildId) return; guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
return this.client.guilds.fetch(this.guildId, force); 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<GuildEmojiStructure> { edit(body: RESTPatchAPIChannelJSONBody, reason?: string): Promise<GuildEmojiStructure> {

View File

@ -7,6 +7,7 @@ export type GuildMemberData =
| GatewayGuildMemberAddDispatchData | GatewayGuildMemberAddDispatchData
| APIInteractionDataResolvedGuildMember; | APIInteractionDataResolvedGuildMember;
import type { ReturnCache } from '../../src';
import { import {
type DMChannelStructure, type DMChannelStructure,
type GuildMemberStructure, type GuildMemberStructure,
@ -59,8 +60,18 @@ export class BaseGuildMember extends DiscordBase {
this.patch(data); this.patch(data);
} }
guild(force = false): Promise<GuildStructure<'api'>> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
return this.client.guilds.fetch(this.guildId, force); guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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<GuildMemberStructure> { fetch(force = false): Promise<GuildMemberStructure> {

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import type { GuildRoleStructure, GuildStructure } from '../client'; import type { GuildRoleStructure, GuildStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import { Formatter, type MethodContext, type ObjectToLower } from '../common'; import { Formatter, type MethodContext, type ObjectToLower } from '../common';
@ -23,9 +24,18 @@ export class GuildRole extends DiscordBase {
this.permissions = new PermissionsBitField(BigInt(data.permissions)); this.permissions = new PermissionsBitField(BigInt(data.permissions));
} }
async guild(force = false): Promise<GuildStructure<'api'> | undefined> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
if (!this.guildId) return; guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
return this.client.guilds.fetch(this.guildId, force); 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<GuildRoleStructure> { fetch(force = false): Promise<GuildRoleStructure> {

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import type { GuildStructure, GuildTemplateStructure } from '../client'; import type { GuildStructure, GuildTemplateStructure } from '../client';
import type { UsingClient } from '../commands'; import type { UsingClient } from '../commands';
import type { MethodContext, ObjectToLower } from '../common'; import type { MethodContext, ObjectToLower } from '../common';
@ -12,8 +13,18 @@ export class GuildTemplate extends Base {
this.__patchThis(data); this.__patchThis(data);
} }
guild(force = false): Promise<GuildStructure<'api'>> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
return this.client.guilds.fetch(this.sourceGuildId, force); guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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<GuildTemplateStructure> { fetch(): Promise<GuildTemplateStructure> {

View File

@ -42,6 +42,7 @@ import {
type RESTPostAPIInteractionCallbackResult, type RESTPostAPIInteractionCallbackResult,
} from '../types'; } from '../types';
import type { ReturnCache } from '../../src';
import type { RawFile } from '../api'; import type { RawFile } from '../api';
import { ActionRow, Embed, Modal, PollBuilder, resolveAttachment, resolveFiles } from '../builders'; import { ActionRow, Embed, Modal, PollBuilder, resolveAttachment, resolveFiles } from '../builders';
import { import {
@ -364,8 +365,20 @@ export class BaseInteraction<
} }
} }
async fetchGuild(force = false): Promise<GuildStructure<'api'> | undefined> { fetchGuild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'> | undefined>;
return this.guildId ? this.client.guilds.fetch(this.guildId, force) : undefined; fetchGuild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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');
}
} }
} }

View File

@ -81,14 +81,27 @@ export class BaseMessage extends DiscordBase {
) as any; ) as any;
switch (mode) { switch (mode) {
case 'cache': 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: default:
return this.client.guilds.fetch(this.guildId, mode === 'rest'); return this.client.guilds.fetch(this.guildId, mode === 'rest');
} }
} }
channel(force = false): Promise<AllChannels> { channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
return this.client.channels.fetch(this.channelId, force); channel(mode: 'cache'): ReturnCache<AllChannels | undefined>;
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) { react(emoji: EmojiResolvable) {

View File

@ -1,4 +1,5 @@
import type { GuildStructure, RawFile, StickerStructure, UsingClient } from '..'; import type { GuildStructure, RawFile, StickerStructure, UsingClient } from '..';
import type { ReturnCache } from '../../src';
import type { Attachment, AttachmentBuilder } from '../builders'; import type { Attachment, AttachmentBuilder } from '../builders';
import { Transformers, type UserStructure } from '../client/transformers'; import { Transformers, type UserStructure } from '../client/transformers';
import type { MethodContext, ObjectToLower } from '../common'; import type { MethodContext, ObjectToLower } from '../common';
@ -16,9 +17,20 @@ export class Sticker extends DiscordBase {
} }
} }
async guild(force = false): Promise<GuildStructure<'api'> | undefined> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'> | undefined>;
if (!this.guildId) return; guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
return this.client.guilds.fetch(this.guildId, force); 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<StickerStructure | undefined> { async edit(body: RESTPatchAPIGuildStickerJSONBody, reason?: string): Promise<StickerStructure | undefined> {

View File

@ -1,9 +1,10 @@
import type { UserStructure, UsingClient, VoiceStateStructure } from '../'; import type { UserStructure, UsingClient, VoiceStateStructure } from '../';
import type { GuildStructure, ReturnCache } from '../../src';
import type { VoiceStateResource } from '../cache/resources/voice-states'; import type { VoiceStateResource } from '../cache/resources/voice-states';
import { type GuildMemberStructure, Transformers } from '../client/transformers'; import { type GuildMemberStructure, Transformers } from '../client/transformers';
import type { ObjectToLower } from '../common'; import type { ObjectToLower } from '../common';
import type { APIVoiceState } from '../types'; import type { APIVoiceState } from '../types';
import type { AllGuildVoiceChannels } from './channels'; import type { AllChannels } from './channels';
import { Base } from './extra/Base'; import { Base } from './extra/Base';
export interface VoiceState extends Base, ObjectToLower<Omit<VoiceStateResource, 'member'>> {} export interface VoiceState extends Base, ObjectToLower<Omit<VoiceStateResource, 'member'>> {}
@ -30,9 +31,20 @@ export class VoiceState extends Base {
return this.client.users.fetch(this.userId, force); return this.client.users.fetch(this.userId, force);
} }
async channel(force?: boolean): Promise<AllGuildVoiceChannels | undefined> { channel(mode?: 'rest' | 'flow'): Promise<AllChannels | undefined>;
if (!this.channelId) return; channel(mode: 'cache'): ReturnCache<AllChannels | undefined>;
return this.client.channels.fetch(this.channelId, force) as Promise<AllGuildVoiceChannels>; 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<GuildMemberStructure> { async setMute(mute = !this.mute, reason?: string): Promise<GuildMemberStructure> {
@ -74,4 +86,18 @@ export class VoiceState extends Base {
this.channelId = channel_id; this.channelId = channel_id;
return member; return member;
} }
guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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');
}
}
} }

View File

@ -1,3 +1,4 @@
import type { ReturnCache } from '../../src';
import { import {
type AnonymousGuildStructure, type AnonymousGuildStructure,
type GuildStructure, 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. * @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. * @returns A promise that resolves to the guild associated with the webhook, or undefined if not applicable.
*/ */
async guild(force = false): Promise<GuildStructure<'api'> | undefined> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'> | undefined>;
if (!this.sourceGuild?.id) return; guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
return this.client.guilds.fetch(this.sourceGuild.id, force); 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. * @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. * @returns A promise that resolves to the channel associated with the webhook, or undefined if not applicable.
*/ */
async channel(force = false): Promise<AllChannels | undefined> { channel(mode?: 'rest' | 'flow'): Promise<AllChannels>;
if (!this.sourceChannel?.id) return; channel(mode: 'cache'): ReturnCache<AllChannels | undefined>;
return this.client.channels.fetch(this.sourceChannel.id, force); 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');
}
} }
/** /**

View File

@ -84,8 +84,18 @@ export class BaseNoEditableChannel<T extends ChannelType> extends DiscordBase<AP
return Formatter.channelLink(this.id); return Formatter.channelLink(this.id);
} }
fetch(force = false): Promise<AllChannels> { fetch(mode?: 'rest' | 'flow'): Promise<AllChannels>;
return this.client.channels.fetch(this.id, force); fetch(mode: 'cache'): ReturnCache<AllChannels | undefined>;
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<AllChannels> { delete(reason?: string): Promise<AllChannels> {
@ -195,6 +205,7 @@ interface IChannelTypes {
export interface BaseGuildChannel extends ObjectToLower<Omit<APIGuildChannel<ChannelType>, 'permission_overwrites'>> {} export interface BaseGuildChannel extends ObjectToLower<Omit<APIGuildChannel<ChannelType>, 'permission_overwrites'>> {}
export class BaseGuildChannel extends BaseChannel<ChannelType> { export class BaseGuildChannel extends BaseChannel<ChannelType> {
declare guildId: string;
constructor(client: UsingClient, data: APIGuildChannel<ChannelType>) { constructor(client: UsingClient, data: APIGuildChannel<ChannelType>) {
const { permission_overwrites, ...rest } = data; const { permission_overwrites, ...rest } = data;
super(client, rest); super(client, rest);
@ -225,8 +236,18 @@ export class BaseGuildChannel extends BaseChannel<ChannelType> {
return this.client.channels.overwritesFor(this.id, member); return this.client.channels.overwritesFor(this.id, member);
} }
guild(force = false): Promise<GuildStructure<'api'>> { guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
return this.client.guilds.fetch(this.guildId!, force); guild(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | 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() { get url() {
@ -341,7 +362,7 @@ export class MessagesMethods extends DiscordBase {
} }
export interface TextBaseGuildChannel export interface TextBaseGuildChannel
extends ObjectToLower<Omit<APITextChannel, 'type' | 'permission_overwrites'>>, extends ObjectToLower<Omit<APITextChannel, 'type' | 'permission_overwrites' | 'guild_id'>>,
MessagesMethods {} MessagesMethods {}
@mix(MessagesMethods) @mix(MessagesMethods)
export class TextBaseGuildChannel extends BaseGuildChannel {} export class TextBaseGuildChannel extends BaseGuildChannel {}
@ -491,7 +512,7 @@ export class WebhookChannelMethods extends DiscordBase {
} }
export interface TextGuildChannel export interface TextGuildChannel
extends ObjectToLower<Omit<APITextChannel, 'type' | 'permission_overwrites'>>, extends ObjectToLower<Omit<APITextChannel, 'type' | 'permission_overwrites' | 'guild_id'>>,
BaseGuildChannel, BaseGuildChannel,
TextBaseGuildChannel, TextBaseGuildChannel,
WebhookChannelMethods {} WebhookChannelMethods {}
@ -506,26 +527,30 @@ export class DMChannel extends BaseNoEditableChannel<ChannelType.DM> {
declare type: ChannelType.DM; declare type: ChannelType.DM;
} }
export interface VoiceChannel export interface VoiceChannel
extends ObjectToLower<Omit<APIGuildVoiceChannel, 'permission_overwrites'>>, extends ObjectToLower<Omit<APIGuildVoiceChannel, 'permission_overwrites' | 'guild_id'>>,
Omit<TextGuildChannel, keyof BaseGuildChannel>, Omit<TextGuildChannel, keyof BaseGuildChannel>,
VoiceChannelMethods, VoiceChannelMethods,
WebhookChannelMethods {} WebhookChannelMethods {
guildId: string;
}
@mix(TextGuildChannel, VoiceChannelMethods) @mix(TextGuildChannel, VoiceChannelMethods)
export class VoiceChannel extends BaseGuildChannel { export class VoiceChannel extends BaseGuildChannel {
declare type: ChannelType.GuildVoice; declare type: ChannelType.GuildVoice;
} }
export interface StageChannel export interface StageChannel
extends ObjectToLower<Omit<APIGuildStageVoiceChannel, 'type' | 'permission_overwrites'>>, extends ObjectToLower<Omit<APIGuildStageVoiceChannel, 'type' | 'permission_overwrites' | 'guild_id'>>,
TopicableGuildChannel, TopicableGuildChannel,
VoiceChannelMethods {} VoiceChannelMethods {
guildId: string;
}
@mix(TopicableGuildChannel, VoiceChannelMethods) @mix(TopicableGuildChannel, VoiceChannelMethods)
export class StageChannel extends BaseGuildChannel { export class StageChannel extends BaseGuildChannel {
declare type: ChannelType.GuildStageVoice; declare type: ChannelType.GuildStageVoice;
} }
export interface MediaChannel export interface MediaChannel
extends ObjectToLower<Omit<APIGuildMediaChannel, 'type' | 'permission_overwrites'>>, extends ObjectToLower<Omit<APIGuildMediaChannel, 'type' | 'permission_overwrites' | 'guild_id'>>,
ThreadOnlyMethods {} ThreadOnlyMethods {}
@mix(ThreadOnlyMethods) @mix(ThreadOnlyMethods)
export class MediaChannel extends BaseGuildChannel { export class MediaChannel extends BaseGuildChannel {
@ -533,7 +558,7 @@ export class MediaChannel extends BaseGuildChannel {
} }
export interface ForumChannel export interface ForumChannel
extends ObjectToLower<Omit<APIGuildForumChannel, 'permission_overwrites'>>, extends ObjectToLower<Omit<APIGuildForumChannel, 'permission_overwrites' | 'guild_id'>>,
Omit<ThreadOnlyMethods, 'type' | 'edit'>, Omit<ThreadOnlyMethods, 'type' | 'edit'>,
WebhookChannelMethods {} WebhookChannelMethods {}
@mix(ThreadOnlyMethods, WebhookChannelMethods) @mix(ThreadOnlyMethods, WebhookChannelMethods)
@ -542,7 +567,7 @@ export class ForumChannel extends BaseGuildChannel {
} }
export interface ThreadChannel export interface ThreadChannel
extends ObjectToLower<Omit<APIThreadChannel, 'permission_overwrites'>>, extends ObjectToLower<Omit<APIThreadChannel, 'permission_overwrites' | 'guild_id'>>,
Omit<TextBaseGuildChannel, 'edit' | 'parentId'> {} Omit<TextBaseGuildChannel, 'edit' | 'parentId'> {}
@mix(TextBaseGuildChannel) @mix(TextBaseGuildChannel)
export class ThreadChannel extends BaseChannel< export class ThreadChannel extends BaseChannel<
@ -602,7 +627,8 @@ export class ThreadChannel extends BaseChannel<
} }
} }
export interface CategoryChannel extends ObjectToLower<Omit<APIGuildCategoryChannel, 'permission_overwrites'>> {} export interface CategoryChannel
extends ObjectToLower<Omit<APIGuildCategoryChannel, 'permission_overwrites' | 'guild_id'>> {}
export class CategoryChannel extends (BaseGuildChannel as unknown as ToClass< export class CategoryChannel extends (BaseGuildChannel as unknown as ToClass<
Omit<BaseGuildChannel, 'setParent' | 'type'>, Omit<BaseGuildChannel, 'setParent' | 'type'>,
@ -612,7 +638,7 @@ export class CategoryChannel extends (BaseGuildChannel as unknown as ToClass<
} }
export interface NewsChannel export interface NewsChannel
extends ObjectToLower<Omit<APINewsChannel, 'permission_overwrites'>>, extends ObjectToLower<Omit<APINewsChannel, 'permission_overwrites' | 'guild_id'>>,
WebhookChannelMethods, WebhookChannelMethods,
Omit<TextGuildChannel, keyof BaseGuildChannel> {} Omit<TextGuildChannel, keyof BaseGuildChannel> {}
@mix(TextGuildChannel, WebhookChannelMethods) @mix(TextGuildChannel, WebhookChannelMethods)

View File

@ -1,4 +1,6 @@
import type { WorkerClient } from '../..'; import type { WorkerClient } from '../..';
import type { ReturnCache } from '../../../src';
import type { GuildStructure } from '../../client';
import { type ObjectToLower, calculateShardId } from '../../common'; import { type ObjectToLower, calculateShardId } from '../../common';
import type { ImageOptions } from '../../common/types/options'; import type { ImageOptions } from '../../common/types/options';
import { type APIPartialGuild, GuildFeature } from '../../types'; import { type APIPartialGuild, GuildFeature } from '../../types';
@ -31,9 +33,18 @@ export class BaseGuild extends DiscordBase<APIPartialGuild> {
/** /**
* Fetch guild on API * Fetch guild on API
*/ */
async fetch() { fetch(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
const data = await this.api.guilds(this.id).get(); fetch(mode: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
return new BaseGuild(this.client, data); 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');
}
} }
/** /**