Skip to content

Commit

Permalink
types: omit unnecessary methods from <ContextMenuCommandInteraction>.…
Browse files Browse the repository at this point in the history
…options (#10003)

* types: omit getUser/Member/Message from ContextMenu interaction

* types: omit getAttachment and add tests

* fix: remove duplicate tests
  • Loading branch information
Qjuh committed Dec 1, 2023
1 parent a44ada6 commit 17a6f5d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
45 changes: 32 additions & 13 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ export abstract class CommandInteraction<Cached extends CacheType = CacheType> e
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getUser'
| 'getMember'
| 'getAttachment'
| 'getNumber'
| 'getInteger'
Expand Down Expand Up @@ -1269,19 +1271,6 @@ export class CommandInteractionOptionResolver<Cached extends CacheType = CacheTy

export class ContextMenuCommandInteraction<Cached extends CacheType = CacheType> extends CommandInteraction<Cached> {
public commandType: ApplicationCommandType.Message | ApplicationCommandType.User;
public options: Omit<
CommandInteractionOptionResolver<Cached>,
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getNumber'
| 'getInteger'
| 'getString'
| 'getChannel'
| 'getBoolean'
| 'getSubcommandGroup'
| 'getSubcommand'
>;
public targetId: Snowflake;
public inGuild(): this is ContextMenuCommandInteraction<'raw' | 'cached'>;
public inCachedGuild(): this is ContextMenuCommandInteraction<'cached'>;
Expand Down Expand Up @@ -2222,6 +2211,21 @@ export class MessageContextMenuCommandInteraction<
Cached extends CacheType = CacheType,
> extends ContextMenuCommandInteraction<Cached> {
public commandType: ApplicationCommandType.Message;
public options: Omit<
CommandInteractionOptionResolver<Cached>,
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getUser'
| 'getNumber'
| 'getAttachment'
| 'getInteger'
| 'getString'
| 'getChannel'
| 'getBoolean'
| 'getSubcommandGroup'
| 'getSubcommand'
>;
public get targetMessage(): NonNullable<CommandInteractionOption<Cached>['message']>;
public inGuild(): this is MessageContextMenuCommandInteraction<'raw' | 'cached'>;
public inCachedGuild(): this is MessageContextMenuCommandInteraction<'cached'>;
Expand Down Expand Up @@ -3233,6 +3237,21 @@ export class UserContextMenuCommandInteraction<
Cached extends CacheType = CacheType,
> extends ContextMenuCommandInteraction<Cached> {
public commandType: ApplicationCommandType.User;
public options: Omit<
CommandInteractionOptionResolver<Cached>,
| 'getMessage'
| 'getFocused'
| 'getMentionable'
| 'getRole'
| 'getNumber'
| 'getAttachment'
| 'getInteger'
| 'getString'
| 'getChannel'
| 'getBoolean'
| 'getSubcommandGroup'
| 'getSubcommand'
>;
public get targetUser(): User;
public get targetMember(): CacheTypeReducer<Cached, GuildMember, APIInteractionGuildMember> | null;
public inGuild(): this is UserContextMenuCommandInteraction<'raw' | 'cached'>;
Expand Down
29 changes: 29 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,8 @@ client.on('interactionCreate', async interaction => {
interaction.commandType === ApplicationCommandType.Message)
) {
expectType<MessageContextMenuCommandInteraction | UserContextMenuCommandInteraction>(interaction);
// @ts-expect-error No attachment options on contextmenu commands
interaction.options.getAttachment('name');
if (interaction.inCachedGuild()) {
expectAssignable<ContextMenuCommandInteraction>(interaction);
expectAssignable<Guild>(interaction.guild);
Expand Down Expand Up @@ -1836,12 +1838,36 @@ client.on('interactionCreate', async interaction => {
interaction.commandType === ApplicationCommandType.Message
) {
expectType<Message>(interaction.targetMessage);
expectType<Message | null>(interaction.options.getMessage('_MESSAGE'));
if (interaction.inCachedGuild()) {
expectType<Message<true>>(interaction.targetMessage);
expectType<Message<true> | null>(interaction.options.getMessage('_MESSAGE'));
} else if (interaction.inRawGuild()) {
expectType<Message<false>>(interaction.targetMessage);
expectType<Message<false> | null>(interaction.options.getMessage('_MESSAGE'));
} else if (interaction.inGuild()) {
expectType<Message>(interaction.targetMessage);
expectType<Message | null>(interaction.options.getMessage('_MESSAGE'));
}
}

if (
interaction.type === InteractionType.ApplicationCommand &&
interaction.commandType === ApplicationCommandType.User
) {
expectType<User>(interaction.targetUser);
expectType<GuildMember | APIInteractionGuildMember | null>(interaction.targetMember);
expectType<User | null>(interaction.options.getUser('user'));
expectType<GuildMember | APIInteractionDataResolvedGuildMember | null>(interaction.options.getMember('user'));
if (interaction.inCachedGuild()) {
expectType<GuildMember | null>(interaction.targetMember);
expectType<GuildMember | null>(interaction.options.getMember('user'));
} else if (interaction.inRawGuild()) {
expectType<APIInteractionGuildMember | null>(interaction.targetMember);
expectType<APIInteractionDataResolvedGuildMember | null>(interaction.options.getMember('user'));
} else if (interaction.inGuild()) {
expectType<GuildMember | APIInteractionGuildMember | null>(interaction.targetMember);
expectType<GuildMember | APIInteractionDataResolvedGuildMember | null>(interaction.options.getMember('user'));
}
}

Expand Down Expand Up @@ -1975,6 +2001,9 @@ client.on('interactionCreate', async interaction => {
expectType<string | null>(interaction.options.getSubcommandGroup());
expectType<string | null>(interaction.options.getSubcommandGroup(booleanValue));
expectType<string | null>(interaction.options.getSubcommandGroup(false));

// @ts-expect-error
interaction.options.getMessage('name');
}

if (interaction.isRepliable()) {
Expand Down

0 comments on commit 17a6f5d

Please sign in to comment.