Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: Use ThreadChannel and AnyThreadChannel consistently #10181

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
54 changes: 29 additions & 25 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ export abstract class BaseChannel extends Base {
public get url(): string;
public delete(): Promise<this>;
public fetch(force?: boolean): Promise<this>;
public isThread(): this is AnyThreadChannel;
public isThread(): this is ThreadChannel;
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
public isTextBased(): this is TextBasedChannel;
public isDMBased(): this is PartialGroupDMChannel | DMChannel | PartialDMChannel;
public isVoiceBased(): this is VoiceBasedChannel;
Expand Down Expand Up @@ -2133,7 +2133,7 @@ export class Message<InGuild extends boolean = boolean> extends Base {
public removeAttachments(): Promise<Message<InGuild>>;
public reply(options: string | MessagePayload | MessageReplyOptions): Promise<Message<InGuild>>;
public resolveComponent(customId: string): MessageActionRowComponent | null;
public startThread(options: StartThreadOptions): Promise<AnyThreadChannel>;
public startThread(options: StartThreadOptions): Promise<PublicThreadChannel<false>>;
public suppressEmbeds(suppress?: boolean): Promise<Message<InGuild>>;
public toJSON(): unknown;
public toString(): string;
Expand Down Expand Up @@ -3158,7 +3158,9 @@ export class TextChannel extends BaseGuildTextChannel {
public type: ChannelType.GuildText;
}

export type AnyThreadChannel<Forum extends boolean = boolean> = PublicThreadChannel<Forum> | PrivateThreadChannel;
export type AnyThreadChannel = PublicThreadChannel<boolean> | PrivateThreadChannel;
export type ForumThreadChannel = PublicThreadChannel<true>;
export type TextThreadChannel = PublicThreadChannel<false> | PrivateThreadChannel;

export interface PublicThreadChannel<Forum extends boolean = boolean> extends ThreadChannel<Forum> {
type: ChannelType.PublicThread | ChannelType.AnnouncementThread;
Expand Down Expand Up @@ -3207,28 +3209,25 @@ export class ThreadChannel<ThreadOnly extends boolean = boolean> extends BaseCha
public type: ThreadChannelType;
public get unarchivable(): boolean;
public delete(reason?: string): Promise<this>;
public edit(options: ThreadEditOptions): Promise<AnyThreadChannel>;
public join(): Promise<AnyThreadChannel>;
public leave(): Promise<AnyThreadChannel>;
public edit(options: ThreadEditOptions): Promise<this>;
public join(): Promise<this>;
public leave(): Promise<this>;
public permissionsFor(memberOrRole: GuildMember | Role, checkAdmin?: boolean): Readonly<PermissionsBitField>;
public permissionsFor(
memberOrRole: GuildMemberResolvable | RoleResolvable,
checkAdmin?: boolean,
): Readonly<PermissionsBitField> | null;
public fetchOwner(options?: BaseFetchOptions): Promise<ThreadMember | null>;
public fetchStarterMessage(options?: BaseFetchOptions): Promise<Message<true> | null>;
public setArchived(archived?: boolean, reason?: string): Promise<AnyThreadChannel>;
public setAutoArchiveDuration(
autoArchiveDuration: ThreadAutoArchiveDuration,
reason?: string,
): Promise<AnyThreadChannel>;
public setInvitable(invitable?: boolean, reason?: string): Promise<AnyThreadChannel>;
public setLocked(locked?: boolean, reason?: string): Promise<AnyThreadChannel>;
public setName(name: string, reason?: string): Promise<AnyThreadChannel>;
public setArchived(archived?: boolean, reason?: string): Promise<this>;
public setAutoArchiveDuration(autoArchiveDuration: ThreadAutoArchiveDuration, reason?: string): Promise<this>;
public setInvitable(invitable?: boolean, reason?: string): Promise<this>;
public setLocked(locked?: boolean, reason?: string): Promise<this>;
public setName(name: string, reason?: string): Promise<this>;
// The following 3 methods can only be run on forum threads.
public setAppliedTags(appliedTags: readonly Snowflake[], reason?: string): Promise<ThreadChannel<true>>;
public pin(reason?: string): Promise<ThreadChannel<true>>;
public unpin(reason?: string): Promise<ThreadChannel<true>>;
public setAppliedTags(appliedTags: readonly Snowflake[], reason?: string): Promise<If<ThreadOnly, this, never>>;
public pin(reason?: string): Promise<If<ThreadOnly, this, never>>;
public unpin(reason?: string): Promise<If<ThreadOnly, this, never>>;
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
public toString(): ChannelMention;
}

Expand Down Expand Up @@ -4459,15 +4458,18 @@ export class StageInstanceManager extends CachedManager<Snowflake, StageInstance

export class ThreadManager<ThreadOnly extends boolean = boolean> extends CachedManager<
Snowflake,
ThreadChannel<ThreadOnly>,
If<ThreadOnly, ForumThreadChannel, TextThreadChannel>,
ThreadChannelResolvable
> {
protected constructor(
channel: TextChannel | NewsChannel | ForumChannel | MediaChannel,
iterable?: Iterable<RawThreadChannelData>,
);
public channel: If<ThreadOnly, ForumChannel | MediaChannel, TextChannel | NewsChannel>;
public fetch(options: ThreadChannelResolvable, cacheOptions?: BaseFetchOptions): Promise<AnyThreadChannel | null>;
public fetch(
options: ThreadChannelResolvable,
cacheOptions?: BaseFetchOptions,
): Promise<If<ThreadOnly, ForumThreadChannel, TextThreadChannel> | null>;
public fetch(
options: FetchThreadsOptions & { archived: FetchArchivedThreadOptions },
cacheOptions?: { cache?: boolean },
Expand All @@ -4478,11 +4480,13 @@ export class ThreadManager<ThreadOnly extends boolean = boolean> extends CachedM
}

export class GuildTextThreadManager<AllowedThreadType> extends ThreadManager<false> {
public create(options: GuildTextThreadCreateOptions<AllowedThreadType>): Promise<ThreadChannel>;
public create(
options: GuildTextThreadCreateOptions<AllowedThreadType>,
): Promise<AllowedThreadType extends ChannelType.PrivateThread ? PrivateThreadChannel : PublicThreadChannel<false>>;
}

export class GuildForumThreadManager extends ThreadManager<true> {
public create(options: GuildForumThreadCreateOptions): Promise<ThreadChannel>;
public create(options: GuildForumThreadCreateOptions): Promise<ForumThreadChannel>;
}

export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember, ThreadMemberResolvable> {
Expand Down Expand Up @@ -4999,20 +5003,20 @@ export interface Caches {
// TODO: GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: typeof ThreadChannel<true>];
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: ForumThreadChannel];
GuildInviteManager: [manager: typeof GuildInviteManager, holds: typeof Invite];
GuildMessageManager: [manager: typeof GuildMessageManager, holds: typeof Message<true>];
GuildScheduledEventManager: [manager: typeof GuildScheduledEventManager, holds: typeof GuildScheduledEvent];
GuildStickerManager: [manager: typeof GuildStickerManager, holds: typeof Sticker];
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: typeof ThreadChannel<false>];
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: TextThreadChannel];
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
// TODO: PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
ReactionManager: [manager: typeof ReactionManager, holds: typeof MessageReaction];
ReactionUserManager: [manager: typeof ReactionUserManager, holds: typeof User];
// TODO: RoleManager: [manager: typeof RoleManager, holds: typeof Role];
StageInstanceManager: [manager: typeof StageInstanceManager, holds: typeof StageInstance];
ThreadManager: [manager: typeof ThreadManager, holds: typeof ThreadChannel];
ThreadManager: [manager: typeof ThreadManager, holds: AnyThreadChannel];
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
ThreadMemberManager: [manager: typeof ThreadMemberManager, holds: typeof ThreadMember];
UserManager: [manager: typeof UserManager, holds: typeof User];
VoiceStateManager: [manager: typeof VoiceStateManager, holds: typeof VoiceState];
Expand Down Expand Up @@ -6679,7 +6683,7 @@ export type TextChannelResolvable = Snowflake | TextChannel;

export type TextBasedChannelResolvable = Snowflake | TextBasedChannel;

export type ThreadChannelResolvable = AnyThreadChannel | Snowflake;
export type ThreadChannelResolvable = Snowflake | ThreadChannel;

export type ThreadChannelType = ChannelType.AnnouncementThread | ChannelType.PublicThread | ChannelType.PrivateThread;

Expand Down
12 changes: 10 additions & 2 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ import {
RoleSelectMenuComponent,
ChannelSelectMenuComponent,
MentionableSelectMenuComponent,
ForumThreadChannel,
TextThreadChannel,
} from '.';
import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand All @@ -225,6 +227,10 @@ const client: Client = new Client({
maxSize: 200,
keepOverLimit: member => member.id === client.user?.id,
},
ThreadManager: {
maxSize: 200,
keepOverLimit: value => !value.archived,
},
}),
});

Expand Down Expand Up @@ -1624,8 +1630,10 @@ declare const guildChannelManager: GuildChannelManager;

declare const threadManager: ThreadManager;
{
expectType<Promise<AnyThreadChannel | null>>(threadManager.fetch('12345678901234567'));
expectType<Promise<AnyThreadChannel | null>>(threadManager.fetch('12345678901234567', { cache: true, force: false }));
RedGuy12 marked this conversation as resolved.
Show resolved Hide resolved
expectType<Promise<ForumThreadChannel | TextThreadChannel | null>>(threadManager.fetch('12345678901234567'));
expectType<Promise<ForumThreadChannel | TextThreadChannel | null>>(
threadManager.fetch('12345678901234567', { cache: true, force: false }),
);
expectType<Promise<FetchedThreads>>(threadManager.fetch());
expectType<Promise<FetchedThreads>>(threadManager.fetch({}));
expectType<Promise<FetchedThreadsMore>>(threadManager.fetch({ archived: { limit: 4 } }));
Expand Down