Skip to content

Commit

Permalink
types(CommandInteractionOptionResolver): allow narrowing of `getMembe…
Browse files Browse the repository at this point in the history
…r()` (#6831)
  • Loading branch information
suneettipirneni committed Oct 16, 2021
1 parent d27fddb commit e8b6997
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 17 additions & 5 deletions typings/index.d.ts
Expand Up @@ -602,12 +602,17 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
public once(event: 'end', listener: (collected: Collection<K, V>, reason: string) => Awaitable<void>): this;
}

export class CommandInteraction extends BaseCommandInteraction {
public options: CommandInteractionOptionResolver;
export type GuildCommandInteraction<Cached extends GuildCacheState> = InteractionResponses<Cached> &
CommandInteraction<Cached>;

export class CommandInteraction<Cached extends GuildCacheState = GuildCacheState> extends BaseCommandInteraction {
public options: CommandInteractionOptionResolver<Cached>;
public inCachedGuild(): this is GuildCommandInteraction<'cached'> & this;
public inRawGuild(): this is GuildCommandInteraction<'raw'> & this;
public toString(): string;
}

export class CommandInteractionOptionResolver {
export class CommandInteractionOptionResolver<Cached extends GuildCacheState = GuildCacheState> {
private constructor(client: Client, options: CommandInteractionOption[], resolved: CommandInteractionResolvedData);
public readonly client: Client;
public readonly data: readonly CommandInteractionOption[];
Expand Down Expand Up @@ -647,8 +652,14 @@ export class CommandInteractionOptionResolver {
public getNumber(name: string, required?: boolean): number | null;
public getUser(name: string, required: true): NonNullable<CommandInteractionOption['user']>;
public getUser(name: string, required?: boolean): NonNullable<CommandInteractionOption['user']> | null;
public getMember(name: string, required: true): NonNullable<CommandInteractionOption['member']>;
public getMember(name: string, required?: boolean): NonNullable<CommandInteractionOption['member']> | null;
public getMember(
name: string,
required: true,
): CacheTypeReducer<Cached, GuildMember, NonNullable<APIInteractionDataResolvedGuildMember>>;
public getMember(
name: string,
required?: boolean,
): CacheTypeReducer<Cached, GuildMember, NonNullable<APIInteractionDataResolvedGuildMember>> | null;
public getRole(name: string, required: true): NonNullable<CommandInteractionOption['role']>;
public getRole(name: string, required?: boolean): NonNullable<CommandInteractionOption['role']> | null;
public getMentionable(
Expand Down Expand Up @@ -1086,6 +1097,7 @@ export interface GuildInteraction<Cached extends GuildCacheState = GuildCacheSta
member: CacheTypeReducer<Cached, GuildMember, APIInteractionGuildMember>;
readonly guild: CacheTypeReducer<Cached, Guild, null>;
channel: CacheTypeReducer<Cached, Exclude<TextBasedChannels, PartialDMChannel | DMChannel> | null>;
isCommand(): this is GuildCommandInteraction<Cached> & this;
}

export class Interaction extends Base {
Expand Down
13 changes: 12 additions & 1 deletion typings/tests.ts
@@ -1,4 +1,9 @@
import { APIGuildMember, APIInteractionGuildMember, APIMessage } from 'discord-api-types/v9';
import {
APIGuildMember,
APIInteractionDataResolvedGuildMember,
APIInteractionGuildMember,
APIMessage,
} from 'discord-api-types/v9';
import {
ApplicationCommand,
ApplicationCommandChannelOptionData,
Expand Down Expand Up @@ -943,15 +948,21 @@ client.on('interactionCreate', async interaction => {
consumeCachedCommand(interaction);
assertType<CommandInteraction>(interaction);
assertType<Promise<APIMessage>>(interaction.reply({ fetchReply: true }));
assertType<APIInteractionDataResolvedGuildMember | null>(interaction.options.getMember('test'));
assertType<APIInteractionDataResolvedGuildMember>(interaction.options.getMember('test', true));
} else if (interaction.inCachedGuild()) {
consumeCachedCommand(interaction);
assertType<GuildMember>(interaction.options.getMember('test', true));
assertType<GuildMember | null>(interaction.options.getMember('test'));
assertType<CommandInteraction>(interaction);
assertType<Promise<Message>>(interaction.reply({ fetchReply: true }));
} else {
// @ts-expect-error
consumeCachedCommand(interaction);
assertType<CommandInteraction>(interaction);
assertType<Promise<Message | APIMessage>>(interaction.reply({ fetchReply: true }));
assertType<APIInteractionDataResolvedGuildMember | GuildMember | null>(interaction.options.getMember('test'));
assertType<APIInteractionDataResolvedGuildMember | GuildMember>(interaction.options.getMember('test', true));
}

assertType<CommandInteraction>(interaction);
Expand Down

0 comments on commit e8b6997

Please sign in to comment.