Get the value that was attempted to be parsed in an Arg
Hiya folks,
I have the following code, where I determine if the arg is either of my custom type (working), or if it's a server member. If it is neither, I want to throw out an error that I can handle, but as part of handling that error, I'd like to know the value that was attempted to be parsed.
in this example,
value
(inside the Error
) doesn't exist, but I think it's pretty clear what I'm trying to do.
I know that I could make an arg type that returns either an Npc or a Guild Member, and use parameter
in the Args.error
similar to what I do here...
But I'd rather not have to make a brand new arg type for trying to do something like this.
Any pointers?Solution:Jump to solution
Hiya folks,
I have the following code, where I determine if the arg is either of my custom type (working), or if it's a server member. If it is neither, I want to throw out an error that I can handle, but as part of handling that error, I'd like to know the value that was attempted to be parsed.
```ts...
7 Replies
Use Args.make for local art types that don't need to be in the argument folder
Or use pickResult which gives a result class back you can manipulate however you want.
<::746069730170896405> Sapphire docs results:
@sapphire/framework / Class: Args / make
<::746069730170896405> Sapphire docs results:
@sapphire/ / Module: @sapphire/result
is there a "shorter" way to use args.make? I do use that for the NPC arg, but if possible I'd like to avoid making a new arg type for this specific case, as I'm sure I'll have many more like it.
I guess there's some way to do this 'inline'? Right now, I have this in it's own args folder.
No that's the shortest way
And using pickResult probably wouldn't really be shorter either
Like at most a few lines but it comes with a complexity trade off imho
yeah fair enough. Okay, I'll just leave the parameter out of the error message, in the first snippet.
I might make a new arg for this case
On that note...
Is there some way to nest the custom args?
Ie could I have
Args.make<Npc>
as above, but another arg that is Args.Make<Npc | Player>
- which tries to sucessfully match against the straight Npc arg?
So I can avoid duplicating that code?No but you can extract the logic to another function and call that function from both places.
Just pass the parameters around as needed
For example make the function return a Boolean that you check in the Args.make to know if it's ok or error
That's a good point, I'll head down that path.
Cheers!