1- import type { AnyInteraction , ChannelTypes , GuildBasedChannelTypes } from '@sapphire/discord.js-utilities' ;
21import { join , type Parameter } from '@sapphire/lexure' ;
3- import { container } from '@sapphire/pieces' ;
4- import { Option , Result } from '@sapphire/result' ;
5- import type {
6- CategoryChannel ,
7- ChannelType ,
8- ChatInputCommandInteraction ,
9- CommandInteraction ,
10- DMChannel ,
11- GuildMember ,
12- Message ,
13- NewsChannel ,
14- Role ,
15- StageChannel ,
16- TextChannel ,
17- ThreadChannel ,
18- User ,
19- VoiceChannel
20- } from 'discord.js' ;
21- import type { URL } from 'node:url' ;
2+ import { Result } from '@sapphire/result' ;
3+ import type { ChatInputCommandInteraction , CommandInteractionOption } from 'discord.js' ;
224import { ArgumentError } from '../errors/ArgumentError' ;
23- import { Identifiers } from '../errors/Identifiers' ;
245import { UserError } from '../errors/UserError' ;
25- import type { EmojiObject } from '../resolvers/emoji' ;
26- import type { IArgument } from '../structures/Argument' ;
276import { Command } from '../structures/Command' ;
28- import { Args , type ArgsOptions , type InferArgReturnType , type PeekArgsOptions , type RepeatArgsOptions } from './Args' ;
7+ import {
8+ Args ,
9+ type ArgsJson ,
10+ type ArgsOptions ,
11+ type ArrayResultType ,
12+ type InferArgReturnType ,
13+ type PeekArgsOptions ,
14+ type RepeatArgsOptions ,
15+ type ResultType
16+ } from './Args' ;
2917import type { ChatInputParser } from './ChatInputParser' ;
18+ import type { ChatInputCommand } from '../types/CommandTypes' ;
3019
3120/**
3221 * The argument parser to be used in {@link Command}.
@@ -40,7 +29,7 @@ export class ChatInputCommandArgs extends Args {
4029 /**
4130 * The command that is being run.
4231 */
43- public readonly command : Command ;
32+ public readonly command : ChatInputCommand ;
4433
4534 /**
4635 * The context of the command being run.
@@ -57,9 +46,14 @@ export class ChatInputCommandArgs extends Args {
5746 * @see Args#save
5847 * @see Args#restore
5948 */
60- private readonly states : number [ ] = [ ] ;
61-
62- public constructor ( interaction : ChatInputCommandInteraction , command : Command , parser : ChatInputParser , context : Record < PropertyKey , unknown > ) {
49+ private readonly states : Set < CommandInteractionOption > [ ] = [ ] ;
50+
51+ public constructor (
52+ interaction : ChatInputCommandInteraction ,
53+ command : ChatInputCommand ,
54+ parser : ChatInputParser ,
55+ context : Record < PropertyKey , unknown >
56+ ) {
6357 super ( ) ;
6458 this . interaction = interaction ;
6559 this . command = command ;
@@ -126,7 +120,7 @@ export class ChatInputCommandArgs extends Args {
126120 const argument = this . resolveArgument ( options . type ) ;
127121 if ( ! argument ) return this . unavailableArgument ( options . type ) ;
128122
129- const result = await this . parser . singleParseAsync ( async ( arg ) =>
123+ const result = await this . parser . singleParseAsync ( options . name , async ( arg ) =>
130124 argument . run ( arg , {
131125 args : this ,
132126 argument,
@@ -317,7 +311,7 @@ export class ChatInputCommandArgs extends Args {
317311 const output : InferArgReturnType < T > [ ] = [ ] ;
318312
319313 for ( let i = 0 , times = options . times ?? Infinity ; i < times ; i ++ ) {
320- const result = await this . parser . singleParseAsync ( async ( arg ) =>
314+ const result = await this . parser . singleParseAsync ( options . name , async ( arg ) =>
321315 argument . run ( arg , {
322316 args : this ,
323317 argument,
@@ -552,75 +546,7 @@ export class ChatInputCommandArgs extends Args {
552546 return { message : this . interaction , command : this . command , commandContext : this . commandContext } ;
553547 }
554548
555- protected unavailableArgument < T > ( type : string | IArgument < T > ) : Result . Err < UserError > {
556- const name = typeof type === 'string' ? type : type . name ;
557- return Result . err (
558- new UserError ( {
559- identifier : Identifiers . ArgsUnavailable ,
560- message : `The argument "${ name } " was not found.` ,
561- context : { name, ...this . toJSON ( ) }
562- } )
563- ) ;
564- }
565-
566- protected missingArguments ( ) : Result . Err < UserError > {
567- return Result . err ( new UserError ( { identifier : Identifiers . ArgsMissing , message : 'There are no more arguments.' , context : this . toJSON ( ) } ) ) ;
549+ private getCommandOption ( name : string ) : CommandInteractionOption | undefined {
550+ return this . interaction . options . data . find ( ( option ) => option . name === name ) ;
568551 }
569-
570- /**
571- * Resolves an argument.
572- * @param arg The argument name or {@link IArgument} instance.
573- */
574- private resolveArgument < T > ( arg : keyof ArgType | IArgument < T > ) : IArgument < T > | undefined {
575- if ( typeof arg === 'object' ) return arg ;
576- return container . stores . get ( 'arguments' ) . get ( arg as string ) as IArgument < T > | undefined ;
577- }
578- }
579-
580- export interface ArgsJson {
581- message : Message | AnyInteraction ;
582- command : Command ;
583- commandContext : Record < PropertyKey , unknown > ;
584- }
585-
586- export interface ArgType {
587- boolean : boolean ;
588- channel : ChannelTypes ;
589- date : Date ;
590- dmChannel : DMChannel ;
591- emoji : EmojiObject ;
592- float : number ;
593- guildCategoryChannel : CategoryChannel ;
594- guildChannel : GuildBasedChannelTypes ;
595- guildNewsChannel : NewsChannel ;
596- guildNewsThreadChannel : ThreadChannel & { type : ChannelType . AnnouncementThread ; parent : NewsChannel | null } ;
597- guildPrivateThreadChannel : ThreadChannel & { type : ChannelType . PrivateThread ; parent : TextChannel | null } ;
598- guildPublicThreadChannel : ThreadChannel & { type : ChannelType . PublicThread ; parent : TextChannel | null } ;
599- guildStageVoiceChannel : StageChannel ;
600- guildTextChannel : TextChannel ;
601- guildThreadChannel : ThreadChannel ;
602- guildVoiceChannel : VoiceChannel ;
603- hyperlink : URL ;
604- integer : number ;
605- member : GuildMember ;
606- message : Message ;
607- number : number ;
608- role : Role ;
609- string : string ;
610- url : URL ;
611- user : User ;
612- enum : string ;
613- }
614-
615- /**
616- * The callback used for {@link Args.nextMaybe} and {@link Args.next}.
617- */
618- export interface ArgsNextCallback < T > {
619- /**
620- * The value to be mapped.
621- */
622- ( value : CommandInteraction ) : Option < T > ;
623552}
624-
625- export type ResultType < T > = Result < T , UserError | ArgumentError < T > > ;
626- export type ArrayResultType < T > = Result < T [ ] , UserError | ArgumentError < T > > ;
0 commit comments