1
- import type { AnyInteraction , ChannelTypes , GuildBasedChannelTypes } from '@sapphire/discord.js-utilities' ;
2
1
import { 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' ;
22
4
import { ArgumentError } from '../errors/ArgumentError' ;
23
- import { Identifiers } from '../errors/Identifiers' ;
24
5
import { UserError } from '../errors/UserError' ;
25
- import type { EmojiObject } from '../resolvers/emoji' ;
26
- import type { IArgument } from '../structures/Argument' ;
27
6
import { 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' ;
29
17
import type { ChatInputParser } from './ChatInputParser' ;
18
+ import type { ChatInputCommand } from '../types/CommandTypes' ;
30
19
31
20
/**
32
21
* The argument parser to be used in {@link Command}.
@@ -40,7 +29,7 @@ export class ChatInputCommandArgs extends Args {
40
29
/**
41
30
* The command that is being run.
42
31
*/
43
- public readonly command : Command ;
32
+ public readonly command : ChatInputCommand ;
44
33
45
34
/**
46
35
* The context of the command being run.
@@ -57,9 +46,14 @@ export class ChatInputCommandArgs extends Args {
57
46
* @see Args#save
58
47
* @see Args#restore
59
48
*/
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
+ ) {
63
57
super ( ) ;
64
58
this . interaction = interaction ;
65
59
this . command = command ;
@@ -126,7 +120,7 @@ export class ChatInputCommandArgs extends Args {
126
120
const argument = this . resolveArgument ( options . type ) ;
127
121
if ( ! argument ) return this . unavailableArgument ( options . type ) ;
128
122
129
- const result = await this . parser . singleParseAsync ( async ( arg ) =>
123
+ const result = await this . parser . singleParseAsync ( options . name , async ( arg ) =>
130
124
argument . run ( arg , {
131
125
args : this ,
132
126
argument,
@@ -317,7 +311,7 @@ export class ChatInputCommandArgs extends Args {
317
311
const output : InferArgReturnType < T > [ ] = [ ] ;
318
312
319
313
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 ) =>
321
315
argument . run ( arg , {
322
316
args : this ,
323
317
argument,
@@ -551,76 +545,4 @@ export class ChatInputCommandArgs extends Args {
551
545
public toJSON ( ) : ArgsJson {
552
546
return { message : this . interaction , command : this . command , commandContext : this . commandContext } ;
553
547
}
554
-
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 ( ) } ) ) ;
568
- }
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
548
}
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 > ;
623
- }
624
-
625
- export type ResultType < T > = Result < T , UserError | ArgumentError < T > > ;
626
- export type ArrayResultType < T > = Result < T [ ] , UserError | ArgumentError < T > > ;
0 commit comments