Skip to content

Commit

Permalink
fix: use declare for store properties to avoid the need for `as *St…
Browse files Browse the repository at this point in the history
…ore` (#602)

This ensures that people no longer need to write code such as
`const commandsStore = this.store as CommandStore`
but instead can keep out the typecast.

resolves #382
  • Loading branch information
favna committed Feb 5, 2023
1 parent b4c252e commit b243bcd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Awaitable } from '@sapphire/utilities';
import type { Message } from 'discord.js';
import type { ArgumentError } from '../errors/ArgumentError';
import { Args } from '../parsers/Args';
import type { ArgumentStore } from './ArgumentStore';
import type { MessageCommand } from './Command';

/**
Expand Down Expand Up @@ -102,6 +103,11 @@ export interface IArgument<T> {
* ```
*/
export abstract class Argument<T = unknown, O extends Argument.Options = Argument.Options> extends AliasPiece<O> implements IArgument<T> {
/**
* The {@link ArgumentStore} that contains this {@link Argument}.
*/
public declare store: ArgumentStore;

public abstract run(parameter: string, context: Argument.Context<T>): Argument.AwaitableResult<T>;

/**
Expand Down
7 changes: 6 additions & 1 deletion src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import { FlagUnorderedStrategy, type FlagStrategyOptions } from '../utils/strate
import type { CommandStore } from './CommandStore';

export class Command<PreParseReturn = Args, O extends Command.Options = Command.Options> extends AliasPiece<O> {
/**
* The {@link CommandStore} that contains this {@link Command}.
*/
public declare store: CommandStore;

/**
* A basic summary about the command
* @since 1.0.0
Expand Down Expand Up @@ -249,7 +254,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.

public override async reload() {
// Remove the aliases from the command store
const store = this.store as CommandStore;
const { store } = this;
const registry = this.applicationCommandRegistry;

for (const nameOrId of registry.chatInputCommands) {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/structures/InteractionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { Piece } from '@sapphire/pieces';
import { Option } from '@sapphire/result';
import type { Awaitable } from '@sapphire/utilities';
import type { Interaction } from 'discord.js';
import type { InteractionHandlerStore } from './InteractionHandlerStore';

export abstract class InteractionHandler<O extends InteractionHandler.Options = InteractionHandler.Options> extends Piece<O> {
/**
* The {@link InteractionHandlerStore} that contains this {@link InteractionHandler}.
*/
public declare store: InteractionHandlerStore;

/**
* The type for this handler
* @since 3.0.0
Expand Down
6 changes: 6 additions & 0 deletions src/lib/structures/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Result } from '@sapphire/result';
import type { Client, ClientEvents } from 'discord.js';
import type { EventEmitter } from 'node:events';
import { Events } from '../types/Events';
import type { ListenerStore } from './ListenerStore';

/**
* The base event class. This class is abstract and is to be extended by subclasses, which should implement the methods. In
Expand Down Expand Up @@ -44,6 +45,11 @@ import { Events } from '../types/Events';
* ```
*/
export abstract class Listener<E extends keyof ClientEvents | symbol = '', O extends Listener.Options = Listener.Options> extends Piece<O> {
/**
* The {@link ListenerStore} that contains this {@link Listener}.
*/
public declare store: ListenerStore;

/**
* The emitter, if any.
* @since 2.0.0
Expand Down
6 changes: 6 additions & 0 deletions src/lib/structures/Precondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import type { CooldownPreconditionContext } from '../../preconditions/Cooldown';
import { PreconditionError } from '../errors/PreconditionError';
import type { UserError } from '../errors/UserError';
import type { ChatInputCommand, ContextMenuCommand, MessageCommand } from './Command';
import type { PreconditionStore } from './PreconditionStore';

export type PreconditionResult = Awaitable<Result<unknown, UserError>>;
export type AsyncPreconditionResult = Promise<Result<unknown, UserError>>;

export class Precondition<O extends Precondition.Options = Precondition.Options> extends Piece<O> {
/**
* The {@link PreconditionStore} that contains this {@link Precondition}.
*/
public declare store: PreconditionStore;

public readonly position: number | null;

public constructor(context: Piece.Context, options: Precondition.Options = {}) {
Expand Down

0 comments on commit b243bcd

Please sign in to comment.