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

feat!: Args support for ChatInputInteractions #506

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export * from './lib/errors/ArgumentError';
export * from './lib/errors/Identifiers';
export * from './lib/errors/PreconditionError';
export * from './lib/errors/UserError';
export * from './lib/parsers/Args';
export * from './lib/parsers/MessageArgs';
export * from './lib/plugins/Plugin';
export * from './lib/plugins/PluginManager';
export * from './lib/plugins/symbols';
Expand Down
9 changes: 9 additions & 0 deletions src/lib/parsers/ChatInputArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CacheType, CommandInteractionOptionResolver } from 'discord.js';

export class ChatInputArgs {
public readonly options: Omit<CommandInteractionOptionResolver<CacheType>, 'getMessage' | 'getFocused'>;

public constructor(options: Omit<CommandInteractionOptionResolver<CacheType>, 'getMessage' | 'getFocused'>) {
this.options = options;
}
}
54 changes: 27 additions & 27 deletions src/lib/parsers/Args.ts → src/lib/parsers/MessageArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { MessageCommand } from '../structures/Command';
/**
* The argument parser to be used in {@link Command}.
*/
export class Args {
export class MessageArgs {
/**
* The original message that triggered the command.
*/
Expand All @@ -50,8 +50,8 @@ export class Args {

/**
* The states stored in the args.
* @see Args#save
* @see Args#restore
* @see MessageArgs#save
* @see MessageArgs#restore
*/
private readonly states: ArgumentStream.State[] = [];

Expand All @@ -65,7 +65,7 @@ export class Args {
/**
* Sets the parser to the first token.
*/
public start(): Args {
public start(): MessageArgs {
this.parser.reset();
return this;
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export class Args {
}

/**
* Similar to {@link Args.pickResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.pickResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand All @@ -146,7 +146,7 @@ export class Args {
*/
public async pick<T>(type: IArgument<T>, options?: ArgOptions): Promise<T>;
/**
* Similar to {@link Args.pickResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.pickResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand Down Expand Up @@ -218,7 +218,7 @@ export class Args {
}

/**
* Similar to {@link Args.restResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.restResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand All @@ -231,7 +231,7 @@ export class Args {
*/
public async rest<T>(type: IArgument<T>, options?: ArgOptions): Promise<T>;
/**
* Similar to {@link Args.restResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.restResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand Down Expand Up @@ -312,7 +312,7 @@ export class Args {
}

/**
* Similar to {@link Args.repeatResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.repeatResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand All @@ -325,7 +325,7 @@ export class Args {
*/
public async repeat<T>(type: IArgument<T>, options?: RepeatArgOptions): Promise<T[]>;
/**
* Similar to {@link Args.repeatResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.repeatResult} but returns the value on success, throwing otherwise.
* @param type The type of the argument.
* @example
* ```typescript
Expand All @@ -343,9 +343,9 @@ export class Args {

/**
* Peeks the following parameter(s) without advancing the parser's state.
* Passing a function as a parameter allows for returning {@link Args.pickResult}, {@link Args.repeatResult},
* or {@link Args.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link Args.pickResult} and only peek a single argument.
* Passing a function as a parameter allows for returning {@link MessageArgs.pickResult}, {@link MessageArgs.repeatResult},
* or {@link MessageArgs.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link MessageArgs.pickResult} and only peek a single argument.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand All @@ -366,9 +366,9 @@ export class Args {
public async peekResult<T>(type: () => Argument.Result<T>): Promise<ResultType<T>>;
/**
* Peeks the following parameter(s) without advancing the parser's state.
* Passing a function as a parameter allows for returning {@link Args.pickResult}, {@link Args.repeatResult},
* or {@link Args.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link Args.pickResult} and only peek a single argument.
* Passing a function as a parameter allows for returning {@link MessageArgs.pickResult}, {@link MessageArgs.repeatResult},
* or {@link MessageArgs.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link MessageArgs.pickResult} and only peek a single argument.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand All @@ -385,9 +385,9 @@ export class Args {
public async peekResult<T>(type: IArgument<T>, options?: ArgOptions): Promise<ResultType<T>>;
/**
* Peeks the following parameter(s) without advancing the parser's state.
* Passing a function as a parameter allows for returning {@link Args.pickResult}, {@link Args.repeatResult},
* or {@link Args.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link Args.pickResult} and only peek a single argument.
* Passing a function as a parameter allows for returning {@link MessageArgs.pickResult}, {@link MessageArgs.repeatResult},
* or {@link MessageArgs.restResult}; otherwise, passing the custom argument or the argument type with options
* will use {@link MessageArgs.pickResult} and only peek a single argument.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand Down Expand Up @@ -419,7 +419,7 @@ export class Args {
}

/**
* Similar to {@link Args.peekResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.peekResult} but returns the value on success, throwing otherwise.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand All @@ -441,7 +441,7 @@ export class Args {
*/
public async peek<T>(type: () => Argument.Result<T>): Promise<T>;
/**
* Similar to {@link Args.peekResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.peekResult} but returns the value on success, throwing otherwise.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand All @@ -464,7 +464,7 @@ export class Args {
*/
public async peek<T>(type: IArgument<T>, options?: ArgOptions): Promise<T>;
/**
* Similar to {@link Args.peekResult} but returns the value on success, throwing otherwise.
* Similar to {@link MessageArgs.peekResult} but returns the value on success, throwing otherwise.
* @param type The function, custom argument, or argument name.
* @example
* ```typescript
Expand Down Expand Up @@ -518,7 +518,7 @@ export class Args {
}

/**
* Similar to {@link Args.nextMaybe} but returns the value on success, null otherwise.
* Similar to {@link MessageArgs.nextMaybe} but returns the value on success, null otherwise.
* @example
* ```typescript
* // !numbers 1 2 3
Expand All @@ -529,7 +529,7 @@ export class Args {
*/
public next(): string;
/**
* Similar to {@link Args.nextMaybe} but returns the value on success, null otherwise.
* Similar to {@link MessageArgs.nextMaybe} but returns the value on success, null otherwise.
* @typeparam T Output type of the {@link ArgsNextCallback callback}.
* @param cb Gives an option of either the resulting value, or nothing if failed.
* @example
Expand Down Expand Up @@ -612,15 +612,15 @@ export class Args {

/**
* Saves the current state into the stack following a FILO strategy (first-in, last-out).
* @see Args#restore
* @see MessageArgs#restore
*/
public save() {
this.states.push(this.parser.save());
}

/**
* Restores the previously saved state from the stack.
* @see Args#save
* @see MessageArgs#save
*/
public restore() {
if (this.states.length !== 0) this.parser.restore(this.states.pop()!);
Expand Down Expand Up @@ -729,7 +729,7 @@ export interface RepeatArgOptions extends ArgOptions {
}

/**
* The callback used for {@link Args.nextMaybe} and {@link Args.next}.
* The callback used for {@link MessageArgs.nextMaybe} and {@link MessageArgs.next}.
*/
export interface ArgsNextCallback<T> {
/**
Expand Down
10 changes: 5 additions & 5 deletions src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Result } from '@sapphire/result';
import type { Awaitable } from '@sapphire/utilities';
import type { Message } from 'discord.js';
import type { ArgumentError } from '../errors/ArgumentError';
import { Args } from '../parsers/Args';
import { MessageArgs } from '../parsers/MessageArgs';
import type { MessageCommand } from './Command';

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export interface IArgument<T> {

/**
* The base argument class. This class is abstract and is to be extended by subclasses implementing the methods. In
* Sapphire's workflow, arguments are called when using {@link Args}'s methods (usually used inside {@link Command}s by default).
* Sapphire's workflow, arguments are called when using {@link MessageArgs}'s methods (usually used inside {@link Command}s by default).
*
* @example
* ```typescript
Expand Down Expand Up @@ -99,23 +99,23 @@ export abstract class Argument<T = unknown, O extends Argument.Options = Argumen
* @param value The value to wrap.
*/
public ok(value: T): Argument.Result<T> {
return Args.ok(value);
return MessageArgs.ok(value);
}

/**
* Constructs an {@link Err} result containing an {@link ArgumentError} with a custom type.
* @param options The options to pass to the ArgumentError.
*/
public error(options: Omit<ArgumentError.Options<T>, 'argument'>): Argument.Result<T> {
return Args.error({ argument: this, identifier: this.name, ...options });
return MessageArgs.error({ argument: this, identifier: this.name, ...options });
}
}

export interface ArgumentOptions extends AliasPiece.Options {}

export interface ArgumentContext<T = unknown> extends Record<PropertyKey, unknown> {
argument: IArgument<T>;
args: Args;
args: MessageArgs;
message: Message;
command: MessageCommand;
commandContext: MessageCommand.RunContext;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Permissions,
Snowflake
} from 'discord.js';
import { Args } from '../parsers/Args';
import { MessageArgs } from '../parsers/MessageArgs';
import { BucketScope, RegisterBehavior } from '../types/Enums';
import { acquire } from '../utils/application-commands/ApplicationCommandRegistries';
import type { ApplicationCommandRegistry } from '../utils/application-commands/ApplicationCommandRegistry';
Expand All @@ -20,7 +20,7 @@ import { getNeededRegistryParameters } from '../utils/application-commands/getNe
import { PreconditionContainerArray, PreconditionEntryResolvable } from '../utils/preconditions/PreconditionContainerArray';
import { FlagStrategyOptions, FlagUnorderedStrategy } from '../utils/strategies/FlagUnorderedStrategy';

export class Command<PreParseReturn = Args, O extends Command.Options = Command.Options> extends AliasPiece<O> {
export class Command<PreParseReturn = MessageArgs, O extends Command.Options = Command.Options> extends AliasPiece<O> {
/**
* A basic summary about the command
* @since 1.0.0
Expand Down Expand Up @@ -126,7 +126,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
public messagePreParse(message: Message, parameters: string, context: MessageCommand.RunContext): Awaitable<PreParseReturn> {
const parser = new Parser(this.strategy);
const args = new ArgumentStream(parser.run(this.lexer.run(parameters)));
return new Args(message, this as any, args, context) as any;
return new MessageArgs(message, this as any, args, context) as any;
}

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ export class Command<PreParseReturn = Args, O extends Command.Options = Command.
/**
* Executes the message command's logic.
* @param message The message that triggered the command.
* @param args The value returned by {@link Command.messagePreParse}, by default an instance of {@link Args}.
* @param args The value returned by {@link Command.messagePreParse}, by default an instance of {@link MessageArgs}.
* @param context The context in which the command was executed.
*/
public messageRun?(message: Message, args: PreParseReturn, context: MessageCommand.RunContext): Awaitable<unknown>;
Expand Down