feat(Contexts): inGuild type guard (#290)

* feat(Contexts): inGuild type guard

* feat: isEntry
This commit is contained in:
Marcos Susaña 2024-11-06 16:31:56 -04:00 committed by GitHub
parent fb26e0e42e
commit 136262f989
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 41 additions and 10 deletions

View File

@ -214,9 +214,13 @@ export class CommandContext<
return this.interaction?.member || ((this.message! as MessageStructure)?.member as any); return this.interaction?.member || ((this.message! as MessageStructure)?.member as any);
} }
isChat(): this is CommandContext { isChat(): this is CommandContext<T, M> {
return true; return true;
} }
inGuild(): this is GuildCommandContext<T, M> {
return !!this.guildId;
}
} }
export interface GuildCommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never> export interface GuildCommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>

View File

@ -119,6 +119,14 @@ export class EntryPointContext<M extends keyof RegisteredMiddlewares = never> ex
get member() { get member() {
return this.interaction.member; return this.interaction.member;
} }
isEntryPoint(): this is EntryPointContext<M> {
return true;
}
inGuild(): this is GuildEntryPointContext<M> {
return !!this.guildId;
}
} }
export interface GuildEntryPointContext<M extends keyof RegisteredMiddlewares = never> export interface GuildEntryPointContext<M extends keyof RegisteredMiddlewares = never>

View File

@ -160,6 +160,10 @@ export class MenuCommandContext<
isMenuMessage(): this is MenuCommandContext<MessageCommandInteraction> { isMenuMessage(): this is MenuCommandContext<MessageCommandInteraction> {
return this.interaction.data.type === ApplicationCommandType.Message; return this.interaction.data.type === ApplicationCommandType.Message;
} }
inGuild(): this is GuildMenuCommandContext<T, M> {
return !!this.guildId;
}
} }
export interface GuildMenuCommandContext< export interface GuildMenuCommandContext<

View File

@ -2,6 +2,7 @@ import type { ModalContext } from '../components';
import type { ComponentContext, ContextComponentCommandInteractionMap } from '../components/componentcontext'; import type { ComponentContext, ContextComponentCommandInteractionMap } from '../components/componentcontext';
import type { MessageCommandInteraction, UserCommandInteraction } from '../structures'; import type { MessageCommandInteraction, UserCommandInteraction } from '../structures';
import type { CommandContext } from './applications/chatcontext'; import type { CommandContext } from './applications/chatcontext';
import type { EntryPointContext } from './applications/entrycontext';
import type { MenuCommandContext } from './applications/menucontext'; import type { MenuCommandContext } from './applications/menucontext';
import type { UsingClient } from './applications/shared'; import type { UsingClient } from './applications/shared';
@ -62,4 +63,8 @@ export class BaseContext {
isStringSelectMenu(): this is ComponentContext<'StringSelect'> { isStringSelectMenu(): this is ComponentContext<'StringSelect'> {
return false; return false;
} }
isEntryPoint(): this is EntryPointContext {
return false;
}
} }

View File

@ -219,29 +219,33 @@ export class ComponentContext<
return true; return true;
} }
isButton(): this is ComponentContext<'Button'> { isButton(): this is ComponentContext<'Button', M> {
return this.interaction.data.componentType === ComponentType.Button; return this.interaction.data.componentType === ComponentType.Button;
} }
isChannelSelectMenu(): this is ComponentContext<'ChannelSelect'> { isChannelSelectMenu(): this is ComponentContext<'ChannelSelect', M> {
return this.interaction.componentType === ComponentType.ChannelSelect; return this.interaction.componentType === ComponentType.ChannelSelect;
} }
isRoleSelectMenu(): this is ComponentContext<'RoleSelect'> { isRoleSelectMenu(): this is ComponentContext<'RoleSelect', M> {
return this.interaction.componentType === ComponentType.RoleSelect; return this.interaction.componentType === ComponentType.RoleSelect;
} }
isMentionableSelectMenu(): this is ComponentContext<'MentionableSelect'> { isMentionableSelectMenu(): this is ComponentContext<'MentionableSelect', M> {
return this.interaction.componentType === ComponentType.MentionableSelect; return this.interaction.componentType === ComponentType.MentionableSelect;
} }
isUserSelectMenu(): this is ComponentContext<'UserSelect'> { isUserSelectMenu(): this is ComponentContext<'UserSelect', M> {
return this.interaction.componentType === ComponentType.UserSelect; return this.interaction.componentType === ComponentType.UserSelect;
} }
isStringSelectMenu(): this is ComponentContext<'StringSelect'> { isStringSelectMenu(): this is ComponentContext<'StringSelect', M> {
return this.interaction.componentType === ComponentType.StringSelect; return this.interaction.componentType === ComponentType.StringSelect;
} }
inGuild(): this is GuildComponentContext<Type, M> {
return !!this.guildId;
}
} }
export interface ContextComponentCommandInteractionMap { export interface ContextComponentCommandInteractionMap {
@ -253,8 +257,10 @@ export interface ContextComponentCommandInteractionMap {
ChannelSelect: ChannelSelectMenuInteraction; ChannelSelect: ChannelSelectMenuInteraction;
} }
export interface GuildComponentContext<M extends keyof RegisteredMiddlewares = never> export interface GuildComponentContext<
extends Omit<MakeRequired<ComponentContext<M>, 'guildId'>, 'guild'> { Type extends keyof ContextComponentCommandInteractionMap,
M extends keyof RegisteredMiddlewares = never,
> extends Omit<MakeRequired<ComponentContext<Type, M>, 'guildId'>, 'guild'> {
guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>; guild(mode?: 'rest' | 'flow'): Promise<GuildStructure<'cached' | 'api'>>;
guild(mode?: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>; guild(mode?: 'cache'): ReturnCache<GuildStructure<'cached'> | undefined>;
} }

View File

@ -185,9 +185,13 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
return this.interaction.member; return this.interaction.member;
} }
isModal(): this is ModalContext { isModal(): this is ModalContext<M> {
return true; return true;
} }
inGuild(): this is GuildModalContext<M> {
return !!this.guildId;
}
} }
export interface GuildModalContext<M extends keyof RegisteredMiddlewares = never> export interface GuildModalContext<M extends keyof RegisteredMiddlewares = never>